Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define sc(a) scanf("%d",&a)
- #define pb push_back
- const int MX=2e5+6;
- int n,q,w[MX],fr[MX],to[4*MX],cnt,L[MX],R[MX],WE[MX];
- ll lasy[4*MX],val;
- int st,en;
- vector<vector<int> > adj;
- void dfs(int x)
- {
- L[x]=++cnt;
- for(int i=0;i<adj[x].size();i++)
- {
- int u=adj[x][i];
- dfs(u);
- }
- R[x]=cnt;
- }
- void push(int x)
- {
- lasy[2*x]+=lasy[x];lasy[2*x+1]+=lasy[x];
- lasy[x]=0;
- }
- void update(int x=1,int l=1,int r=cnt)
- {
- if(l>=st&&r<=en)
- {
- lasy[x]+=val;
- return;
- }
- if(l>en||r<st)return;
- push(x);
- update(2*x,l,(l+r)/2);
- update(2*x+1,(l+r)/2+1,r);
- }
- ll get(int x=1,int l=1,int r=cnt)
- {
- if(l>en||r<st)return 0;
- if(l==r)return lasy[x];
- push(x);
- return get(2*x,l,(l+r)/2)+get(2*x+1,(l+r)/2+1,r);
- }
- int main()
- {
- cin>>n>>q;
- adj.resize(n+6);
- for(int i=1;i<n;i++)
- {
- int u,v,c;
- sc(u);sc(v);sc(c);
- adj[u].pb(v);
- w[i]=c;fr[i]=u,to[i]=v;
- }
- dfs(1);
- for(int i=1;i<n;i++)
- {
- int x=to[i];
- st=L[x],en=R[x];val=w[i];
- update();
- }
- for(int i=1;i<n;i++){
- int x,y,c;
- sc(x);sc(y);sc(c);
- to[i+n-1]=x;
- WE[x]=c;
- }
- for(int i=1;i<=q;i++)
- {
- int t;sc(t);
- int u,v;sc(u);sc(v);
- if(t==1)
- {
- if(u>=n)
- {
- WE[to[u]]=v;
- }
- else{
- int x=to[u];
- st=L[x],en=R[x];val=v-w[u];
- update();
- w[u]=v;
- }
- }else
- {
- st=en=L[u];
- ll zb = get();
- ll ans = 1e18;
- st=en=L[v];
- ll zb2=get();
- ans=min(ans,zb2+WE[u]);
- if(L[v]>=L[u]&&L[v]<=R[u]){
- ans=zb2-zb;
- }
- printf("%lld\n",ans);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement