HDU 3966 树链剖分学习
这道题不难,看出是树链剖分就很简单,其实奇异也很明显就是一个树链剖分,不过这次不是把树链扔到线段树上,是把节点扔到线段树上。这个时候注意一下w[1] = 1就行了,其他就是线段树的一个区间覆盖的问题了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #define LL long long #define FOR(i,x,y) for(int i = x;i < y;i ++) #define IFOR(i,x,y) for(int i = x;i > y;i --) #define lrt rt<<1 #define rrt rt<<1|1 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define MAXN 55000 using namespace std; int n,m,p; struct Edge{ int u,v; int nt; }edge[MAXN<<1]; int edge_cnt,head[MAXN]; void add_edge(int u,int v){ edge[edge_cnt].u = u; edge[edge_cnt].v = v; edge[edge_cnt].nt = head[u]; head[u] = edge_cnt++; } int fa[MAXN],top[MAXN],sz[MAXN],son[MAXN],dep[MAXN],w[MAXN],val[MAXN],totw; void dfs1(int u,int father,int depth){ fa[u] = father; sz[u] = 1; dep[u] = depth; int idd = -1,maxx = -1; for(int i = head[u];i != -1;i = edge[i].nt){ int v = edge[i].v; if(v == father) continue; dfs1(v,u,depth+1); sz[u] += sz[v]; if(sz[v] > maxx){ maxx = sz[v]; idd = v; } } son[u] = idd; } void dfs2(int u,int father){ if(son[u] == -1) return; top[son[u]] = top[u]; w[son[u]] = ++totw; dfs2(son[u],u); for(int i = head[u];i != -1;i = edge[i].nt){ int v = edge[i].v; if(v == father) continue; if(v == son[u]) continue; top[v] = v; w[v] = ++totw; dfs2(v,u); } } struct Tree{ int l,r; int lazy; }tree[MAXN<<2]; void PushDown(int rt){ if(tree[rt].lazy){ tree[lrt].lazy += tree[rt].lazy; tree[rrt].lazy += tree[rt].lazy; tree[rt].lazy = 0; } } void Build(int rt,int l,int r){ tree[rt].l = l; tree[rt].r = r; tree[rt].lazy = 0; if(l == r) return; int mid = (l+r)>>1; Build(lson); Build(rson); } void Modify(int rt,int l,int r,int value){ if(tree[rt].l == l && tree[rt].r == r){ tree[rt].lazy += value; return; } PushDown(rt); int mid = (tree[rt].l + tree[rt].r)>>1; if(r <= mid) Modify(lrt,l,r,value); else if(l > mid) Modify(rrt,l,r,value); else{ Modify(lson,value); Modify(rson,value); } } int Query(int rt,int x){ if(tree[rt].l == tree[rt].r){ return tree[rt].lazy; } PushDown(rt); int mid = (tree[rt].l + tree[rt].r)>>1; if(x <= mid) return Query(lrt,x); else return Query(rrt,x); } void C_Modify(int l,int r,int value){ while(top[l] != top[r]){ if(dep[top[l]] >= dep[top[r]]){ Modify(1,w[top[l]],w[l],value); l = fa[top[l]]; } else{ Modify(1,w[top[r]],w[r],value); r = fa[top[r]]; } } if(l == r){ Modify(1,w[l],w[r],value); } else if(dep[l] >= dep[r]){ Modify(1,w[r],w[l],value); } else Modify(1,w[l],w[r],value); } int main() { //freopen("test.in","r",stdin); while(~scanf("%d%d%d",&n,&m,&p)){ FOR(i,1,n+1) scanf("%d",&val[i]); edge_cnt = 0; memset(head,-1,sizeof(head)); FOR(i,0,m){ int u,v; scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); } dfs1(1,-1,1); totw = 0; top[1] = 1; w[1] = ++totw; dfs2(1,-1); Build(1,1,totw); FOR(i,0,p){ char op[10]; scanf("%s",op); if(op[0] == 'I'){ int l,r,value; scanf("%d%d%d",&l,&r,&value); C_Modify(l,r,value); } else if(op[0] == 'D'){ int l,r,value; scanf("%d%d%d",&l,&r,&value); C_Modify(l,r,-value); } else{ int x; scanf("%d",&x); printf("%d\n",val[x]+Query(1,w[x])); } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。