【GDOI2017第四輪模擬day2】絕版題

題目大意

對於一棵樹,q個操作可以新增節點或改變一個點的權值,或詢問整棵樹的帶權重心,強制在線
1q3×105

題解

考慮如何找帶權重心,顯然是每次往最大權的子樹走,條件是這個子樹的權×2大於整棵樹的權值和。
那麼就很明顯了,我們要做的是維護以每個點爲根的子樹的權值和,以及每個點的兒子中的最大權。
由於一條鏈上的信息更改會影響很多點,那麼我們可以用LCT,維護的是以每個點爲根的子樹的權值和,以及每個點的虛邊兒子中的最大權。
對於後者,可以對每個點開一個set,在斷開和連起來的時候都更改一下就好了
找答案的時候,我們考慮從根節點開始,每次在鏈上二分出要走到的位置,然後再走到他的最大兒子。
這樣的複雜度貌似很懸?
我們這樣做其實相當於從根走到重心,這根重心做一次access的複雜度是一樣的,所以總的時間複雜度是O(nlog2n)

Code

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>

#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)

using namespace std;

typedef long long LL;
typedef double db;

int get(){
    char ch;
    while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
    if (ch=='-'){
        int s=0;
        while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
        return -s;
    }
    int s=ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
    return s;
}

const int N = 300010;

struct light_edge{
    int x;
    LL s;
    friend bool operator <(light_edge a,light_edge b){
        if (a.s!=b.s)return a.s>b.s;
        return a.x<b.x;
    }
};
set<light_edge>s[N];
struct point{
    int s[2],val,ad,tot;
    LL sum,mv;
}tree[N];
int fa[N];
int n;
int m;
int a[N],k;
LL val[N];
int w[N];

LL maxll(LL x,LL y){
    if (x>y)return x;
    return y;
}

void update(int x){
    tree[x].tot=tree[tree[x].s[0]].tot+tree[tree[x].s[1]].tot+1;
    tree[x].mv=maxll(tree[tree[x].s[0]].mv+tree[x].ad,maxll(tree[tree[x].s[1]].mv+tree[x].ad,tree[x].sum));
}

void down(int x){
    if (tree[x].ad!=0){
        int y=tree[x].s[0];
        if (y){
            tree[y].sum=tree[y].sum+tree[x].ad;
            tree[y].mv=tree[y].mv+tree[x].ad;
            tree[y].ad=tree[y].ad+tree[x].ad;
        }
        y=tree[x].s[1];
        if (y){
            tree[y].sum=tree[y].sum+tree[x].ad;
            tree[y].mv=tree[y].mv+tree[x].ad;
            tree[y].ad=tree[y].ad+tree[x].ad;
        }
        tree[x].ad=0;
    }
    update(x);
}

int pd(int x){
    if (tree[fa[x]].s[0]==x)return 0;
    if (tree[fa[x]].s[1]==x)return 1;
    return -1;
}

void clear(int x){
    k=0;
    for(;pd(x)!=-1;x=fa[x])a[++k]=x;
    a[++k]=x;
    fd(i,k,1)down(a[i]);
}

void rotate(int x){
    int y=fa[x],z=fa[y];
    int tx=pd(x),ty=pd(y);
    if (ty!=-1)tree[z].s[ty]=x;
    fa[x]=z;
    if (tree[x].s[tx^1])fa[tree[x].s[tx^1]]=y;
    fa[y]=x;
    tree[y].s[tx]=tree[x].s[tx^1];
    tree[x].s[tx^1]=y;
    update(y);
    update(x);
    if (ty!=-1)update(z);
}

void splay(int x){
    clear(x);
    while(pd(x)!=-1){
        if (pd(fa[x])!=-1){
            if (pd(fa[x])==pd(x))rotate(fa[x]);
            else rotate(x);
        }
        rotate(x);
    }
}

