Guest User

Untitled

a guest
Nov 3rd, 2024
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define sws ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
  4. #define ld long double
  5. #define ff first
  6. #define ss second
  7. #define dbg(x) cout<<#x<<": "<<x<<"\n";
  8. #define pq priority_queue
  9. #define mp make_pair
  10. #define pb push_back
  11. #define rep(i,a,b) for(int i=a;i<b;i++)
  12. typedef vector<int> vi;
  13. typedef vector<vi> vvi;
  14. typedef pair<int,int>pii;
  15. const int MAXN = 1e5+1;
  16. vi adj[MAXN];
  17. int head[MAXN];
  18. int heavy_c[MAXN];
  19. int idx[MAXN];
  20. int fat[MAXN];
  21. int h[MAXN];
  22. int tt=0;
  23.  
  24. int dfs1(int v=1 ,int p=1,int he=0){
  25. pii m = mp(0,v);
  26. fat[v]=p;
  27. h[v]=he;
  28. for(auto c:adj[v]){
  29. if(c==p)continue;
  30. pii mm = mp(dfs1(c,v,he+1),c);
  31. mm.ff++;
  32. m = max(mm,m);
  33. }
  34. heavy_c[v]=m.ss;
  35. return m.ff;
  36. }
  37.  
  38. void dfs2(int v=1,int p=-1,int curhead=1){
  39. idx[v]=tt++;
  40. head[v]=curhead;
  41. if(heavy_c[v]!=v)dfs2(heavy_c[v],v,curhead);
  42.  
  43. for(auto c:adj[v]){
  44. if(c==p || c==heavy_c[v])continue;
  45. dfs2(c,v,c);
  46. }
  47. }
  48.  
  49.  
  50. struct SegTree{
  51. vi lazy;
  52. vi t;
  53. int n;
  54. SegTree(int n):n(n),lazy(n<<2,0),t(n<<2,0){}
  55.  
  56. void push(int v,int tl,int tr){
  57. if(lazy[v]==0)return;
  58. int tm = (tl+tr)/2;
  59. int szl = tm-tl+1;
  60. int szr = tr-tm;
  61. t[v<<1]+=(szl*lazy[v]);
  62. t[v<<1|1]+=(szr*lazy[v]);
  63. lazy[v<<1]+=lazy[v];
  64. lazy[v<<1|1]+=lazy[v];
  65. lazy[v]=0;
  66. }
  67.  
  68. void update(int v,int tl,int tr,int l,int r,int val){
  69. if(tl==l && tr==r){
  70. t[v]+=(tr-tl+1)*val;
  71. lazy[v]+=val;
  72. return;
  73. }
  74. if(tl>r ||tr<l)return;
  75. push(v,tl,tr);
  76. int tm = (tl+tr)/2;
  77. update(v<<1,tl,tm,l,min(tm,r),val);
  78. update(v<<1|1,tm+1,tr,max(tm+1,l),r,val);
  79. t[v]=t[v<<1]+t[v<<1|1];
  80. }
  81.  
  82. void update(int l,int r,int val){
  83. update(1,0,n-1,l,r,val);
  84. }
  85.  
  86.  
  87.  
  88. int get(int v,int tl,int tr,int l,int r){
  89. if(tl==l && tr==r)return t[v];
  90. if(tl>r ||tr<l)return 0 ;
  91. push(v,tl,tr);
  92. int tm = (tl+tr)/2;
  93. return get(v<<1,tl,tm,l,min(tm,r))+get(v<<1|1,tm+1,tr,max(tm+1,l),r);
  94. }
  95.  
  96. int get(int l,int r){
  97. return get(1,0,n-1,l,r);
  98. }
  99. };
  100.  
  101.  
  102. SegTree st(MAXN);
  103.  
  104. void update(int a,int b){
  105. // cout<<"UPDATE MASTER ENTRE "<<a<<" e "<<b<<"\n";
  106. while(head[a]!=head[b]){
  107. if(h[head[a]]<h[head[b]])swap(a,b);
  108. st.update(idx[head[a]],idx[a],1);
  109.  
  110. a=fat[head[a]];
  111. }
  112.  
  113. if(idx[a]>idx[b])swap(a,b);
  114. st.update(idx[a],idx[b],1);
  115. }
  116.  
  117. int get(int a,int b){
  118. int ans=0;
  119.  
  120. while(head[a]!=head[b]){
  121. if(h[head[a]]<h[head[b]])swap(a,b);
  122. ans+=st.get(idx[head[a]],idx[a]);
  123. a=fat[head[a]];
  124. }
  125.  
  126. if(idx[a]>idx[b])swap(a,b);
  127. ans+= st.get(idx[a],idx[b]);
  128. return ans;
  129. }
  130. signed main() {
  131. sws;
  132. int n,q;
  133. cin>>n>>q;
  134. rep(i,0,n-1){
  135. int a,b;
  136. cin>>a>>b;
  137. adj[a].pb(b);
  138. adj[b].pb(a);
  139. }
  140.  
  141. dfs1();
  142. dfs2();
  143.  
  144. while(q--){
  145. int a,b,c,d;
  146. cin>>a>>b>>c>>d;
  147. int prior = get(c,d);
  148. update(a,b);
  149. cout<<get(c,d)-prior<<"\n";
  150. }
  151.  
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment