题目链接

题面:

题解:
人丑常数大。。。一个log的LCT,比两个log的线段树分治还要慢。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#include<list>
#include<ctime>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x) (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=1e9+7;
const double eps=1e-1;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=550100;
const int maxp=800100;
const int maxm=1000100;
const int up=200000;

struct tree
{
    int son[2];
    int fa,minn,si,rev;
}t[maxn];

int st[maxn];
int deltime[maxn],isdel[maxn];
int n,m;
int idin[maxn];

struct node
{
    int x,y;
    int begintime,endtime;
    int id;
    node(){}
    node(int a,int b,int c,int d,int e)
    {
        x=a,y=b,begintime=c,endtime=d,id=e;
    }
}in[maxn],out[maxn];

bool cmpin(const node &a,const node &b)
{
    return a.begintime<b.begintime;
}
bool cmpout(const node &a,const node &b)
{
    return a.endtime<b.endtime;
}


struct nn
{
    int x,y,id;
    nn(){}
    nn(int a,int b,int c)
    {
        x=a,y=b,id=c;
    }
};

vector<nn>vc[maxn];
int ans[maxn];
map<pair<int,int>,int>mp;


void pushup(int x)
{
    t[x].minn=x;
    t[x].si=x>n;
    if(t[x].son[0])
    {
        t[x].si+=t[t[x].son[0]].si;
        t[x].minn=deltime[t[x].minn]<deltime[t[t[x].son[0]].minn]?t[x].minn:t[t[x].son[0]].minn;
    }
    if(t[x].son[1])
    {
        t[x].si+=t[t[x].son[1]].si;
        t[x].minn=deltime[t[x].minn]<deltime[t[t[x].son[1]].minn]?t[x].minn:t[t[x].son[1]].minn;
    }
}

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

void pushdown(int x)
{
    if(!x) return ;
    if(t[x].rev)
    {
        _reverse(t[x].son[0]);
        _reverse(t[x].son[1]);
        t[x].rev=0;
    }
}

bool notroot(int x)
{
    return t[t[x].fa].son[0]==x||t[t[x].fa].son[1]==x;
}

int get_son(int x)
{
    return x==t[t[x].fa].son[1];
}

void rot(int x)
{
    int y=t[x].fa,z=t[y].fa;
    int k=get_son(x),w=t[x].son[k^1];
    t[y].son[k]=w,t[w].fa=y;

    if(notroot(y))t[z].son[get_son(y)]=x;
    t[x].fa=z;

    t[x].son[k^1]=y,t[y].fa=x;
    pushup(y),pushup(x);
}

void splay(int x)
{
    int top=0;
    st[++top]=x;
    for(int pos=x;notroot(pos);pos=t[pos].fa) st[++top]=t[pos].fa;
    while(top) pushdown(st[top--]);

    while(notroot(x))
    {
        int y=t[x].fa;
        int z=t[y].fa;
        if(notroot(y))
            if(get_son(x)==get_son(y)) rot(y);
            else rot(x);
        rot(x);
    }
}

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

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

int findroot(int x)
{
    access(x);
    splay(x);
    while(t[x].son[0]) pushdown(x),x=t[x].son[0];
    splay(x);
    return x;
}

void split(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
}

bool link(int x,int y)
{
    makeroot(x);
    if(findroot(y)==x) return false;
    t[x].fa=y;
    return true;
}

bool cut(int x,int y)
{
    makeroot(x);
    if(findroot(y)!=x||t[y].fa!=x||t[y].son[0]) return false;
    t[y].fa=t[x].son[1]=0;
    pushup(x);
    return true;
}

bool connected(int x,int y)
{
    x=findroot(x);
    y=findroot(y);
    return x==y;
}

//拉出x-y 链,其中y现在为根节点且只有左子树
void ***(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
}

void _insert(int p)
{
    deltime[in[p].id]=in[p].endtime;
    t[in[p].id].minn=in[p].id;
    int x=in[p].x,y=in[p].y,id=in[p].id;
    if(connected(x,y))
    {
        ***(x,y);
        if(deltime[t[y].minn]>deltime[id])
            isdel[id]=1;
        else
        {
            isdel[t[y].minn]=1;

            int de=t[y].minn;
            cut(in[idin[de]].x,de);
            cut(in[idin[de]].y,de);
            link(x,id);
            link(y,id);
        }
    }
    else
    {
        link(in[p].x,in[p].id);
        link(in[p].y,in[p].id);
    }
}

void del(int p)
{
    if(isdel[out[p].id])
        isdel[out[p].id]=0;
    else
    {
        cut(out[p].x,out[p].id);
        cut(out[p].y,out[p].id);
    }
}

int main(void)
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        deltime[i]=inf;

    int op,x,y;
    int tot=0,cnt=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&op,&x,&y);
        if(x>y) swap(x,y);
        if(op==0)
        {
            mp[pr(x,y)]=i;
        }
        else if(op==1)
        {
            int pos=mp[pr(x,y)];
            mp.erase(pr(x,y));
            ++cnt;
            in[cnt]=node(x,y,pos,i,cnt+n);
            out[cnt]=in[cnt];
        }
        else if(op==2)
        {
            vc[i].pb(nn(x,y,++tot));
        }
    }
    for(auto p : mp)
    {
        ++cnt;
        in[cnt]=node(p.first.first,p.first.second,p.second,m+1,cnt+n);
        out[cnt]=in[cnt];
    }
    sort(in+1,in+cnt+1,cmpin);
    sort(out+1,out+cnt+1,cmpout);
    for(int i=1;i<=cnt;i++)
        idin[in[i].id]=i;

    int cntin=1,cntout=1;
    for(int i=1;i<=m;i++)
    {
        if(vc[i].size()==0) continue;
        while(cntin<=cnt&&in[cntin].begintime<=i)
            _insert(cntin++);
        while(cntout<=cnt&&out[cntout].endtime<=i)
            del(cntout++);
        for(int j=0;j<vc[i].size();j++)
        {
            if(connected(vc[i][j].x,vc[i][j].y))
                ans[vc[i][j].id]=1;
        }
    }
    for(int i=1;i<=tot;i++)
    {
        if(ans[i]) printf("Y\n");
        else printf("N\n");
    }
    return 0;
}