TLE求调QAQ
查看原帖
TLE求调QAQ
147401
koishi_offical楼主2021/12/14 13:57
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node 
  {
      int son[2];
      int f;
      int val;
      int siz;
      int sum;
      bool rev;
  }tr[N];
bool nroot(int x)
  {
      return tr[tr[x].f].son[0]==x||tr[tr[x].f].son[1]==x;
  }
void pushrev(int x)
  {
      tr[x].rev^=1;
      swap(tr[x].son[0],tr[x].son[1]);
  }
void pushup(int x) 
  {
      tr[x].siz=tr[tr[x].son[0]].siz+tr[tr[x].son[1]].siz+1;
      tr[x].sum=tr[tr[x].son[0]].sum^tr[tr[x].son[1]].sum^tr[x].val;
  }
void pushdown(int x)
  {
      if(!tr[x].rev) return;
      if(tr[x].son[0]) pushrev(tr[x].son[0]);
      if(tr[x].son[1]) pushrev(tr[x].son[1]);
      tr[x].rev=0;
  }
void rotate(int x)
  {
      int f=tr[x].f,g=tr[f].f;
      int w=tr[f].son[1]==x;
      if(nroot(f)) tr[g].son[tr[g].son[1]==f]=x;
      tr[x].f=g;
      tr[f].son[w]=tr[x].son[w^1];
      tr[f].f=x;
      tr[tr[x].son[w^1]].f=f;
      tr[x].son[w^1]=f;
      pushup(f),pushup(x);
  }
int stk[N];
void splay(int x)
  {
      int top=0;
      int r=x;
      stk[++top]=r;
      while(nroot(r)) stk[++top]=r=tr[r].f;
      while(top) pushdown(stk[top--]);
      while(nroot(x))
        {
            int y=tr[x].f,z=tr[y].f;
            if(nroot(y))
              {
                  if((tr[z].son[1]==y)^(tr[y].son[1]==x)) rotate(x);
                  else rotate(y);
                  rotate(x);
              }
        }
  }
void access(int x)
  {
      int z=x;
      for(int y=0;x;y=x,x=tr[x].f)
        {
            splay(x);
            tr[x].son[1]=y;
            pushup(x);
        }
      splay(z);
  }
void makeroot(int x)
  {
     access(x);
     pushrev(x);
  }
int findroot(int x)
  {
      access(x);
      while(tr[x].son[0]) pushdown(x),x=tr[x].son[0];
      splay(x);
      return x;
  }
 void split(int x,int y)
   {
       makeroot(x);
       access(y);
       //splay(y);
   }
 void link(int x,int y)
   {
       makeroot(x);
       if(findroot(y)!=x) tr[x].f=y;
   }
 void cut(int x,int y)
   {
       makeroot(x);
       if(findroot(y)==x&&tr[y].f==x&&!tr[y].son[0]) 
         {
             tr[x].son[1]=tr[y].f=0;
             pushup(x);
         }
   }
int n,m;
int main() {
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>tr[i].val;
    while(m--)
      {
          int p,x,y;
          cin>>p>>x>>y;
          if(p==0)
            {
                split(x,y);
                cout<<tr[y].sum<<endl;
            }
          if(p==1)
            {
                link(x,y);
            }
          if(p==2)
            {
                cut(x,y);
            }
          if(p==3)
            {
                splay(x);
                tr[x].val=y;
                pushup(x);
            }
      }
}
2021/12/14 13:57
加载中...