Click to open link
Test instructions: Chinese
Idea: The most basic tree chain, interval summation and single-point update, the result of a long long is nothing
#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int in f=0x3f3f3f3f;const ll Inf=0x3f3f3f3f3f3f3f3fll;const int Maxn=100010;int fa[maxn],siz[maxn],son[maxn],w[maxn],p[ MAXN],DEP[MAXN],FP[MAXN],HEAD[MAXN];//FA is the parent node, Siz is the largest siz in the child node, DEP is the depth, son is heavy, and W represents the position in the segment tree in int num[maxn<<2]; int tree_id,n,kkk=0;struct node{int to,next;} Ee[maxn*10];void Add_edge (int u,int v) {ee[kkk].to=v; ee[kkk].next=head[u];head[u]=kkk++;} void dfs1 (int u,int ff,int deep) {son[u]=0;fa[u]=ff;siz[u]=1;dep[u]=deep; for (int i=head[u];i!=-1;i=ee[i].next) {int v=ee[i].to; if (V==FF) continue; DFS1 (v,u,deep+1); SIZ[U]+=SIZ[V]; if (Siz[v]>siz[son[u]]) son[u]=v; }}void DFS2 (int u,int ff) {w[u]=++tree_id;p[u]=ff; if (Son[u]) DFS2 (SON[U],FF); else return; for (int i=head[u];i!=-1;i=ee[i].next) { int v=ee[i].to; if (V!=fa[u]&&v!=son[u]) DFS2 (V,V); }}void Update (int pos,int val,int le,int ri,int node) {if (Le==ri) {num[node]=val; return; } int t= (LE+RI) >>1; if (pos<=t) update (POS,VAL,LE,T,NODE<<1); else update (POS,VAL,T+1,RI,NODE<<1|1); NUM[NODE]=NUM[NODE<<1]+NUM[NODE<<1|1];} int query (int l,int r,int le,int ri,int node) {if (l<=le&&ri<=r) return Num[node]; int t= (LE+RI) >>1,ans=0; if (l<=t) ans=ans+query (l,r,le,t,node<<1); if (r>t) ans=ans+query (l,r,t+1,ri,node<<1|1); return ans;} ll getmin (int u,int v) {int f1=p[u],f2=p[v]; ll Tmp=0; while (F1!=F2) {if (Dep[f1]<dep[f2]) {swap (F1,F2); Swap (U,V); } tmp=tmp+query (w[f1],w[u],1,n,1); U=fa[f1];f1=p[u]; } if (U==V) return tmp; if (Dep[u]>dep[v]) swap (U,V); Return Tmp+query (w[son[u]],w[v],1,n,1);} int U[maxn],v[maxn],c[maxn];int mAin () {int u,v,q,op; while (scanf ("%d%d", &n,&q)!=-1) {memset (head,-1,sizeof (head)); memset (son,0,sizeof (son)); tree_id=0;kkk=0; for (int i=0;i<n-1;i++) {scanf ("%d%d%d", &u[i],&v[i],&c[i]); Add_edge (U[i],v[i]); Add_edge (V[i],u[i]); } DFS1 (1,1,0); DFS2 (a); memset (num,0,sizeof (num)); for (int i=0;i<n-1;i++) {if (Dep[u[i]]>dep[v[i]) swap (u[i],v[i]); Update (w[v[i]],c[i],1,n,1); } while (q--) {scanf ("%d%d%d", &op,&u,&v); if (op==0) update (w[v[u-1]],v,1,n,1); else{ll Fans=getmin (u,v); printf ("%i64d\n", fans); }}} return 0;}
Fzu 2082 tree Chain split