BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)

蛋蛋用链剖A的,我写的LCT

3282: Tree
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 1241 Solved: 542
[Submit][Status][Discuss]

Description
给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。
0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。
1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。
2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。
3:后接两个整数(x,y),代表将点X上的权值变成Y。

Input
第1行两个整数,分别为N和M,代表点数和操作数。
第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。
第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

Output
对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input
3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output
3
1

HINT
1<=N,M<=300000

Source
动态树

Link-Cut-Tree模版啊。。话不多说直接干

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

#define N 300100
int fa[N],sum[N],tmp[N],val[N],son[N][2];
bool rev[N];
int n,m;

bool isroot(int x)
{
    return !fa[x]||son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}

void rev1(int x)
{
    if (!x) return;
    swap(son[x][0],son[x][1]);
    rev[x]^=1;
}

void pb(int x)
{
    if (rev[x])
        rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;
}

void up(int x)
{
    sum[x]=sum[son[x][0]]^sum[son[x][1]]^val[x];
} 

void rotate(int x)
{
    int y=fa[x],w=son[y][1]==x;
    son[y][w]=son[x][w^1];
    if(son[x][w^1]) fa[son[x][w^1]]=y;
    if(fa[y])
        {
            int z=fa[y];
            if(son[z][0]==y) son[z][0]=x;
                else if(son[z][1]==y) son[z][1]=x;
        }
    fa[x]=fa[y]; fa[y]=x;
    son[x][w^1]=y; up(y);
}

void splay(int x)
{
    int s=1,i=x,y;tmp[1]=i;
    while (!isroot(i)) tmp[++s]=i=fa[i];
    while (s) pb(tmp[s--]);
    while (!isroot(x))
        {
            y=fa[x];
            if(!isroot(y))
                {
                    if((son[fa[y]][0]==y)^(son[y][0]==x))
                    rotate(x);  else  rotate(y);
                }
            rotate(x);
        }
    up(x);
}

void access(int x)
{
    for (int y=0; x; y=x,x=fa[x])
        splay(x),son[x][1]=y,up(x); 
}

void makeroot(int x)
{
    access(x);splay(x);rev1(x);
}

void link(int x,int y)
{
    makeroot(x); fa[x]=y; access(x);
}

void cutf(int x)
{
    access(x);splay(x);fa[son[x][0]]=0; son[x][0]=0; up(x);
}

void cut(int x,int y)
{
    makeroot(x);cutf(y);
}

int find(int x)
{
    access(x);splay(x);
    int y=x;
    while (son[y][0]) y=son[y][0];
    return y;
}

int main()
{
    n=read(),m=read();
    for (int i=1; i<=n; i++)
        {
            int x=read();
            sum[i]=x; val[i]=x;
        }
    for (int i=1; i<=m; i++)
        {
            int com=read();
            int x=read(),y=read();
            switch (com)
                {
                    case 0: makeroot(x); access(y); splay(y); printf("%d\n",sum[y]); break;
                    case 1: if (find(x)!=find(y)) link(x,y); break;
                    case 2: if (find(x)==find(y)) cut(x,y); break;
                    case 3: access(x); splay(x); val[x]=y; up(x); break;
                }
        }
    return 0;
}
posted @ 2016-03-11 23:29  DaD3zZ  阅读(148)  评论(0编辑  收藏  举报