bzoj 3282: Tree LCT第三题

来源:互联网 发布:安卓最快的数据库 编辑:程序博客网 时间:2024/06/05 09:58

这题一度想精神AC。。
但想了下,人生的第三题LCT就精神AC好像不太好。。
于是很玄学地弄了很久

这题就是LCT的裸题。。
没什么好说的

下面都是废话

然而我TLE了很多发。。
smg。。
完全看不出来好不好。。
最后改用了一下wohenshuai大佬的splay,27s卡过去了
等等,为什么是卡过去。。
我仔细看了下代码。。
几乎是一样好不因为我一开始就是用他的模板
然而时间差了十倍,他是2s,我是27s
QAQ被虐哭了好不
然后我就开始找不同。。
发现我用了读入优化。。
去掉试试TLE了。。
不玩了,这题说白了就是不想我过

#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<stack>using namespace std;const int N=300005;struct qq{    int son[2],fa;    bool rev;    int c;//和    int d;    void bt (int x)    {        son[0]=son[1]=fa=rev=0;        d=c=x;    };}s[N];int n,m;inline 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;}void Push_down (int x){    if (s[x].rev)    {        s[x].rev=false;        s[s[x].son[0]].rev^=1;s[s[x].son[1]].rev^=1;        swap(s[x].son[0],s[x].son[1]);    }}bool Is_root(int x){  if((s[s[x].fa].son[0]!=x)&&(s[s[x].fa].son[1]!=x)) return true;  return false;}void update (int x){    int s1=s[x].son[0],s2=s[x].son[1];    s[x].c=s[s1].c^s[s2].c^s[x].d;}void Rotate(int x){  int y=s[x].fa; int z=s[y].fa;  int a=s[y].son[1]==x; int b=s[z].son[1]==y;  int g=s[x].son[a^1];  if(!Is_root(y)) s[z].son[b]=x; s[x].fa=z;  s[x].son[a^1]=y; s[y].fa=x;  s[y].son[a]=g; if(g) s[g].fa=y;  update(y);}stack<int>S;void Preserve(int x){  while(!Is_root(x)) S.push(x),x=s[x].fa;  S.push(x);  while(!S.empty()) Push_down(S.top()),S.pop();}void Splay(int x){  Preserve(x);  while(!Is_root(x))  {    int y=s[x].fa; int z=s[y].fa;    int a=s[y].son[1]==x; int b=s[z].son[1]==y;    if(Is_root(y)) Rotate(x);    else    {      if(a==b) Rotate(y);      else Rotate(x);      Rotate(x);    }  }update(x);}void Access (int x){    int last=0;    while (x!=0)    {        Splay(x);        s[x].son[1]=last;        update(x);        last=x;        x=s[x].fa;    }}void Make_root (int x){    Access(x);    Splay(x);    s[x].rev^=1;}void Split (int x,int y){    Make_root(x);    Access(y);    Splay(y);}int find_root(int x){    Access(x);    Splay(x);    while (s[x].son[0]!=0)  x=s[x].son[0];    return x;}void Link (int x,int y){    Make_root(y);    s[y].fa=x;}void Cut (int x,int y){    Split(x,y);    if (s[y].son[0]==x)    {        s[x].fa=0;        s[y].son[0]=0;        update(y);    }}int main(){    n=read();m=read();    for (int u=1;u<=n;u++)   s[u].bt(read());    while (m--)    {        int c=read();        if (c==0)        {            int x=read(),y=read();            Split(x,y);printf("%d\n",s[y].c);        }        else if (c==1)        {            int x=read(),y=read();            if (find_root(x)==find_root(y))                continue;            Link (x,y);        }        else if (c==2)        {            int x=read(),y=read();            if(find_root(x)!=find_root(y)) continue;            Split(x,y);            s[y].son[0]=s[s[y].son[0]].fa=0; update(y);        }        else        {            int x=read(),y=read();            Access(x);Splay(x);s[x].d=y;update(x);        }    }    return 0;}
原创粉丝点击