Advertisement
jeff69

B codeforces

Aug 7th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define sc(a) scanf("%d",&a)
  5. #define pb push_back
  6. const int MX=2e5+6;
  7. int n,q,w[MX],fr[MX],to[4*MX],cnt,L[MX],R[MX],WE[MX];
  8. ll lasy[4*MX],val;
  9. int st,en;
  10. vector<vector<int> > adj;
  11. void dfs(int x)
  12. {
  13.    L[x]=++cnt;
  14.     for(int i=0;i<adj[x].size();i++)
  15.     {
  16.         int u=adj[x][i];
  17.         dfs(u);
  18.     }
  19.     R[x]=cnt;
  20. }
  21. void push(int x)
  22. {
  23.     lasy[2*x]+=lasy[x];lasy[2*x+1]+=lasy[x];
  24.     lasy[x]=0;
  25. }
  26. void update(int x=1,int l=1,int r=cnt)
  27. {
  28.     if(l>=st&&r<=en)
  29.     {
  30.         lasy[x]+=val;
  31.         return;
  32.     }
  33.     if(l>en||r<st)return;
  34.     push(x);
  35.     update(2*x,l,(l+r)/2);
  36.     update(2*x+1,(l+r)/2+1,r);
  37. }
  38. ll get(int x=1,int l=1,int r=cnt)
  39. {
  40.     if(l>en||r<st)return 0;
  41.     if(l==r)return lasy[x];
  42.     push(x);
  43.     return get(2*x,l,(l+r)/2)+get(2*x+1,(l+r)/2+1,r);
  44. }
  45. int main()
  46. {
  47.     cin>>n>>q;
  48.     adj.resize(n+6);
  49.     for(int i=1;i<n;i++)
  50.     {
  51.         int u,v,c;
  52.         sc(u);sc(v);sc(c);
  53.         adj[u].pb(v);
  54.         w[i]=c;fr[i]=u,to[i]=v;
  55.     }
  56.     dfs(1);
  57.     for(int i=1;i<n;i++)
  58.     {
  59.         int x=to[i];
  60.         st=L[x],en=R[x];val=w[i];
  61.         update();
  62.     }
  63.     for(int i=1;i<n;i++){
  64.         int x,y,c;
  65.         sc(x);sc(y);sc(c);
  66.         to[i+n-1]=x;
  67.         WE[x]=c;
  68.     }
  69.     for(int i=1;i<=q;i++)
  70.     {
  71.         int t;sc(t);
  72.         int u,v;sc(u);sc(v);
  73.         if(t==1)
  74.         {
  75.             if(u>=n)
  76.             {
  77.                 WE[to[u]]=v;
  78.             }
  79.             else{
  80.             int x=to[u];
  81.              st=L[x],en=R[x];val=v-w[u];
  82.             update();
  83.             w[u]=v;
  84.             }
  85.         }else
  86.         {
  87.             st=en=L[u];
  88.          ll zb = get();
  89.           ll ans = 1e18;
  90.             st=en=L[v];
  91.             ll zb2=get();
  92.             ans=min(ans,zb2+WE[u]);
  93.             if(L[v]>=L[u]&&L[v]<=R[u]){
  94.                 ans=zb2-zb;
  95.             }
  96.             printf("%lld\n",ans);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement