Title Link: POJ 2763 housewife Wind
Test instructions: Abstraction is a Benquan between known nodes, two operations, 1 • Modify Edge, 2 • Ask for Benquan between two nodes.
AC Code:
#include <string.h> #include <stdio.h> #include <algorithm>using namespace std; #define Lson L,M,RT <<1#define rson m+1,r,rt<<1|1const int maxn=100000+10;const int inf=9999999999;struct Edge{int to,next;}; struct Edge edge[maxn*2];int head[maxn],tot;int stree[maxn];//node v number int pre[maxn];//in the segment tree is numbered in the corresponding original image of the V node in the segment tree. int Fa[maxn];//f[v] Represents the Father node of V, int son[maxn];//, which represents the number of nodes of the son node (heavy son) int num[maxn];//num[v] V that is the root of the V-same on a heavy chain. int Top[maxn];//top[v] V is located at the top node of the chain. int Deep[maxn];//deep[v] represents the depth of V (the depth of the root is 1) int data[maxn];int pos,n;void addedge (int u,int v) {Edge[tot].to=v;edge[tot]. next=head[u];head[u]=tot++;} void dfs1 (int u,int father,int dep) {int i;deep[u]=dep;fa[u]=father;num[u]=1;for (i=head[u];i!=-1;i=edge[i].next) {int V=edge[i].to;if (v!=father) {DFS1 (v,u,dep+1); Num[u]+=num[v];if (Son[u]==-1 | | num[v]>num[son[u]]) {son[u]=v;}}} Give the point number to get the corresponding label on the segment tree void getpos (int u,int sp) {int i;top[u]=sp;stree[u]=pos++;p re[stree[u]]=u;if (son[u]==-1) return; GetPos (SON[U],SP); for (I=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to;if (V!=son[u] && V!=fa[u]) {GetPos (v,v);}}} Tree array or segment tree or other int Max (int a,int b) {if (a>b) return A;return B;} struct node{int l,r;int Sum,max;}; struct node tree[maxn*3];void pushup (int rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;} void build (int l,int R,int RT) {tree[rt].l=l;tree[rt].r=r;tree[rt].sum=0;if (l==r) {return;} int m= (TREE[RT].L+TREE[RT].R) >>1;build (Lson); build (Rson); Pushup (RT);} void update (int p,int Add,int RT) {if (TREE[RT].L==TREE[RT].R && tree[rt].l==p) {Tree[rt].sum=add;return;} int m= (TREE[RT].L+TREE[RT].R) >>1;if (p<=m) update (p,add,rt<<1); else update (P,ADD,RT<<1|1); Pushup (RT);} int query_sum (int l,int r,int RT) {if (l<=tree[rt].l && tree[rt].r<=r) {return tree[rt].sum;} int m= (TREE[RT].L+TREE[RT].R) >>1;int ret=0;if (l<=m) ret+=query_sum (l,r,rt<<1); if (R>m) ret+=query _sum (l,r,rt<<1|1); return ret;} int find_sum (int u,int v) {int F1,f2,ans=0;f1=top[u];f2=top[v];whilE (F1!=F2) {//Always f1 a greater depth than F2. Depth First update if (DEEP[F1]<DEEP[F2]) {swap (F1,F2); swap (u,v);} Ans+=query_sum (stree[f1],stree[u],1); u=fa[f1];f1=top[u];} if (u==v) return ans; if (Deep[u]>deep[v]) swap (U,V); Ans+=query_sum (stree[son[u]],stree[v],1); return ans;} void Init () {Tot=0;memset (head,-1,sizeof head);p Os=0;memset (son,-1,sizeof son); int E[maxn][5];int Main () {int t,x,y;int q,i,s,tmp;init (); scanf ("%d%d%d", &n,&q,&s); Tmp=s;for (i=0;i< n-1;i++) {scanf ("%d%d%d", &e[i][0],&e[i][1],&e[i][2]); Addedge (e[i][0],e[i][1]); Addedge (e[i][1],e[i][ 0]);} DFS1 (1,0,0);//Root Deep is 0.father starting from 0. GetPos (1,n,1), build (i=0;i<n-1;i++) {if (deep[e[i][0]]>deep[e[i][1]) swap (e[i][0],e[i][1]), update ( stree[e[i][1]],e[i][2],1);} int Op;while (q--) {int a,b;scanf ("%d", &op), if (op==0) {scanf ("%d", &a);p rintf ("%d\n", Find_sum (Tmp,a)); tmp=a;} ELSE{SCANF ("%d%d", &a,&b); update (stree[e[a-1][1]],b,1);}} return 0;}
POJ 2763 housewife Wind (tree chain split + line tree)