int get1st(int x){
    if (!x)return 0;
    down(x);
    if (tree[x].s[0])return get1st(tree[x].s[0]);
    return x;
}

void put_light(int x,int y,LL sum){
    fa[y]=x;
    light_edge u;
    u.x=y;u.s=sum;
    s[x].insert(u);
    if (sum>val[x]){
        val[x]=sum;
        w[x]=y;
    }
}

void del_light(int x,int y,LL sum){
    light_edge u;
    u.x=y;u.s=sum;
    s[x].erase(u);
    set<light_edge>::iterator h=s[x].begin();
    if (h!=s[x].end()){
        w[x]=(*h).x;
        val[x]=(*h).s;
    }
    else val[x]=w[x]=0;
}

void split(int x,int y){
    splay(y);
    splay(x);
    tree[x].s[1]=0;
    update(x);
    put_light(x,y,tree[y].sum);
}

void merge(int x,int y){
    splay(y);
    del_light(x,y,tree[y].sum);
    tree[x].s[1]=y;
    update(x);
}

void access(int x){
    splay(x);
    if (tree[x].s[1])split(x,get1st(tree[x].s[1]));
    while(fa[x]){
        int y=fa[x];
        splay(y);
        if (tree[y].s[1])split(y,get1st(tree[y].s[1]));
        merge(y,get1st(x));
        splay(x);
    }
}

void walk(int &x){x=w[x];}

int kth(int x,int k){
    if (k>tree[x].tot)return 0;
    if (k<=tree[tree[x].s[0]].tot)return kth(tree[x].s[0],k);
    if (k==tree[tree[x].s[0]].tot+1)return x;
    return kth(tree[x].s[1],k-tree[tree[x].s[0]].tot-1);
}

int getw(int x){
    down(x);
    if (tree[x].s[1]&&tree[tree[x].s[1]].mv*2>tree[1].sum)return getw(tree[x].s[1]);
    if (tree[x].sum*2>tree[1].sum)return x;
    if (tree[x].s[0]&&tree[tree[x].s[0]].mv*2>tree[1].sum)return getw(tree[x].s[0]);
    return x;
}

int ans;

void getans(){
    int x=1;
    ans=1;
    LL tv=1e+13;
    bool bz=1;
    while(bz&&x){
        bz=0;
        splay(x);
        int w=getw(x);
        splay(w);
        int u=get1st(tree[w].s[1]);
        LL tmp=maxll(val[w],maxll(tree[u].sum,tree[1].sum-tree[w].sum));
        if (tmp<tv){
            tv=tmp;
            ans=w;
            x=w;
            walk(x);
            bz=1;
        }
        if (u){
            splay(u);
            w=get1st(tree[u].s[1]);
            tmp=maxll(val[u],maxll(tree[w].sum,tree[1].sum-tree[u].sum));
            if (tmp<tv){
                tv=tmp;
                ans=u;
                x=u;
                walk(x);
                bz=1;
            }
        }
    }
    printf("%d\n",ans);
}

int main(){
    freopen("jueban.in","r",stdin);
    freopen("jueban.out","w",stdout);
    m=get();tree[n=1].val=tree[1].sum=get();
    ans=0;
    fo(cas,1,m){
        int ty=get();
        if (ty==1){
            int x=get(),t=get();
            x^=ans;t^=ans;
            tree[++n].val=tree[n].sum=t;
            fa[n]=x;
            access(x);
            splay(x);
            tree[x].ad=tree[x].ad+t;
            tree[x].sum=tree[x].sum+t;
            put_light(x,n,t);
            update(x);
        }
        if (ty==2){
            int x=get(),t=get();
            x^=ans;t^=ans;
            access(x);
            splay(x);
            tree[x].ad=tree[x].ad+t-tree[x].val;
            tree[x].sum=tree[x].sum+t-tree[x].val;
            tree[x].val=t;
            update(x);
        }
        if (ty==3)getans();
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章