[JZOJ6257] [8.9] road simulation provincial election

topic

Subject to the effect

There are a bunch of points, each point has its weight \ (C_i \) .
Each insertion side \ ((U, V) \) , \ (U \) and \ (1 \) communication, \ (V \) and \ (1 \) do not communicate. Finally, to ensure the formation of a tree.
Each insertion of the interrogation \ (1 \) to \ (U \) number on the reverse path pair. Then \ (1 \) to \ (U \) right to all nodes on the path is set to the value \ (C_V \) .


Reflections on History

To see that this is what the data structure that ......
but just do not know how to do for a long time ......
so he beat direct violence. Violence jump \ (FA \) , calculated using the number on the reverse Fenwick tree.
Later, a little bit of time, so the sights \ (c_i \ leq 2 \) data.
So called split tree plus strand segment tree is maintained. Each tree line maintenance interval is of size \ (3 \) of the barrel and answers, the interval time of the merger is that both sides plus the right answer left is greater than the number on the right.
After playing a hurry to eat, did not transfer over ......
later discovered that this tree is not divided ...... split chain'd cover my original violence is violence ...... can eat \ (c_i \ leq 2 \) data ......


Correct

When I see positive solutions also shocked ......
please look at the screen simulation operation with the mind.
Then try with \ (LCT \) to establish contact.

So we find that this process operation \ (LCT \) bore a striking resemblance!
Each (splay \) \ maintain the same weight of a chain, modify the time equivalent to \ (access \) up ......
it all to ancestral path becomes the same weight.
We also know that \ (LCT \) time complexity is shared equally \ (\ lg n \) is.Although not proven.
So we can understand the number of the same color into a segment that appeared \ (O (n \ lg n ) \) level.
When asked by Fenwick tree to maintain. Analog \ (access \) process on it.

So the question is very easy to the AC. And because this problem completely without \ (mroot \) operation, so do not flip ......
\ (LCT \) short-chain takes a tree split almost ......


Code

using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
int n;
int c[N],maxc;
int *p[N];
inline bool cmpp(int *x,int *y){
    return *x<*y;
}
int t[N];
#define lowbit(x) ((x)&(-(x)))
inline void add(int x,int c){
    for (;x<=maxc;x+=lowbit(x))
        t[x]+=c;
}
inline int query(int x){
    int res=0;
    for (;x;x-=lowbit(x))
        res+=t[x];
    return res;
}
inline void clear(int x){
    for (;x<=maxc && t[x];x+=lowbit(x))
        t[x]=0;
}
struct Node{
    Node *fa,*c[2];
    int is_root;
    int siz;
    inline void update(){siz=c[0]->siz+c[1]->siz+1;}
    inline bool getson(){return fa->c[0]!=this;}
    inline void rotate(){
        Node *y=fa,*z=y->fa;
        if (y->is_root){
            is_root=y->is_root;
            y->is_root=0;
        }
        else
            z->c[y->getson()]=this;
        int k=getson();
        fa=z;
        y->c[k]=c[k^1];
        c[k^1]->fa=y;
        c[k^1]=y;
        y->fa=this;
        siz=y->siz,y->update();
    }
    inline void splay(){
        while (!is_root){
            if (!fa->is_root){
                if (getson()!=fa->getson())
                    rotate();
                else
                    fa->rotate();
            }
            rotate();
        }
    }
} d[N],*null;
int main(){
    freopen("road.in","r",stdin);
    freopen("road.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
        scanf("%d",&c[i]),p[i]=&c[i];
    sort(p+1,p+n+1,cmpp);
    for (int i=1,last=-1;i<=n;++i){
        if (*p[i]!=last){
            maxc++;
            last=*p[i];
        }
        *p[i]=maxc;
    }
    null=d;
    *null={null,null,null,0,0};
    for (int i=1;i<=n;++i)
        d[i]={null,null,null,c[i],1};
    for (int i=1;i<n;++i){
        int u,v;
        scanf("%d%d",&u,&v);
        Node *x,*y;
        long long ans=0;
        for (x=&d[u];x!=null;x=x->fa){
            x->splay();
            ans+=(long long)query(x->is_root-1)*(x->c[0]->siz+1);
            add(x->is_root,x->c[0]->siz+1);
        }
        printf("%lld\n",ans);
        d[v].fa=&d[u];
        for (x=&d[v],y=null;x!=null;y=x,x=x->fa){
            x->splay();
            clear(x->is_root);
            x->c[1]->is_root=x->is_root;
            x->c[1]=y;
            y->is_root=0;
            x->update();
        }
        y->is_root=c[v];
    }
    return 0;
}

Reflections on History

When analyzing the complexity, you may try to combine their own data structures learned ......

Guess you like

Origin www.cnblogs.com/jz-597/p/11329667.html