2019 Hang electric multi-school season fourth HDU6621 K-th Closest Distance Chairman tree half

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=6621

That Italy: (3) T sample group, the n (1e5) number, q (1e5) queries, the query [l, r], | a [i] - p | k of a large number, and mandatory off of

Analysis: The initial idea is to follow the changes, thinking to do with the dynamic President of the trees, but does not, but also very slow, in fact, can do by static Chairman tree

Chairman of the tree node is directly 1e6 values, does not need to be discrete, we are the number of occurrences for each value of statistics alone

With binary, query (p-mid, p + mid) the number of the number of specific implementation look at the code, the complexity is O (q * log (m) * log (m)), the query code is changed a bit like a segment tree interval sum, it can help to understand

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod=1e9+7;
const int maxn=1e5+7;
const int maxm=1e4+7;
const int  inf=1<<30;
const int M=1e6;
const double pi=acos(-1);
int nodeNum;                                          
int L[maxn<<6],R[maxn<<6],sum[maxn<<6];                          
int a[maxn];                                                           
int T[maxn];                                                                      

int read()                                               
{                                                         
    int ans=0,flag=1;char ch=getchar();
    while(ch<'0'||ch>' . 9 ' ) { IF (CH == ' - ' ) In Flag = - . 1 ; CH = getchar ();}
     the while (CH> = ' 0 ' && CH <= ' . 9 ' ) = {ANS (ANS << . 3 ) + (ANS << . 1 ) CH-, + ' 0 ' ; CH = getchar ();}
     return ANS * In Flag; 
}                                    
int Update ( int pre, int L, int R & lt, int X) // pre-tree of the old location the number of nodes
{
    int num=++nodeNum; 
    L[num]=L[pre];R[num]=R[pre];sum[num]=sum[pre]+1;
    
    if(l!=r)
    {
        int m=(l+r)>>1;
        if(x<=m) L[num]=update(L[pre],l,m,x);
        else R[num]=update(R[pre],m+1,r,x);
    }
    return num;
}

int query(int u,int v,int l,int r,int pl,int pr) 
{
    if(l>=pl&&r<=pr) return sum[v]-sum[u];
    int res=0;
    int mid=(l+r)>>1;
    if(pl<=mid) res+=query(L[u],L[v],l,mid,pl,pr);
    if(pr>mid) res+=query(R[u],R[v],mid+1,r,pl,pr);
    return res;
}

int main(){
    int Test;Test=read();
    while(Test--){
        int n,m;n=read();m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            T[i]=update(T[i-1],1,M,a[i]);
        }
        int ans=0;
        while(m--){
            int l,r,p,k;l=read();r=read();p=read();k=read();
            l=l^ans,r=r^ans,p=p^ans,k=k^ans;
            int left=0,right=M;
            while(left<=right){
                int mid=(left+right)>>1;
                if(query(T[l-1],T[r],1,M,max(1,p-mid),min(M,p+mid))>=k){
                    ans=mid;
                    right=mid-1;
                }
                else left=mid+1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11298609.html