2019icpc Nanjing tournament F Chairman tree network

The meaning of problems

To a \ (n-\) a full permutation array \ (A \) , find a value of each array a recursive: \ (ANS [I] = ANS [J] + 1'd \) , \ (J \) is \ (a [pos [i] -k] to a [pos [i] + k ], (pos [i] for the i subscript in the array a) \) is less than the \ (i \) a maximum value .

analysis

This question set practice better, but want to practice in half of the President of the trees.

President tree built by weight, for each \ (I \) to query \ (a [pos [i] -k] to a [pos [i] + k ] \) is less than \ (I \) maximum , son of the first queries the right query, the query again left his son, because his son must find the right than the left big son, until you find a spot to meet the conditions.

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
int T;
int n,k;
int a[maxn],b[maxn],ans[maxn],tr[maxn*30],ls[maxn*30],rs[maxn*30],rt[maxn],tot;
void bd(int l,int r,int &p){
    tr[++tot]=tr[p],ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
    if(l==r) return;
    int mid=l+r>>1;
    bd(l,mid,ls[p]);bd(mid+1,r,rs[p]);
}
void up(int k,int l,int r,int &p){
    tr[++tot]=tr[p]+1,ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
    if(l==r) return;
    int mid=l+r>>1; 
    if(k<=mid) up(k,l,mid,ls[p]);
    else up(k,mid+1,r,rs[p]);
}
int qy(int dl,int dr,int l,int r,int a,int b){
    if(tr[b]-tr[a]==0) return 0;
    if(l>=dl&&r<=dr){
        if(l==r) return l;
        int mid=l+r>>1,res=0;
        if(dl<=mid) res=qy(dl,dr,l,mid,ls[a],ls[b]);
        if(dr>mid&&res==0) res=qy(dl,dr,mid+1,r,rs[a],rs[b]);
        return res;
    }int mid=l+r>>1,res=0;
    if(dl<=mid) res=qy(dl,dr,l,mid,ls[a],ls[b]);
    if(dr>mid&&res==0) res=qy(dl,dr,mid+1,r,rs[a],rs[b]);
    return res;
}
int main(){
    //ios::sync_with_stdio(false);
    //freopen("in","r",stdin);
    scanf("%d",&T);
    while(T--){
        tot=0;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            b[a[i]]=i;
        }
        bd(1,n,rt[0]);
        for(int i=1;i<=n;i++){
            rt[i]=rt[i-1];
            up(a[i],1,n,rt[i]);
        }ans[1]=1;
        for(int i=2;i<=n;i++){
            int l=max(0,b[i]-k-1),r=min(n,b[i]+k);
            ans[i]=ans[qy(1,i-1,1,n,rt[l],rt[r])]+1;
        }
        for(int i=1;i<=n;i++){
            printf("%d",ans[i]);
            if(i==n) printf("\n");
            else printf(" ");
            ans[i]=0;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xyq0220/p/11443966.html