博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3924 : [Zjoi2015]幻想乡战略游戏
阅读量:6585 次
发布时间:2019-06-24

本文共 3486 字,大约阅读时间需要 11 分钟。

Sol

作为一个刚刚学动态点分治的新手,表示这道题很难啃动。。。

既然是动态点分治,那么先建出点分树,之后暴跳父亲就是log的

这道题就是要求带权重心,可以证明,随意在点分树上从一个点出发,每次选最小答案的子重心,最后一定能找到答案。。感觉就相当于在树上二分。。。

修改就爆跳父亲

# include 
# define RG register# define IL inline# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;const int _(2e5 + 10);IL ll Read(){ RG ll x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1; for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z;}int n, fst[_], nxt[_], w[_], to[_], cnt, Q;IL void Add(RG int u, RG int v, RG int ww){ nxt[cnt] = fst[u]; to[cnt] = v; w[cnt] = ww; fst[u] = cnt++; }namespace ChainDiv{ int fa[_], size[_], top[_], deep[_], son[_], dfn[_], Index; IL void Dfs1(RG int u){ size[u] = 1; for(RG int e = fst[u]; e != -1; e = nxt[e]){ if(size[to[e]]) continue; deep[to[e]] = deep[u] + w[e]; fa[to[e]] = u; Dfs1(to[e]); size[u] += size[to[e]]; if(size[to[e]] > size[son[u]]) son[u] = to[e]; } } IL void Dfs2(RG int u, RG int Top){ top[u] = Top; dfn[u] = ++Index; if(son[u]) Dfs2(son[u], Top); for(RG int e = fst[u]; e != -1; e = nxt[e]) if(!dfn[to[e]]) Dfs2(to[e], to[e]); } IL ll Dis(RG int u, RG int v){ RG ll dis = deep[u] + deep[v]; while(top[u] ^ top[v]){ if(deep[top[u]] < deep[top[v]]) swap(u, v); u = fa[top[u]]; } if(deep[u] > deep[v]) swap(u, v); return dis - 2 * deep[u]; }}int size[_], mx[_], frt[_], vis[_], rt, sz, root, ft[_];struct Edge{ int nt, to, rt; } edge[_];ll sum[_], pres[_], alls[_];IL void _Add(RG int u, RG int v, RG int rrt){ edge[cnt] = (Edge){ft[u], v, rrt}; ft[u] = cnt++; }IL void Getroot(RG int u, RG int ff){ size[u] = 1; mx[u] = 0; for(RG int e = fst[u]; e != -1; e = nxt[e]){ if(vis[to[e]] || to[e] == ff) continue; Getroot(to[e], u); size[u] += size[to[e]]; mx[u] = max(mx[u], size[to[e]]); } mx[u] = max(mx[u], sz - size[u]); if(mx[u] < mx[rt]) rt = u;}IL void Create(RG int u, RG int ff){ frt[u] = ff; vis[u] = 1; for(RG int e = fst[u]; e != -1; e = nxt[e]){ if(vis[to[e]]) continue; rt = 0; sz = size[to[e]]; Getroot(to[e], u); _Add(u, to[e], rt); Create(rt, u); }}IL void Modify(RG int u, RG ll d){ sum[u] += d; for(RG int v = u; frt[v]; v = frt[v]){ RG ll dis = ChainDiv::Dis(u, frt[v]); sum[frt[v]] += d; pres[v] += d * dis; alls[frt[v]] += d * dis; }}IL ll Calc(RG int u){ RG ll ret = alls[u]; for(RG int v = u; frt[v]; v = frt[v]){ RG ll dis = ChainDiv::Dis(u, frt[v]); ret += dis * (sum[frt[v]] - sum[v]); ret += alls[frt[v]] - pres[v]; } return ret;}IL ll Query(RG int u){ RG ll tmp = Calc(u); for(RG int e = ft[u]; e != -1; e = edge[e].nt) if(Calc(edge[e].to) < tmp) return Query(edge[e].rt); return tmp;}int main(RG int argc, RG char* argv[]){ sz = n = Read(); Q = Read(); Fill(fst, -1); Fill(ft, -1); for(RG int i = 1, a, b, c; i < n; ++i) a = Read(), b = Read(), c = Read(), Add(a, b, c), Add(b, a, c); ChainDiv::Dfs1(1); ChainDiv::Dfs2(1, 1); mx[0] = n + 1; cnt = 0; Getroot(1, 0); root = rt; Create(rt, 0); while(Q--){ RG int u = Read(), d = Read(); Modify(u, d); printf("%lld\n", Query(root)); } return 0;}

转载于:https://www.cnblogs.com/cjoieryl/p/8278299.html

你可能感兴趣的文章
csa Round #66 (Div. 2 only)
查看>>
BIT+DP
查看>>
智能指针
查看>>
day32--面向对象的程序设计之继承实现的原理(继承顺序)、封装、property
查看>>
虚拟机全屏问题
查看>>
942. DI String Match
查看>>
spring
查看>>
java_oop_方法2
查看>>
tomcat集群
查看>>
java_生态环境
查看>>
笔记-人老了-github
查看>>
https域名强弱校验的区别
查看>>
MariaDB 10.3 instant ADD COLUMN亿级大表毫秒级加字段
查看>>
堆结构导致数据文件不能收缩
查看>>
linux运维常见英文报错中文翻译(菜鸟必知)
查看>>
微软私有云Azure Pack实践系列之三创建虚拟机角色
查看>>
Exchange 2010 UM角色安装后无法启动服务,错误 1000,1001
查看>>
运维的核心竞争力是什么
查看>>
.NET Micro Framework开发板用户简明手册(v3.0)
查看>>
Gartner:智能SOC/情报驱动的SOC的五大特征
查看>>