关于LCT时间优化
查看原帖
关于LCT时间优化
256970
xie_lzh楼主2023/7/1 10:57

是我写的太不优了吗,被卡T了

#include<bits/stdc++.h>
using namespace std;
int read()
{
    int r=0,f=1;
    char c=getchar();
    while(!isdigit(c))
    {
        if(c=='-') f=0;
        c=getchar();
    }
    while(isdigit(c))
    {
        r=(r<<1)+(r<<3)+c-48;
        c=getchar();
    }
    return f?r:-r;
}
const int N=2e5+5;
int n,m;
struct node
{
    int ch[2],val,fa,r,sum,laz;
}pos[N];
int gett(int x){return (x==pos[pos[x].fa].ch[1]);}
void pushup(int x)
{
    pos[x].sum=(pos[pos[x].ch[0]].sum^pos[pos[x].ch[1]].sum^pos[x].val);
}
void swapp(int x)
{
    swap(pos[x].ch[0],pos[x].ch[1]);
    pos[x].laz^=1;
}
void pushdown(int x)
{
    if(pos[x].laz)
    {
        if(pos[x].ch[0])swapp(pos[x].ch[0]);
        if(pos[x].ch[1])swapp(pos[x].ch[1]);
        pos[x].laz=0;
    }
}
bool isroot(int x)
{
    return (x!=pos[pos[x].fa].ch[0]&&x!=pos[pos[x].fa].ch[1]);
}
void rot(int x)
{
    int y=pos[x].fa; int z=pos[y].fa;
    int k1=gett(x),k2=gett(y);
    if(!isroot(y))pos[z].ch[k2]=x; pos[x].fa=z;
    pos[y].ch[k1]=pos[x].ch[(k1^1)],pos[pos[x].ch[(k1^1)]].fa=y;

    pos[x].ch[(k1^1)]=y; pos[y].fa=x;
    pushup(y); pushup(x);
}
void update(int x)
{
    if(!isroot(x))update(pos[x].fa);
    pushdown(x);
}
void splay(int x)
{
    update(x);
    while(!isroot(x))
    {
        int y=pos[x].fa;
        if(!isroot(y))(gett(x)^gett(y))?rot(y):rot(x);
        rot(x); 
    }
    pushup(x);
}
void access(int x)
{
    for(int y=0;x;y=x,x=pos[x].fa)
    {
        splay(x);
        pos[x].ch[1]=y;
        pushup(x);
    }
}
void makeroot(int x)
{
    access(x);
    splay(x);
    swapp(x);
    // change(x);
}
int findroot(int x)
{
    access(x);
    splay(x);
    while(pos[x].ch[0]) x=pos[x].ch[0];
    return x;
}
void Link(int x,int y)
{
    makeroot(x);
    if(findroot(y)==x) return ;
    pos[x].fa=y;
}
void Cut(int x,int y)
{
    makeroot(x);
    if(findroot(y)!=x) return ;
    makeroot(x);
    if(pos[y].fa==x&&pos[y].ch[0]==0)
    {
        pos[x].ch[1]=0;
        pos[y].fa=0;
        pushup(x);
    }
}
void split(int x,int y)
{
    makeroot(x);
    access(y);
    // splay(y);
}
signed main()
{
    n=read(); m=read();
    for(int i=1;i<=n;i++)
    {
        pos[i].val=pos[i].sum=read();
    }
    while(m--)
    {
        int op=read(),x=read(),y=read();
        if(op==0) split(x,y),printf("%d\n",pos[y].sum);
        if(op==1) Link(x,y);
        if(op==2) Cut(x,y);
        if(op==3) splay(x),pos[x].val=y,pushup(x);
    }
}
2023/7/1 10:57
加载中...