Problem Summary league after learning phase

2019.12.1 ~ 2019.12.7

"Luogu4556" Vani have a date - rain tail

Portal
apparent difference can be considered a tree + tub, is updated each time a point on this chain strand is marked with number 1 in the position corresponding to the barrel,
and finally the position of the tub to take maximum non-zero value for each point as an answer we can, if all output is 0 to 0, so the time complexity and space complexity are \ (O (nm) \)
consider this one optimization algorithm:
we consider the use of weights segment tree instead of barrels
we can consider tree cross way to update a chain, then it becomes a tree for each segment in the range interval difference log segment,
important to note that, we have to play a positive mark on that side of the low depth, because we can follow \ (\ text {dfs} \ ) order Operators answer
little proof:

  • Do not ask for contributions to the current point, this is clearly not affected
  • Contribution to the current point of inquiry, its contribution must be to lay back marked vertex heavy chains, it will certainly be the end of the heavy chain of light elimination son

Such an action would have been the answer statistical method for off-line algorithm is similar, so, as long as the tree line will open a
Code:

#include <cstdio>
#include <vector>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 1e5 + 5;

int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }

int n, m, ans[_]; vector < int > vec[_];
int dep[_], siz[_], son[_], fa[_];
int dfn[_], rev[_], top[_];
struct node { int mx, pos; } t[_ << 2];

inline int lc(int p) { return p << 1; }

inline int rc(int p) { return p << 1 | 1; }

inline void pushup(int p) {
    if (t[lc(p)].mx >= t[rc(p)].mx)
        t[p].mx = t[lc(p)].mx, t[p].pos = t[lc(p)].pos;
    else
        t[p].mx = t[rc(p)].mx, t[p].pos = t[rc(p)].pos;
}

inline void build(int p = 1, int l = 1, int r = 100000) {
    if (l == r) { t[p].mx = 0, t[p].pos = l; return ; }
    int mid = (l + r) >> 1;
    build(lc(p), l, mid), build(rc(p), mid + 1, r), pushup(p);
}

inline void update(int x, int v, int p = 1, int l = 1, int r = 100000) {
    if (l == r) { t[p].mx += v; return ; }
    int mid = (l + r) >> 1;
    if (x <= mid) update(x, v, lc(p), l, mid);
    else update(x, v, rc(p), mid + 1, r);
    pushup(p);
}

inline void uptRange(int x, int y, int z) {
    int fx = top[x], fy = top[y];
    while (fx != fy) {
        if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
        vec[dfn[fx]].push_back(z), vec[dfn[x] + 1].push_back(-z);
        x = fa[fx], fx = top[x];
    }
    if (dep[x] > dep[y]) swap(x, y);
    vec[dfn[x]].push_back(z), vec[dfn[y] + 1].push_back(-z);
}

inline void dfs(int u, int f) {
    siz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
    for (rg int i = head[u]; i; i = nxt[i]) {
        int v = ver[i]; if (v == f) continue;
        dfs(v, u), siz[u] += siz[v];
        if (siz[v] > siz[son[u]]) son[u] = v;
    }
}

inline void dfs(int u, int f, int topf) {
    top[rev[dfn[u] = ++dfn[0]] = u] = topf;
    if (son[u]) dfs(son[u], u, topf);
    for (rg int i = head[u]; i; i = nxt[i]) {
        int v = ver[i]; if (v == f || v == son[u]) continue;
        dfs(v, u, v);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    file("cpp");
#endif
    read(n), read(m);
    for (rg int u, v, i = 1; i < n; ++i)
        read(u), read(v), Add_edge(u, v), Add_edge(v, u);
    dfs(1, 0), dfs(1, 0, 1), build();
    for (rg int x, y, z; m--; ) read(x), read(y), read(z), uptRange(x, y, z);
    for (rg int i = 1; i <= n; ++i) {
        for (rg int j : vec[i]) if (j > 0) update(j, 1); else update(-j, -1);
        ans[rev[i]] = t[1].mx > 0 ? t[1].pos : 0;
    }
    for (rg int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zsbzsb/p/11966045.html