Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ri register int
  4. #define IO ios::sync_with_stdio(false)
  5. #define mid ((a[x].l+a[x].r)>>1)
  6. using namespace std;
  7. template < typename T >
  8. inline void read(T &x){
  9. x = 0;bool flag = 1;char c = getchar();
  10. while(c < '0' or c > '9'){
  11. if(c == '-') flag = 0;
  12. c = getchar();
  13. }
  14. while(c >= '0' and c <= '9'){
  15. x = (x << 1) + (x << 3) + (c^48); c = getchar();
  16. }
  17. x = (flag) ? x : -x ;
  18. }
  19. template < typename T >
  20. void print(T x)
  21. {
  22. if(x < 0){putchar('-'),x=-x;}
  23. if(x>9)print(x/10);
  24. putchar(int (x%10) + '0');
  25. }
  26.  
  27. const int N = 2e5 + 10;
  28. int head[N],dfn[N],top[N],siz[N],fa[N],son[N],va[N],val[N],dep[N],x[N],y[N];
  29. int to[N<<1],nxt[N<<1],w[N<<1];
  30. int n,m,cnt,tim;
  31. struct node{
  32. int l,r,mins,sum,maxs,laz;
  33. }a[N<<2];
  34.  
  35. inline void add(int u,int v,int val){to[++cnt]=v,nxt[cnt]=head[u],head[u]=cnt,w[cnt]=val;}
  36.  
  37. void dfs1(int x,int f){
  38. fa[x]=f,siz[x]=1,dep[x]=dep[f]+1;
  39. int ms=-1;
  40. for(int i=head[x];i;i=nxt[i]){
  41. int v=to[i];
  42. if(v==f) continue;
  43. va[v]=w[i];
  44. dfs1(v,x);
  45. siz[x]+=siz[v];
  46. if(ms<siz[v]) ms=siz[v],son[x]=v;
  47. }
  48. }
  49.  
  50. void dfs2(int x,int t){
  51. top[x]=t,dfn[x]=++tim,val[tim]=va[x];
  52. if(!son[x]) return;
  53. dfs2(son[x],t);
  54. for(int i=head[x];i;i=nxt[i]){
  55. int v=to[i];
  56. if(v==fa[x]||v==son[x])continue;
  57. dfs2(v,v);
  58. }
  59. }
  60.  
  61. //seg
  62. inline void pushup(int x){
  63. a[x].sum=a[x<<1].sum+a[x<<1|1].sum;
  64. a[x].maxs=max(a[x<<1].maxs,a[x<<1|1].maxs);
  65. a[x].mins=min(a[x<<1].mins,a[x<<1|1].mins);
  66. }
  67. inline void ch(int x){
  68. int tmp=a[x].maxs;
  69. a[x].laz^=1;
  70. a[x].maxs=-a[x].mins,a[x].mins=-tmp,a[x].sum=-a[x].sum;
  71. }
  72. inline void pushdown(int x){
  73. if(!a[x].laz) return;
  74. ch(x<<1),ch(x<<1|1);
  75. a[x].laz^=1;
  76. }
  77.  
  78. void build(int x,int l,int r){
  79. a[x].l=l,a[x].r=r;
  80. if(l==r) return a[x].mins=a[x].maxs=a[x].sum=val[l],void();
  81. build(x<<1,l,mid),build(x<<1|1,mid+1,r);
  82. pushup(x);
  83. }
  84.  
  85. void mpoint(int x,int p,int k){
  86. if(a[x].l==a[x].r) return a[x].sum=a[x].mins=a[x].maxs=k,void();
  87. pushdown(x);
  88. if(p<=mid)mpoint(x<<1,p,k);
  89. else mpoint(x<<1|1,p,k);
  90. pushup(x);
  91. }
  92.  
  93. void modify(int x,int l,int r){
  94. if(a[x].l>=l&&a[x].r<=r) return ch(x),void();
  95. pushdown(x);
  96. if(l <= mid) modify(x<<1,l,r);
  97. if(mid < r ) modify(x<<1|1,l,r);
  98. pushup(x);
  99. }
  100.  
  101. int qsum(int x,int l,int r){
  102. if(a[x].l>=l&&a[x].r<=r) return a[x].sum;
  103. pushdown(x);
  104. int res=0;
  105. if(l<=mid) res+=qsum(x<<1,l,r);
  106. if(mid <r) res+=qsum(x<<1|1,l,r);
  107. return res;
  108. }
  109.  
  110. int qmin(int x,int l,int r){
  111. if(a[x].l>=l&&a[x].r<=r) return a[x].mins;
  112. pushdown(x);
  113. int res=0x7fffffff;
  114. if(l<=mid) res=min(res,qmin(x<<1,l,r));
  115. if(mid <r) res=min(res,qmin(x<<1|1,l,r));
  116. return res;
  117. }
  118.  
  119. int qmax(int x,int l,int r){
  120. if(a[x].l>=l&&a[x].r<=r) return a[x].maxs;
  121. pushdown(x);
  122. int res=-0x7fffffff;
  123. if(l<=mid) res=max(res,qmax(x<<1,l,r));
  124. if(mid <r) res=max(res,qmax(x<<1|1,l,r));
  125. return res;
  126. }
  127.  
  128. void mchain(int x,int y){
  129. while(top[x]!=top[y]){
  130. if(dep[top[x]]<dep[top[y]]) swap(x,y);
  131. modify(1,dfn[top[x]],dfn[x]);
  132. x=fa[top[x]];
  133. }
  134. if(dep[x] > dep[y]) swap(x,y);
  135. modify(1,dfn[x]+1,dfn[y]);
  136. }
  137.  
  138. int qcsum(int x,int y){
  139. int res=0;
  140. while(top[x]!=top[y]){
  141. if(dep[top[x]] < dep[top[y]]) swap(x,y);
  142. res+=qsum(1,dfn[top[x]],dfn[x]);
  143. x=fa[top[x]];
  144. }
  145. if(dep[x]>dep[y]) swap(x,y);
  146. res+=qsum(1,dfn[x]+1,dfn[y]);
  147. return res;
  148. }
  149.  
  150. int qcmin(int x,int y){
  151. int res=0x7fffffff;
  152. while(top[x]!=top[y]){
  153. if(dep[top[x]] < dep[top[y]]) swap(x,y);
  154. res=min(res,qmin(1,dfn[top[x]],dfn[x]));
  155. x=fa[top[x]];
  156. }
  157. if(dep[x]>dep[y]) swap(x,y);
  158. res=min(res,qmin(1,dfn[x]+1,dfn[y]));
  159. return res;
  160. }
  161.  
  162. int qcmax(int x,int y){
  163. int res=-0x7fffffff;
  164. while(top[x]!=top[y]){
  165. if(dep[top[x]] < dep[top[y]]) swap(x,y);
  166. res=max(res,qmax(1,dfn[top[x]],dfn[x]));
  167. x=fa[top[x]];
  168. }
  169. if(dep[x]>dep[y]) swap(x,y);
  170. res=max(res,qmax(1,dfn[x]+1,dfn[y]));//记得要+1
  171. return res;
  172. }
  173.  
  174. void medge(int u,int v,int w){
  175. if(fa[u]==v) mpoint(1,dfn[u],w);
  176. else mpoint(1,dfn[v],w);
  177. }
  178.  
  179. int main(int argc,const char *argv[]){
  180. string tmp;
  181. int u,v,w;
  182. read(n);
  183. for(int i = 1; i < n ; i++){
  184. read(u),read(v),read(w);
  185. x[i]=u+1,y[i]=v+1;
  186. add(u+1,v+1,w),add(v+1,u+1,w);
  187. }
  188. dfs1(1,1),dfs2(1,1),build(1,1,n);
  189. read(m);
  190. while(m--){
  191. cin >> tmp;
  192. read(u),read(v);
  193. if(tmp=="C") medge(x[u],y[u],v);
  194. else if(tmp=="N") mchain(u+1,v+1);
  195. else if(tmp=="SUM") print(qcsum(u+1,v+1)),putchar('\n');
  196. else if(tmp=="MIN") print(qcmin(u+1,v+1)),putchar('\n');
  197. else if(tmp=="MAX") print(qcmax(u+1,v+1)),putchar('\n');
  198. }
  199. return 0;
  200. }
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement