[Luogu3767] Membrane Method

Source: Internet
Author: User
[Luogu3767] Membrane Method

Luogu
Fairy question
Line Segment tree grouping + weighted query set
Treat each operation as a point
First, the structure of this operation is a tree.
You find that each vertex has an impact on its subtree.
We can think of converting it into a range using the dfn sequence and using the line segment tree for grouping.
However, there is also a delete operation, which is equivalent to digging out several cells in a large interval.
You can create a vector record interval for each operation.
Then, the weighted query set is in the sense of mod 5. You can think that the operation is equivalent to an edge with a weight of 1 or 2 from u to v.
When u and v are in the same set, determine whether the conditions are met. Otherwise, even edges

#define pb push_back#define ls x<<1,l,mid#define rs x<<1|1,mid+1,r#include<bits/stdc++.h>using namespace std;const int _=1e5+5;int re(){    int x=0,w=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();    return x*w;}int n,m,ts,top,dis;int fa[_],del[_],sz[_],dfn[_],par[_],siz[_],d[_];bool ans[_];struct node{int u,v;}st[_];struct edge{int u,v,w;}e[_];vector<int>son[_],s[_];vector<edge>t[_<<2];void dfs(int u){    sz[u]=1;if(u)dfn[u]=++ts;    if(del[u])s[del[u]].pb(u);    for(int i=0,j=son[u].size();i<j;i++){        int v=son[u][i];        dfs(v);sz[u]+=sz[v];    }}void add(int&x,int y){x=(x+y)%5;}int find(int x){    add(dis,d[x]);    if(x==par[x])return x;    return find(par[x]);}void upd(int x,int l,int r,int ql,int qr,edge E){    if(ql<=l&&r<=qr){t[x].pb(E);return;}    int mid=(l+r)>>1;if(ql<=mid)upd(ls,ql,qr,E);    if(qr>mid)upd(rs,ql,qr,E);}void solve(int x,int l,int r,bool ok){    int pre=top;    for(int i=0,j=t[x].size();i<j;i++){        int u=t[x][i].u,v=t[x][i].v,w=t[x][i].w;        dis=0;int fu=find(u),du=dis;        dis=0;int fv=find(v),dv=dis;        if(fu==fv&&(du-dv+5)%5!=w)ok=0;        if(fu^fv){            if(siz[fu]>siz[fv]){swap(du,dv);swap(u,v);swap(fu,fv);w=-w;}            siz[fv]+=siz[fu];par[fu]=fv;            d[fu]=(w+dv-du+10)%5;st[++top]=(node){fu,fv};        }    }    if(l==r)ans[l]=ok;    else{int mid=(l+r)>>1;solve(ls,ok);solve(rs,ok);}    while(top^pre){        int u=st[top].u,v=st[top].v;top--;        siz[v]-=siz[u];par[u]=u;d[u]=0;    }}int main(){    n=re(),m=re();    int op,u,v;    for(int i=1;i<=m;i++){        son[fa[i]=re()].pb(i);op=re();        if(op==3)del[i]=re();        else{u=re(),v=re();e[i]=(edge){u,v,op};}    }    dfs(0);    for(int i=1;i<=m;i++){        if(del[i])continue;        int k=s[i].size(),lst=dfn[i];        for(int j=0;j<k;j++){            int u=s[i][j];            if(lst<dfn[u])upd(1,1,m,lst,dfn[u]-1,e[i]);            lst=dfn[u]+sz[u];        }        if(lst<dfn[i]+sz[i])upd(1,1,m,lst,dfn[i]+sz[i]-1,e[i]);    }    for(int i=1;i<=n;i++)par[i]=i,siz[i]=1;    solve(1,1,m,1);    for(int i=1;i<=m;i++)puts(ans[dfn[i]]?"excited":"naive");    return 0;}

[Luogu3767] Membrane Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.