记录编号 413454 评测结果 AAAAAAAAAA
题目名称 [HAOI 2015]树上操作 最终得分 100
用户昵称 GravatarBaDBoY 是否通过 通过
代码语言 C++ 运行时间 0.802 s
提交时间 2017-06-11 16:35:28 内存使用 15.58 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn=100010;
  6. typedef long long ll;
  7. #define lson root<<1,l,mid
  8. #define rson root<<1|1,mid+1,r
  9. inline int read(){
  10. int s=0,f=1; char ch=getchar();
  11. while(ch>'9'||ch<'0'){if(ch=='-')f=-1; ch=getchar();}
  12. while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0'; ch=getchar();}
  13. return s*f;
  14. }
  15. int n,m,a[maxn],r[maxn],tot;
  16. struct oo{
  17. int fr,to,next;
  18. }c[2*maxn];
  19. inline void add(int x,int y){
  20. c[tot].fr=x;
  21. c[tot].to=y;
  22. c[tot].next=r[x];
  23. r[x]=tot++;
  24. }
  25. ll size[maxn],pos[maxn],deep[maxn],son[maxn],fa[maxn];
  26. inline void dfs1(int x){
  27. size[x]=1; son[x]=0;
  28. for(int i=r[x];i!=-1;i=c[i].next)
  29. if(c[i].to!=fa[x]){
  30. fa[c[i].to]=x;
  31. deep[c[i].to]=deep[x]+1;
  32. dfs1(c[i].to);
  33. size[x]+=size[c[i].to];
  34. if(size[son[x]]<size[c[i].to])
  35. son[x]=c[i].to;
  36. }
  37. }
  38. ll top[maxn],cnt,fir[maxn],la[maxn];
  39. inline void dfs2(int u,int v){
  40. top[u]=v;
  41. fir[u]=la[u]=++cnt;
  42. pos[cnt]=u;
  43. if(son[u]) dfs2(son[u],v);
  44. for(int i=r[u];i!=-1;i=c[i].next)
  45. if(c[i].to!=fa[u]&&c[i].to!=son[u])
  46. dfs2(c[i].to,c[i].to);
  47. la[u]=cnt;
  48. }
  49. long long sum[4*maxn];
  50. inline void build(int root,int l,int r){
  51. if(l==r){
  52. sum[root]=a[pos[l]];
  53. return ;
  54. }
  55. int mid=(l+r)>>1;
  56. build(lson); build(rson);
  57. sum[root]=sum[root<<1]+sum[root<<1|1];
  58. }
  59. long long addd[4*maxn];
  60. inline void pushdown(int rt,long long len){
  61. if(addd[rt]){
  62. addd[rt<<1]+=addd[rt];
  63. addd[rt<<1|1]+=addd[rt];
  64. sum[rt<<1]+=addd[rt]*(len-(len>>1));
  65. sum[rt<<1|1]+=addd[rt]*(len>>1);
  66. addd[rt]=0;
  67. }
  68. }
  69. inline long long query(int L,int R,int root,int l,int r){
  70. if(L<=l&&R>=r) return sum[root];
  71. pushdown(root,r-l+1);
  72. int mid=(l+r)>>1;
  73. long long ans=0;
  74. if(L<=mid) ans+=query(L,R,lson);
  75. if(R>mid) ans+=query(L,R,rson);
  76. return ans;
  77. }
  78. inline ll Query(int x,int y){
  79. ll fx=top[x],fy=top[y],ans=0;
  80. while(fx^fy){
  81. if(deep[fx]<deep[fy]){
  82. swap(fx,fy);
  83. swap(x,y);
  84. }
  85. ans+=query(fir[fx],fir[x],1,1,n);
  86. x=fa[fx];
  87. fx=top[x];
  88. }
  89. if(deep[x]>deep[y]) swap(x,y);
  90. ans+=query(fir[x],fir[y],1,1,n);
  91. return ans;
  92. }
  93. inline void update(int L,int R,ll val,int root,int l,int r){
  94. if(L<=l&&r<=R){
  95. addd[root]+=val;
  96. sum[root]+=(ll)(r-l+1)*val;
  97. return ;
  98. }
  99. pushdown(root,r-l+1);
  100. int mid=(r+l)>>1;
  101. if(L<=mid) update(L,R,val,lson);
  102. if(R>mid) update(L,R,val,rson);
  103. sum[root]=sum[root<<1]+sum[root<<1|1];
  104. }
  105. void out(){
  106. printf("\n");
  107. for(int i=1;i<=n;i++)
  108. printf("%d ",query(fir[i],fir[i],1,1,n));
  109. printf("\n");
  110. }
  111. int main(){
  112. freopen("haoi2015_t2.in","r",stdin);
  113. freopen("haoi2015_t2.out","w",stdout);
  114. n=read(),m=read();
  115. r[0]=-1;
  116. for(int i=1;i<=n;i++){
  117. a[i]=read();
  118. r[i]=-1;
  119. }
  120. for(int i=1;i<n;i++){
  121. int x=read(),y=read();
  122. add(x,y); add(y,x);
  123. }
  124. dfs1(1); dfs2(1,1); build(1,1,n);
  125. for(int i=1;i<=m;i++){
  126. int pan=read();
  127. if(pan==1){
  128. int x=read(),y=read();
  129. update(fir[x],fir[x],y,1,1,n);
  130. }
  131. if(pan==2){
  132. int x=read(),y=read();
  133. update(fir[x],la[x],y,1,1,n);
  134. }
  135. if(pan==3){
  136. //out();
  137. int x=read();
  138. printf("%lld\n",Query(1,x));
  139. }
  140. }
  141. //while(1);
  142. return 0;
  143. }