HDU 3966 树链剖分学习
来源:互联网 发布:ubuntu uefi启动盘 编辑:程序博客网 时间:2024/05/17 20:25
这道题不难,看出是树链剖分就很简单,其实奇异也很明显就是一个树链剖分,不过这次不是把树链扔到线段树上,是把节点扔到线段树上。这个时候注意一下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 55000using 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;}
0 0
- HDU 3966 树链剖分学习
- HDU 3966 树链剖分
- hdu 3966 树链剖分模版
- hdu 3966 树链剖分
- hdu--3966(树链剖分)
- HDU 3966 树链剖分模板
- hdu 3966(树链剖分)
- HDU 3966 树链剖分
- HDU 3966 树链剖分
- hdu 3966(树链剖分)
- HDU 3966 树链剖分
- HDU 3966 [树链剖分]
- 树链剖分 HDU 3966
- HDU-3966-树链剖分
- hdu 3966 树链剖分
- hdu 3966 树链剖分
- 三分学习 hdu 4454
- hdu 3966 (树链剖分+线段树)
- NYOJ 118 修路方案 (次小生成树--prime)
- 第二章 概述
- [leetcode-274]H-Index(java)
- 【二分匹配】【HK算法模板】
- [译]NGINX缓存使用官方指南
- HDU 3966 树链剖分学习
- openwrt编译error: ext4_allocate_best_fit_partial: failed to allocate 13 blocks, out of space?
- nodejs 浏览器弹窗下载图片 data:image/jpeg;base64示例
- hdu2188(巴什博弈)
- JNI里面调用Java,JNI非JAVA线程里面调用Java方法
- 用python列出当前目录的.py文件
- 【Python基础】模块
- Java
- easyUI 笔记