luogu P3345 [ZJOI2015] township fantasy strategy game | dynamic point divide and conquer

Title Description

Fragrance playfully girls are playing a very interesting strategy games, this game would have really not too much of a map, pipe fragrance can get through, but I do not know why the game of online vendors map bigger and bigger, to As a fragrance simply to see, however, let alone others fought.

Before the war, fragrance now faced with a very basic management issues need to be addressed. The entire map is a tree, a total of n open areas, which open spaces are the n-1 sides of the right connecting strip, so that there is a unique path between each connecting these two points.

In the game, fragrance may increase or decrease the number of troops on the ground. Meanwhile, a fragrance may be placed on a depot space. If a staging post on the point u, and there is open space dv v units of the army, the fragrance will have to spend dv * dist (u, v) money every day to supply these forces.

Since all of the forces fragrance needs to be replenished, and therefore would cost a total of fragrance Sigma (Dv * dist (u, v), where 1 <= V <= N) cost. Where dist (u, v) represents a u-th (right and unique path) v at a distance tree.

Because the provisions of the game, fragrance can select only one open space as a staging post. In the course of the game, fragrance may create some troops in some open space, but also may reduce some of the open space on the army carried out such operations in the future, due to economic considerations, fragrance can often move his supply station thus save some money.

However, due to the game map is too big, fragrance can not easily be optimal arrangement, can you help her? You can assume that the army did not start on all the open space.

Input Format

The first line of the two numbers n and Q represent the tree points and the number of operations fragrance, wherein from 1 to n reference points. Next, n-1 lines, each line three positive integers a, b, c, a and b indicates there is an edge between the right side of c. Next Q lines of two numbers U, e, e represents the fragrance unit put on a military point u (if e <0, the equivalent of reducing the fragrance on U | e | a military unit, said white It is du ← du + e). Data on the number of troops to ensure that any time each point is non-negative.

Output Format

For each operation, after the output operation is completed, the minimum cost per day fragrance, i.e., fragrance, if it takes time to select the optimal supply point replenished.


Explanations from

Built dotted tree, we dotted every rule in his father's center of gravity is set to focus on the level of

Each query just from the root node (the center of gravity of the entire tree) began to see which (the original tree) son better, if gifted son go out into the center of gravity of the corresponding sub-tree walk

Then consider changes only in his own ancestry modified tree dotted on the line

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
typedef long long LL;
template<typename T>inline void read(T &num) {
    char ch; int flg = 1;
    while((ch=getchar())<'0'||ch>'9')if(ch=='-')flg=-flg;
    for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
    num*=flg;
}
const int MAXN = 100005;
int n, q, fir[MAXN], cnt;
struct edge { int to, nxt, w; }e[MAXN<<1];
inline void add(int u, int v, int wt) {
    e[cnt] = (edge){ v, fir[u], wt }, fir[u] = cnt++;
    e[cnt] = (edge){ u, fir[v], wt }, fir[v] = cnt++;
}
int dis[MAXN], son[MAXN], sz[MAXN], top[MAXN], fa[MAXN], dep[MAXN];
inline void dfs(int u, int ff) {
    dep[u] = dep[fa[u]=ff] + (sz[u]=1);
    for(int i = fir[u], v; ~i; i = e[i].nxt)
        if((v=e[i].to) != ff) {
            dis[v] = dis[u] + e[i].w;
            dfs(v, u), sz[u] += sz[v];
            if(sz[v] > sz[son[u]]) son[u] = v;
        }
}
inline void dfs2(int u,int tp){
    top[u]=tp;
    if(son[u])dfs2(son[u],tp);
    for(int i = fir[u], v; ~i; i = e[i].nxt)
        if((v=e[i].to) != fa[u] && v != son[u])
            dfs2(v,v);
}
inline int lca(int u,int v){
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        u=fa[top[u]];
    }
    return dep[u]<dep[v]?u:v;
}
inline int dist(int u,int v){
    return dis[u]+dis[v]-2*dis[lca(u,v)];
}
int Fa[MAXN],size[MAXN],Size,Minr,root,info[MAXN],CNT;
struct EDGE { int to, nxt, rt; }E[MAXN];
inline void ADD(int u,int v,int rr){
    E[CNT]=(EDGE){v,info[u],rr},info[u]=CNT++;
}
bool vis[MAXN];
void Getrt(int u, int ff) {
    size[u] = 1;
    int ret = 0;
    for(int i = fir[u], v; ~i; i = e[i].nxt)
        if((v=e[i].to) != ff && !vis[v]) {
            Getrt(v, u), size[u] += size[v];
            ret = max(ret, size[v]);
        }
    ret = max(ret, Size-size[u]);
    if(ret < Minr) Minr = ret, root = u;
}
inline void DFS(int u,int ff){
    vis[u]=1; Fa[u]=ff;
    for(int i = fir[u], v; ~i; i = e[i].nxt)
        if(!vis[v=e[i].to]) {
            Minr = n; Size = size[v];
            Getrt(v, u);
            ADD(u, v, root);
            DFS(root, u);
        }
}
LL sum[MAXN],sumd[MAXN],sumf[MAXN];
inline void Modify(int u,int val){
    sum[u]+=val;
    for(int i = u; Fa[i]; i = Fa[i]) {
        int len = dist(u, Fa[i]);
        sum[Fa[i]] += val;
        sumd[Fa[i]] +=val * len;
        sumf[i] +=val * len;
    }
}
inline LL Count(int u){
    LL res=sumd[u];
    for(int i=u;Fa[i];i=Fa[i]){
        int len=dist(u,Fa[i]);
        res+=(sum[Fa[i]]-sum[i])*len;
        res+=(sumd[Fa[i]]-sumf[i]);
    }
    return res;
}
inline LL Query(int u) {
    LL tmp = Count(u);
    for(int i = info[u]; ~i; i = E[i].nxt)
        if(Count(E[i].to) < tmp) return Query(E[i].rt);
    return tmp;
}
signed main(){
    memset(fir, -1, sizeof fir); memset(info, -1, sizeof info);
    read(n), read(q);
    for(int i = 1, x, y, z; i < n; ++i) read(x), read(y), read(z), add(x, y, z);
    dfs(1, 0), dfs2(1, 1);
    Size = Minr = n; Getrt(1, 0);
    int RT = root; 
    DFS(root, 0);
    int x, y;
    while(q--) {
        read(x), read(y);
        Modify(x, y);
        printf("%lld\n", Query(RT));
    }
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/12123700.html