Header Ads

Header ADS

Chika and Friendly Pairs(HDU-6534)

 Problem Link:https://vjudge.net/problem/HDU-6534

Give you a sequence, ask multiple times, and each time let you answer the logarithm of the element whose absolute value of the difference in an interval does not exceed a given constant K. Discretize all the elements in the sequence and the values ​​of these elements +K and -K. Then use the Mo team algorithm, in the process of moving the endpoint of the Mo team algorithm, insert a new element x in the process of inserting it into the tree array, and query the number of elements in the range of [xK, x+K]. The process of deleting an element is similar.

Discretization + Mo team + tree array, the tree array maintains the changes contributed by each pointer movement.

Solution:

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<string>

#include<vector>

#include<stack>

#include<bitset>

#include<cstdlib>

#include<cmath>

#include<set>

#include<map>

#include<list>

#include<deque>

#include<queue>

using namespace std;

typedef long long ll;

static const ll mod=1000000000+7;

const ll N=100010;

struct nnode{

    int l,r,id;

}g[N];

int n,m,k,unit,a[N];

ll num[N],res[N],tre[N],tmp;

void add_tree(ll idx,ll val){

    while(idx<=N){

        tre[idx]+=val;

        idx+=idx&-idx;

    }

}

ll query(ll idx){

    ll sum=0;

    idx=min(idx,N);

    while(idx>0){

        sum+=tre[idx];

        idx-=idx&-idx;

    }

    return sum;

}

void add(int i){

    tmp+=query(a[i]+k);

    tmp-=(a[i]-k-1)>0?query(a[i]-k-1):0;

    add_tree(a[i],1);

}

void del(int i){

    add_tree(a[i],-1);

    tmp-=query(a[i]+k);

    tmp+=(a[i]-k-1)>0?query(a[i]-k-1):0;

}

void solve(){

    tmp=0;

    memset(num,0,sizeof(num));

    unit=(int)sqrt(1.0*n);

    sort(g+1,g+1+m,[](nnode a,nnode b){return a.l/unit!=b.l/unit?a.l/unit<b.l/unit:a.r<b.r;});

    ll l=1,r=0;

    for(int i=1;i<=m;i++){

        while(r<g[i].r)add(++r);

        while(r>g[i].r)del(r--);

        while(l<g[i].l)del(l++);

        while(l>g[i].l)add(--l);

        res[g[i].id]=tmp;

    }

    for(int i=1;i<=m;i++)printf("%d\n",res[i]);

}

int main()

{

    scanf("%d%d%d",&n,&m,&k);

    for(int i=1;i<=n;i++)cin>>a[i];

    for(int i=1;i<=m;i++){

        scanf("%d%d",&g[i].l,&g[i].r);

        g[i].id=i;

    }

    solve();

}


No comments

Theme images by enot-poloskun. Powered by Blogger.