Advertisement
Saleh127

Light OJ 1348 / LCA with weight update using Segment Tree

Feb 5th, 2022
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. /***
  2.  created: 2022-02-05-01.57.27
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. vector<ll>g[30005];
  13. ll a[100005];
  14. ll tree[500005];
  15. ll st[30005];
  16. ll fn[30005];
  17. bool v[30005];
  18. ll blift[30005][20];
  19. ll depth[30005];
  20. ll tim=1,lg,n;
  21. ll val[30005];
  22.  
  23. void eulerpath(ll node)
  24. {
  25.     st[node]=tim;
  26.     tim++;
  27.     v[node]=1;
  28.     for(auto d:g[node])
  29.     {
  30.         if(v[d]==0)
  31.         {
  32.             eulerpath(d);
  33.         }
  34.     }
  35.     fn[node]=tim;
  36.     tim++;
  37. }
  38.  
  39. void dfs(ll node)
  40. {
  41.     v[node]=1;
  42.     for(int d:g[node])
  43.     {
  44.         if(v[d]==0)
  45.         {
  46.             depth[d]=depth[node]+1;
  47.             blift[d][0]=node;
  48.             dfs(d);
  49.         }
  50.     }
  51. }
  52.  
  53. void binarylifting()
  54. {
  55.     for(ll j=1; j<=lg; j++)
  56.     {
  57.         for(ll i=1; i<=n; i++)
  58.         {
  59.             if(blift[i][j-1]!=-1)
  60.             {
  61.                 ll par = blift[i][j-1];
  62.  
  63.                 blift[i][j] = blift[par][j-1];
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69. ll lca(ll a,ll b)
  70. {
  71.     if(depth[a]<depth[b]) swap(a,b);
  72.  
  73.     ll k=depth[a]-depth[b];
  74.  
  75.     for(ll j=lg-1; j>=0; j--)
  76.     {
  77.         if(k &(1<<j))
  78.         {
  79.             a=blift[a][j];
  80.         }
  81.     }
  82.  
  83.     if(a==b)
  84.     {
  85.         return a;
  86.     }
  87.  
  88.     for(ll j=lg-1; j>=0; j--)
  89.     {
  90.         if(blift[b][j]!=-1 &&  blift[a][j]!=blift[b][j])
  91.         {
  92.             a=blift[a][j];
  93.             b=blift[b][j];
  94.         }
  95.     }
  96.  
  97.     return blift[a][0];
  98. }
  99.  
  100.  
  101. void treeconstruct(ll node,ll b,ll e)
  102. {
  103.     if(b==e)
  104.     {
  105.         tree[node]=a[b];
  106.         return;
  107.     }
  108.     ll left = node*2;
  109.     ll right = left|1ll;
  110.     ll mid=(b+e)/2;
  111.     treeconstruct(left,b,mid);
  112.     treeconstruct(right,mid+1,e);
  113.     tree[node]=tree[left]+tree[right];
  114. }
  115.  
  116. void update(ll node,ll b,ll e,ll i,ll newv)
  117. {
  118.     if(i>e || i<b) return;
  119.     if(b==e)
  120.     {
  121.         tree[node]=newv;
  122.         return;
  123.     }
  124.     ll left = node*2;
  125.     ll right = left|1ll;
  126.     ll mid=(b+e)/2;
  127.     update(left,b,mid,i,newv);
  128.     update(right,mid+1,e,i,newv);
  129.     tree[node]=tree[left]+tree[right];
  130. }
  131.  
  132. ll queries(ll node,ll b,ll e,ll i,ll j)
  133. {
  134.     if(i>e || j<b) return 0;
  135.     if(b>=i && e<=j) return tree[node];
  136.     ll left = node*2;
  137.     ll right = left|1ll;
  138.     ll mid=(b+e)/2;
  139.     ll x=queries(left,b,mid,i,j);
  140.     ll y=queries(right,mid+1,e,i,j);
  141.     return x+y;
  142. }
  143.  
  144. void clr()
  145. {
  146.     tim=1;
  147.     for(ll i=0; i<n+2; i++)
  148.     {
  149.         g[i].clear();
  150.         depth[i]=0;
  151.         v[i]=0;
  152.     }
  153.     memset(blift,-1,sizeof blift);
  154. }
  155.  
  156. int main()
  157. {
  158.     ios_base::sync_with_stdio(0);
  159.     cin.tie(0);
  160.     cout.tie(0);
  161.  
  162.  
  163.     test
  164.     {
  165.         ll q,m,i,j,k,l,root,ans;
  166.  
  167.         cin>>n;
  168.  
  169.         lg=log2(n)+1;
  170.         clr();
  171.  
  172.         for(i=1; i<=n; i++)
  173.         {
  174.             cin>>val[i];
  175.         }
  176.         for(i=0; i<n-1; i++)
  177.         {
  178.             cin>>j>>k;
  179.             j++,k++;
  180.             g[j].push_back(k);
  181.             g[k].push_back(j);
  182.         }
  183.         eulerpath(1);
  184.         memset(v,0,sizeof v);
  185.         dfs(1);
  186.         binarylifting();
  187.  
  188.         for(i=1; i<=n; i++)
  189.         {
  190.             a[st[i]]=val[i];
  191.             a[fn[i]]=-val[i];
  192.         }
  193.  
  194.         treeconstruct(1,1,2*n);
  195.  
  196.         cout<<"Case "<<cs<<":"<<nl;
  197.  
  198.         cin>>q;
  199.  
  200.         while(q--)
  201.         {
  202.             cin>>i;
  203.  
  204.             if(i==0)
  205.             {
  206.                 cin>>j>>k;
  207.                 j++,k++;
  208.  
  209.                 if(st[j]>st[k]) swap(j,k);
  210.  
  211.                 root=lca(j,k);
  212.  
  213.                 ll j1=queries(1,1,2*n,st[root],st[j]);
  214.                 ll k1=queries(1,1,2*n,st[root],st[k]);
  215.  
  216.                 ans = j1 + k1 - val[root];
  217.  
  218.                 cout<<ans<<nl;
  219.             }
  220.             else
  221.             {
  222.                  cin>>j>>k;
  223.                  j++;
  224.                  val[j]=k;
  225.                  update(1,1,2*n,st[j],k);
  226.                  update(1,1,2*n,fn[j],-k);
  227.             }
  228.         }
  229.  
  230.     }
  231.  
  232.     get_lost_idiot;
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement