1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <cmath> using namespace std; const int maxn=100005; int n,m,last; vector<int> e[maxn]; int father[maxn],dep[maxn],size[maxn],hson[maxn],top[maxn],seg[maxn],rev[maxn<<2],num[maxn],tot; struct segmenttree{ int l,r,sum,add=-1; #define l(x) tree[x].l #define r(x) tree[x].r #define sum(x) tree[x].sum #define add(x) tree[x].add }; segmenttree tree[maxn<<2]; inline int readint() { int x=0;char ch=getchar(); while(ch<48||ch>57) ch=getchar(); while(ch>=48&&ch<=57) x=x*10+ch-48,ch=getchar(); return x; } inline void readchar(char * input) { int len=0; char ch=getchar(); while(ch!=' ' && ch!='\r' && ch !='\n') { input[len++]=ch; ch=getchar(); } } void addedge(int x,int y) { e[x].push_back(y); e[y].push_back(x); } void dfs1(int u,int f) { size[u]=1; father[u]=f; dep[u]=dep[f]+1; for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(v!=f) { dfs1(v,u); size[u]+=size[v]; if(size[v]>size[hson[u]]) hson[u]=v; } } } void dfs2(int u,int f) { if(hson[u]) { seg[hson[u]]=++tot; top[hson[u]]=top[u]; rev[tot]=hson[u]; dfs2(hson[u],u); } for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(!top[v]) { seg[v]=++tot; rev[tot]=v; top[v]=v; dfs2(v,u); } } } void build(int p,int l,int r) { int mid=(l+r)>>1; l(p)=l;r(p)=r; if(l==r){ sum(p)=num[rev[l]]; return; } build(p<<1,l,mid); build(p<<1|1,mid+1,r); sum(p)=(sum(p<<1)+sum(p<<1|1)); return; } void spread(int p) { if(add(p)>=0) { sum(p<<1)=add(p)*(r(p<<1)-l(p<<1)+1); sum(p<<1|1)=add(p)*(r(p<<1|1)-l(p<<1|1)+1); add(p<<1)=add(p); add(p<<1|1)=add(p); add(p)=-1; } } int query(int p,int l,int r) { if(l>r) swap(l,r); if(l<=l(p) && r>=r(p)) return sum(p); spread(p); int mid=((l(p)+r(p))>>1); int ans=0; if(l<=mid) ans=(ans+query(p<<1,l,r)); if(r>mid) ans=(ans+query(p<<1|1,l,r)); return ans; } void update(int p,int l,int r,int c) { if(l>r) swap(l,r); spread(p); if(l<=l(p) && r>=r(p)) { sum(p)=c*(r(p)-l(p)+1); add(p)=c; return; } int mid=((l(p)+r(p))>>1); if(l<=mid) update(p<<1,l,r,c); if(r>mid) update(p<<1|1,l,r,c); sum(p)=(sum(p<<1)+sum(p<<1|1)); } void crange(int x,int y,int k) { while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) swap(x,y); update(1,seg[top[x]],seg[x],k); x=father[top[x]]; } if(dep[x]>dep[y]) swap(x,y); update(1,seg[x],seg[y],k); } void cson(int x,int k) { update(1,seg[x],seg[x]+size[x]-1,k); } int main() { n=readint(); for(int i=2,a;i<=n;i++){ a=readint(); addedge(i,a+1); } m=readint(); dfs1(1,0); tot=seg[1]=1; top[1]=rev[1]=1; dfs2(1,0); build(1,1,n+1); int last=query(1,1,n); for(int i=1,x;i<=m;i++) { char op[20]; scanf("%s\n",op); if (op[0]=='i') { x=readint(); crange(x+1,1,1); int cur=query(1,1,n); printf("%d\n",abs(last-cur)); last=query(1,1,n); } else if (op[0]=='u') { x=readint(); cson(x+1,0); int cur=query(1,1,n); printf("%d\n",abs(last-cur)); last=cur; } } return 0; }
|