首页 技术 正文
技术 2022年11月7日
0 收藏 652 点赞 529 浏览 4148 个字

Description

傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来,更别说和别人打仗了。 在打仗之前,幽香现在面临一个非常基本的管理问题需要解决。 整个地图是一个树结构,一共有n块空地,这些空地被n-1条带权边连接起来,使得每两个点之间有一条唯一的路径将它们连接起来。在游戏中,幽香可能在空地上增加或者减少一些军队。同时,幽香可以在一个空地上放置一个补给站。 如果补给站在点u上,并且空地v上有dv个单位的军队,那么幽香每天就要花费dv×dist(u,v)的金钱来补给这些军队。由于幽香需要补给所有的军队,因此幽香总共就要花费为Sigma(Dv*dist(u,v),其中1<=V<=N)的代价。其中dist(u,v)表示u个v在树上的距离(唯一路径的权和)。 因为游戏的规定,幽香只能选择一个空地作为补给站。在游戏的过程中,幽香可能会在某些空地上制造一些军队,也可能会减少某些空地上的军队,进行了这样的操作以后,出于经济上的考虑,幽香往往可以移动他的补给站从而省一些钱。但是由于这个游戏的地图是在太大了,幽香无法轻易的进行最优的安排,你能帮帮她吗? 你可以假定一开始所有空地上都没有军队。

PDF版试题:JudgeOnline/upload/201708/zjoi2015d1.pdf

Input

第一行两个数n和Q分别表示树的点数和幽香操作的个数,其中点从1到n标号。 接下来n-1行,每行三个正整数a,b,c,表示a和b之间有一条边权为c的边。 接下来Q行,每行两个数u,e,表示幽香在点u上放了e单位个军队(如果e<0,就相当于是幽香在u上减少了|e|单位个军队,说白了就是du←du+e)。数据保证任何时刻每个点上的军队数量都是非负的。 1<=c<=1000, 0<=|e|<=1000, n<=10^5, Q<=10^5对于所有数据,这个树上所有点的度数都不超过20N,Q>=1

Output

对于幽香的每个操作,输出操作完成以后,每天的最小花费,也即如果幽香选择最优的补给点进行补给时的花费。

Sample Input

10 5
1 2 1
2 3 1
2 4 1
1 5 1
2 61
2 7 1
5 8 1
7 91
1 10 1
3 1
2 1
8 1
3 1
4 1

Sample Output

0
1
4
5
6

题解

其实和[HNOI 2015]开店的动态点分统计答案的方法类似,不再赘述。

这里主要讲如何找到“带权重心”。

我们每次从点分的根开始遍历与它相邻的所有点,统计出他们的答案,值得肯定的是这些值只会存在两种情况:

1. 相邻的所有值都大于这个点的答案,显然这个点就是要求的点;

2. 相邻的点只有一个的值,小于这个点统计出的答案,就直接向这棵更小的子树走,注意是走到“分治树”的下一个重心,继续操作。

 //It is made by Awson on 2018.1.9
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define lowbit(x) ((x)&(-(x)))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
const int N = 1e5;
const int INF = ~0u>>;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int n, q, u, v, c, fa[N+], G;
LL sum[N+], dis1[N+], dis2[N+];
struct tt {
int to, next, cost;
}G1edge[(N<<)+], G2edge[N+];
int G1path[N+], G1top, G2path[N+], G2top;
void G1add(int u, int v, int c) {
G1edge[++G1top].to = v, G1edge[G1top].cost = c, G1edge[G1top].next = G1path[u], G1path[u] = G1top;
}
void G2add(int u, int v, int c) {
G2edge[++G2top].to = v, G2edge[G2top].cost = c, G2edge[G2top].next = G2path[u], G2path[u] = G2top;
}
namespace LCA {
int lim, bin[], dfn[N+], tim, logn[(N<<)+];
LL f[(N<<)+][];
void dfs(int o, LL cost, int father) {
f[dfn[o] = ++tim][] = cost;
for (int i = G1path[o]; i; i = G1edge[i].next)
if (G1edge[i].to != father) dfs(G1edge[i].to, cost+G1edge[i].cost, o), f[++tim][] = cost;
}
LL query(int x, int y) {
if (dfn[x] > dfn[y]) Swap(x, y);
int lim = logn[dfn[y]-dfn[x]+];
return Min(f[dfn[x]][lim], f[dfn[y]-bin[lim]+][lim]);
}
LL dist(int x, int y) {return f[dfn[x]][]+f[dfn[y]][]-(query(x, y)<<); }
void main() {
lim = log(n<<)/log(); bin[] = ; for (int i = ; i <= ; i++) bin[i] = bin[i-]<<;
logn[] = -; for (int i = ; i <= (n<<); i++) logn[i] = logn[i>>]+;
dfs(, , );
for (int t = ; t <= lim; t++) for (int i = ; i+bin[t]- <= (n<<); i++) f[i][t] = Min(f[i][t-], f[i+bin[t-]][t-]);
}
}
namespace Point_divide {
int size[N+], mx[N+], vis[N+], minsize, root;
void get_size(int o, int fa) {
size[o] = , mx[o] = ;
for (int i = G1path[o]; i; i = G1edge[i].next)
if (G1edge[i].to != fa && !vis[G1edge[i].to]) {
get_size(G1edge[i].to, o);
size[o] += size[G1edge[i].to];
if (size[G1edge[i].to] > mx[o]) mx[o] = size[G1edge[i].to];
}
}
void get_root(int o, int pa, int fa) {
mx[o] = Max(mx[o], size[pa]-size[o]);
if (minsize > mx[o]) minsize = mx[o], root = o;
for (int i = G1path[o]; i; i = G1edge[i].next)
if (G1edge[i].to != fa && !vis[G1edge[i].to]) get_root(G1edge[i].to, pa, o);
}
void work(int o, int pa) {
minsize = INF; get_size(o, ), get_root(o, o, );
vis[root] = ; fa[root] = pa; int rt = root; G2add(pa, root, o);
for (int i = G1path[root]; i; i = G1edge[i].next)
if (!vis[G1edge[i].to]) work(G1edge[i].to, rt);
G = rt;
}
void main() {work(, ); }
} void update(int o, int c) {
for (int x = o; x; x = fa[x]) {
sum[x] += c;
dis1[x] += (LL)c*LCA::dist(x, o);
if (fa[x]) dis2[x] += (LL)c*LCA::dist(fa[x], o);
}
}
LL get_ans(int o) {
LL ans = ;
for (int x = o; x; x = fa[x]) {
ans += dis1[x]+sum[x]*LCA::dist(x, o);
if (fa[x]) ans -= sum[x]*LCA::dist(fa[x], o)+dis2[x];
}
return ans;
}
LL query(int o) {
LL ans = get_ans(o);
for (int i = G2path[o]; i; i = G2edge[i].next) {
LL tmp = get_ans(G2edge[i].cost);
if (tmp < ans) return query(G2edge[i].to);
}
return ans;
}
void work() {
read(n), read(q);
for (int i = ; i < n; i++) read(u), read(v), read(c), G1add(u, v, c), G1add(v, u, c);
LCA::main(); Point_divide::main();
while (q--) {
read(u), read(c); update(u, c);
printf("%lld\n", query(G));
}
}
int main() {
work();
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902