首页 > 代码库 > HDU 3966 Aragorn's Story 树链剖分
HDU 3966 Aragorn's Story 树链剖分
入门题
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 50010; struct edge { int v, next; }e[maxn*2]; int n, m, q; int first[maxn], cnt; int top[maxn], tid[maxn], rank[maxn], sz[maxn], f[maxn], son[maxn], dep[maxn], tim; int d[maxn][2], a[maxn]; void AddEdge(int u, int v) { e[cnt].v = v; e[cnt].next = first[u]; first[u] = cnt++; e[cnt].v = u; e[cnt].next = first[v]; first[v] = cnt++; } void dfs1(int u, int fa, int d) { sz[u] = 1; f[u] = fa; dep[u] = d; for(int i = first[u]; i != -1; i = e[i].next) { int v = e[i].v; if(v == f[u]) continue; dfs1(v, u, d+1); sz[u] += sz[v]; if(son[u] == -1 || sz[son[u]] < sz[v]) son[u] = v; } } void dfs2(int u, int tp) { top[u] = tp; tid[u] = ++tim; rank[tid[u]] = u; if(son[u] == -1) return; dfs2(son[u], tp); for(int i = first[u]; i != -1; i = e[i].next) { int v = e[i].v; if(v != son[u] && v != f[u]) dfs2(v, v); } } void init() { memset(first, -1, sizeof(first)); cnt = 0; memset(son, -1, sizeof(son)); tim = 0; } int st[maxn<<2]; int lz[maxn<<2]; void build(int l, int r, int rt) { st[rt] = lz[rt] = 0; if(l == r) { lz[rt] = a[rank[l]]; return; } int m = (l + r) >> 1; build(l, m, rt<<1); build(m+1, r, rt<<1|1); } void pushdown(int l, int r, int rt) { if(lz[rt]) { lz[rt<<1] += lz[rt]; lz[rt<<1|1] += lz[rt]; lz[rt] = 0; } } void update(int x, int y, int l, int r, int rt, int w) { if(x == l && y == r) { lz[rt] += w; return; } pushdown(l, r, rt); int m = (l + r) >> 1; if(y <= m) update(x, y, l, m, rt<<1, w); else if(x > m) update(x, y, m+1, r, rt<<1|1, w); else { update(x, m, l, m, rt<<1, w); update(m+1, y, m+1, r, rt<<1|1, w); } } int query(int l, int r, int rt, int x) { if(l == r) return lz[rt]; pushdown(l, r, rt); int m = (l + r) >> 1; if(x <= m) return query(l, m, rt<<1, x); else return query(m+1, r, rt<<1|1, x); } void change(int u, int v, int w) { while(top[u] != top[v]) { if(dep[top[u]] < dep[top[v]]) swap(u, v); update(tid[top[u]], tid[u], 1, n, 1, w); u = f[top[u]]; } if(dep[u] > dep[v]) swap(u, v); update(tid[u], tid[v], 1, n, 1, w); } int main() { while(scanf("%d %d %d", &n, &m, &q) != EOF) { init(); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); AddEdge(u, v); } dfs1(1, -1, 0); dfs2(1, 1); build(1, n, 1); while(q--) { char s[10]; scanf("%s", s); if(s[0] == 'I') { int u, v, w; scanf("%d %d %d", &u, &v, &w); change(u, v, w); } else if(s[0] == 'D') { int u, v, w; scanf("%d %d %d", &u, &v, &w); change(u, v, -w); } else { int x; scanf("%d", &x); printf("%d\n", query(1, n, 1, tid[x])); } } } return 0; }
HDU 3966 Aragorn's Story 树链剖分
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。