记录编号 219943 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOI 2015]软件包管理器 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 Pascal 运行时间 8.048 s
提交时间 2016-01-16 21:05:50 内存使用 16.75 MiB
显示代码纯文本
  1. uses math;
  2. type
  3. link=^way;
  4. way=record
  5. son:longint;
  6. next:link; //0 means it's uninstalled,1 means it's installed.
  7. end;
  8. rec=record
  9. l,r,v,y,len:longint;
  10. end;
  11. var
  12. n,q,i,j,x,y,count1,c,ans,l,r:longint;
  13. fa,f,count,hash,edge,lian:array[0..100000]of longint;
  14. w,pro:array[0..100000]of link;
  15. xd:array[0..800000]of rec;
  16. s:string;
  17. a,b:link;
  18.  
  19. procedure readl;
  20. var
  21. ch:char;
  22. begin
  23. s:='';
  24. repeat
  25. read(ch);
  26. if ch<>' ' then s:=s+ch;
  27. until ch=' ';
  28. readln(x);x:=hash[x];
  29. end;
  30.  
  31. procedure mtree(x:longint);
  32. var
  33. i:link;
  34. begin
  35. count[x]:=1;i:=w[x];
  36. while i<>nil do
  37. begin
  38. mtree(i^.son);
  39. inc(count[x],count[i^.son]);
  40. i:=i^.next;
  41. end;
  42. end;
  43.  
  44. procedure process(x:longint);
  45. var
  46. i:link;
  47. begin
  48. inc(count1);hash[x]:=count1;
  49. i:=w[x];
  50. while i<>nil do
  51. begin
  52. process(i^.son);i:=i^.next;
  53. end;
  54. end;
  55.  
  56. procedure exchange;
  57. var
  58. i:longint;
  59. begin
  60. for i:=0 to n do
  61. f[hash[i]]:=count[i];
  62. for i:=0 to n do
  63. count[i]:=f[i];
  64. for i:=0 to n do
  65. f[hash[i]]:=hash[fa[i]];
  66. for i:=0 to n do
  67. fa[i]:=f[i];
  68. end;
  69.  
  70. procedure makeedge;
  71. var
  72. i,j,count:longint;
  73. begin
  74. i:=0;count:=1;edge[0]:=1;lian[1]:=0;
  75. for j:=1 to n do
  76. if fa[j]=i then
  77. begin
  78. edge[j]:=count;i:=j;
  79. end
  80. else
  81. begin
  82. inc(count);edge[j]:=count;i:=j;lian[count]:=j;
  83. end;
  84. edge[n]:=count;
  85. end;
  86.  
  87. procedure makexd(x:longint);
  88. var
  89. m:longint;
  90. begin
  91. with xd[x] do
  92. begin
  93. len:=xd[x].r-xd[x].l+1;
  94. if l=r then exit;
  95. m:=(l+r) div 2;
  96. xd[x*2].l:=l;xd[x*2].r:=m;
  97. xd[x*2+1].l:=m+1;xd[x*2+1].r:=r;
  98. makexd(x*2);makexd(x*2+1);
  99. end;
  100. end;
  101.  
  102. function qj(a,b,c,d:longint):longint;
  103. begin
  104. exit(min(b,d)-max(a,c)+1);
  105. end;
  106.  
  107. function sum(x,a:longint):longint;
  108. begin
  109. if (xd[x].l>r)or(xd[x].r<l) then exit(0);
  110. case xd[x].y of
  111. 1:xd[x].v:=xd[x].len;
  112. -1:xd[x].v:=0;
  113. end;
  114. if xd[x].y<>0 then
  115. begin
  116. xd[x*2].y:=xd[x].y;xd[x*2+1].y:=xd[x].y;xd[x].y:=0;
  117. end;
  118. if (xd[x].l>=l)and(xd[x].r<=r) then
  119. case a of
  120. 1:exit(xd[x].len-xd[x].v);
  121. -1:exit(xd[x].v);
  122. end;
  123. sum:=sum(x*2,a)+sum(x*2+1,a);
  124. end;
  125.  
  126. procedure add(x,a:longint);
  127. var
  128. i,d:longint;
  129. bool:boolean;
  130. begin
  131. if (xd[x].l>r)or(xd[x].r<l) then exit;
  132. bool:=false;
  133. case xd[x].y of
  134. 1:xd[x].v:=xd[x].len;
  135. -1:xd[x].v:=0;
  136. end;
  137. if (xd[x].l>=l)and(xd[x].r<=r) then
  138. begin
  139. xd[x].y:=a;
  140. case a of
  141. 1:d:=xd[x].len-xd[x].v;
  142. -1:d:=-xd[x].v;
  143. end;
  144. i:=x;
  145. repeat
  146. inc(xd[i].v,d);i:=i div 2;
  147. until i=0;
  148. bool:=true;
  149. end;
  150. if xd[x].y<>0 then
  151. begin
  152. xd[x*2].y:=xd[x].y;xd[x*2+1].y:=xd[x].y;xd[x].y:=0;
  153. end;
  154. if bool then exit;
  155. add(x*2,a);add(x*2+1,a);
  156. end;
  157.  
  158. procedure install(x:longint);
  159. var
  160. i,j:longint;
  161. begin
  162. i:=x;
  163. repeat
  164. j:=lian[edge[i]];l:=j;r:=i;
  165. ans:=ans+sum(1,1);add(1,1);
  166. i:=fa[j];
  167. until j=0;
  168. end;
  169.  
  170. procedure uninstall(x:longint);
  171. begin
  172. l:=x;r:=x+count[x]-1;
  173. ans:=sum(1,-1);
  174. add(1,-1);
  175. end;
  176.  
  177. begin
  178. assign(input,'manager.in');
  179. reset(input);
  180. assign(output,'manager.out');
  181. rewrite(output);
  182. read(n);
  183. dec(n);
  184. for i:=1 to n do
  185. read(fa[i]);
  186.  
  187. for i:=0 to n do
  188. begin
  189. new(w[i]);pro[i]:=w[i];w[i]^.next:=nil;
  190. end;
  191. for i:=1 to n do
  192. begin
  193. new(pro[fa[i]]^.next);
  194. pro[fa[i]]:=pro[fa[i]]^.next;
  195. pro[fa[i]]^.son:=i;
  196. pro[fa[i]]^.next:=nil;
  197. end;
  198. for i:=0 to n do w[i]:=w[i]^.next;
  199.  
  200. mtree(0);
  201.  
  202. for i:=0 to n do
  203. begin
  204. b:=w[i];
  205. if b=nil then continue;
  206. repeat
  207. b:=b^.next;
  208. if b=nil then break;
  209. if count[w[i]^.son]<count[b^.son] then
  210. begin
  211. y:=b^.son;b^.son:=w[i]^.son;w[i]^.son:=y;
  212. end;
  213. until 1=2;
  214. end;
  215.  
  216. count1:=-1;
  217. process(0);
  218. exchange;
  219. makeedge;
  220. xd[1].l:=0;xd[1].r:=n;
  221. makexd(1);
  222.  
  223. readln(q);
  224. for q:=1 to q do
  225. begin
  226. readl;
  227. ans:=0;
  228. case s[1] of
  229. 'i':install(x);
  230. 'u':uninstall(x);
  231. end;
  232. writeln(ans);
  233. end;
  234. close(input);
  235. close(output);
  236. end.