bzoj 4551: [Tjoi2016&Heoi2016]树
一开始本蒟蒻打眼一看,直接上链剖,维护深度最大不就行了,,,,,,霹雳巴拉打完,(搞了半天还忘了size[x]=1.....各种蛋疼的调)
然后翻了一下众神犇的题解,发现我真的是个蒟蒻。。。
神犇们大概是这么做的:把操作离线,倒叙处理,把打标记变成删标记,然后把一开始的未标记点往上连,也搞成一些一块一块的东西(没错,又是用并查集维护)
然后如果标记删除就把这个点连到父亲点上(并查集中)(从神犇博客中发现,和疯狂的馒头相似)(其实是做了这个题才做的**馒头,然而还是不会,,,)
(神犇果然是神犇2333)
1 #include<bits/stdc++.h> 2 #define N 100005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define ls c[x][0] 6 #define rs c[x][1] 7 using namespace std; 8 inline int ra() 9 { 10 int x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 int n,m,cnt; 16 int head[N],c[N],a[N],f[N],fa[N],ans[N]; 17 char opt[N]; 18 struct edge{int next,to;}e[N*2]; 19 void insert(int x, int y){e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt;} 20 int find(int x){return f[x]==x?x:f[x]=find(f[x]);} 21 void dfs(int x) 22 { 23 f[x]=c[x]?x:fa[x]; 24 for (int i=head[x];i;i=e[i].next) 25 { 26 if (e[i].to==fa[x]) continue; 27 fa[e[i].to]=x; dfs(e[i].to); 28 } 29 } 30 int main() 31 { 32 n=ra(); m=ra(); 33 c[1]=1; 34 for (int i=1; i<n; i++) 35 { 36 int x=ra(),y=ra(); 37 insert(x,y); insert(y,x); 38 } 39 for (int i=1; i<=m; i++) 40 { 41 char s[2]; scanf("%s",s); 42 if (s[0]=='C') opt[i]='C'; else opt[i]='A'; 43 a[i]=ra(); 44 if (opt[i]=='C') c[a[i]]++; 45 } 46 dfs(1); 47 for (int i=m; i>=1; i--) 48 { 49 if (opt[i]=='C'){c[a[i]]--; if (!c[a[i]]) f[a[i]]=fa[a[i]];} 50 else ans[i]=find(a[i]); 51 } 52 for (int i=1; i<=m; i++) 53 if (ans[i]) printf("%d\n",ans[i]); 54 return 0; 55 }
1 #include<bits/stdc++.h> 2 #define N 100005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define ls c[x][0] 6 #define rs c[x][1] 7 using namespace std; 8 inline int ra() 9 { 10 int x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 struct node{int to,next;}e[N<<1]; 16 struct seg{int mx,l,r;}t[N<<4]; 17 int head[N],cnt,deep[N],fa[N][20],size[N],dfn[N],belong[N]; 18 bool vis[N]; 19 int n,sz; 20 void insert(int x, int y) {e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt;} 21 void dfs1(int x) 22 { 23 size[x]=1;//fuck... 24 for (int i=1; i<=16; i++) 25 if (deep[x]>(1<<i)) 26 fa[x][i]=fa[fa[x][i-1]][i-1]; 27 else break; 28 for (int i=head[x];i;i=e[i].next) 29 { 30 if (e[i].to==fa[x][0]) continue; 31 deep[e[i].to]=deep[x]+1; 32 fa[e[i].to][0]=x; 33 dfs1(e[i].to); 34 size[x]+=size[e[i].to]; 35 } 36 } 37 void dfs2(int x, int chain) 38 { 39 belong[x]=chain; dfn[x]=++sz; int k=n+1; 40 for (int i=head[x];i;i=e[i].next) 41 if (size[k]<size[e[i].to] && e[i].to!=fa[x][0]) 42 k=e[i].to; 43 if (k!=n+1) dfs2(k,chain); 44 for (int i=head[x];i;i=e[i].next) 45 if (e[i].to!=fa[x][0] && e[i].to!=k) 46 dfs2(e[i].to,e[i].to); 47 } 48 void build(int k, int l, int r) 49 { 50 t[k].l=l; t[k].r=r; 51 if (l==r) return; 52 int mid=l+r>>1; 53 build(k<<1,l,mid); build(k<<1|1,mid+1,r); 54 } 55 void update(int k) 56 { 57 t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx); 58 } 59 void change(int k, int x, int val) 60 { 61 int l=t[k].l,r=t[k].r; 62 if (l==r) 63 { 64 t[k].mx=val; 65 return; 66 } 67 int mid=l+r>>1; 68 if (x<=mid) change(k<<1,x,val); 69 else change(k<<1|1,x,val); 70 update(k); 71 } 72 void solve_change(int x) 73 { 74 if (vis[x]) return; 75 int val=deep[x]; 76 change(1,dfn[x],val); 77 vis[x]=1; 78 } 79 int ask(int k, int x, int y) 80 { 81 int l=t[k].l,r=t[k].r; 82 if (l==x && y==r) return t[k].mx; 83 int mid=l+r>>1; 84 if (y<=mid) return ask(k<<1,x,y); 85 else if (x>mid) return ask(k<<1|1,x,y); 86 else return max(ask(k<<1,x,mid),ask(k<<1|1,mid+1,y)); 87 } 88 int get(int x, int t) 89 { 90 for (int i=0; i<=16,t>=(1<<i); i++) 91 if (t&(1<<i)) x=fa[x][i]; 92 return x; 93 } 94 void solve_ask(int x) 95 { 96 int pos=x; 97 if (vis[x]) 98 { 99 printf("%d\n",x); 100 return; 101 } 102 int ans=0; 103 while (belong[x]!=1) 104 { 105 ans=max(ans,ask(1,dfn[belong[x]],dfn[x])); 106 x=fa[belong[x]][0]; 107 } 108 ans=max(ans,ask(1,1,dfn[x])); 109 if (ans==0) printf("1\n"); 110 else printf("%d\n",get(pos,deep[pos]-ans)); 111 } 112 int main() 113 { 114 n=ra(); int Q=ra(); 115 for (int i=1; i<n; i++) 116 { 117 int x=ra(),y=ra(); 118 insert(x,y); insert(y,x); 119 } 120 deep[1]=1; dfs1(1); dfs2(1,1); build(1,1,n); solve_change(1); 121 while (Q--) 122 { 123 char s[2]; scanf("%s",s); int x=ra(); 124 if (s[0]=='C') solve_change(x); 125 if (s[0]=='Q') solve_ask(x); 126 } 127 return 0; 128 }