欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

程序员文章站 2022-05-27 22:30:07
题意 "题目链接" Sol 树链剖分板子 + 动态开节点线段树板子 cpp include define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second // define int long lon ......

题意

题目链接

sol

树链剖分板子 + 动态开节点线段树板子

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define ll long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int ch(int x, int l, int r) {
    return x >= l && x <= r;
}
int n, q, c[maxn], w[maxn], dep[maxn], fa[maxn], son[maxn], siz[maxn], top[maxn], id[maxn], cnt;
vector<int> v[maxn];
void dfs1(int x, int _fa) {
    dep[x] = dep[_fa] + 1; siz[x] = 1; fa[x] = _fa; 
    for(auto &to : v[x]) {
        if(to == _fa) continue;
        dfs1(to, x);
        siz[x] += siz[to];
        if(siz[to] > siz[son[x]]) son[x] = to;
    }
}
void dfs2(int x, int topf) {
    top[x] = topf; id[x] = ++cnt;
    if(!son[x]) return ;
    dfs2(son[x], topf);
    for(auto &to : v[x]) {
        if(top[to]) continue;
        dfs2(to, to);
    }
}
const int ss = (3e6 + 10);
int root[maxn], ls[ss], rs[ss], mx[ss], sum[ss], tot;
void update(int k) {
    mx[k] = max(mx[ls[k]], mx[rs[k]]);
    sum[k] = sum[ls[k]] + sum[rs[k]];
}
void modify(int &k, int l, int r, int p, int v) {
    if(!k) k = ++tot;
    if(l == r) {sum[k] = v, mx[k] = v; return ;}
    int mid = l + r >> 1;
    if(p <= mid) modify(ls[k], l, mid, p, v);
    if(p  > mid) modify(rs[k], mid + 1, r, p, v);
    update(k);
}
int query(int k, int l, int r, int ql, int qr, int opt) {
    if(ql <= l && r <= qr) return opt == 0 ? mx[k] : sum[k];
    int mid = l + r >> 1;
    if(ql > mid) return query(rs[k], mid + 1, r, ql, qr, opt);
    else if(qr <= mid) return query(ls[k], l, mid, ql, qr, opt);
    else return opt == 0 ? max(query(ls[k], l, mid, ql, qr, opt), query(rs[k], mid + 1, r, ql, qr, opt)) 
                         : query(ls[k], l, mid, ql, qr, opt) + query(rs[k], mid + 1, r, ql, qr, opt);
}
int treemax(int x, int y) {
    int ans = -inf, p = x;
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        chmax(ans, query(root[c[p]], 1, n, id[top[x]], id[x], 0));
        x = fa[top[x]];
    }
    if(dep[x] < dep[y]) swap(x, y);
    chmax(ans, query(root[c[p]], 1, n, id[y], id[x], 0));
    return ans;
}
int treesum(int x, int y) {
    int ans = 0, p = x;
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        ans += query(root[c[p]], 1, n, id[top[x]], id[x], 1);
        x = fa[top[x]];
    }
    if(dep[x] < dep[y]) swap(x, y);
    ans += query(root[c[p]], 1, n, id[y], id[x], 1);
    return ans;
}
signed main() {
    n = read(); q = read();
    for(int i = 1; i <= n; i++) w[i] = read(), c[i] = read();
    for(int i = 1; i <= n - 1; i++) {
        int x = read(), y = read();
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    for(int i = 1; i <= n; i++) 
        modify(root[c[i]], 1, n, id[i], w[i]);
    while(q--) {
        char s[4];
        scanf("%s", s);
        int x = read(), c = read();
        if(s[0] == 'c' && s[1] == 'c') {
            modify(root[c[x]], 1, n, id[x], 0);
            modify(root[c], 1, n, id[x], w[x]);
            c[x] = c;
        }
        if(s[0] == 'c' && s[1] == 'w') {
            modify(root[c[x]], 1, n, id[x], c);
            w[x] = c;
        }
        if(s[0] == 'q' && s[1] == 'm') {
            printf("%d\n", treemax(x, c));  
        }
        if(s[0] == 'q' && s[1] == 's') {
            printf("%d\n", treesum(x, c));
        }
    }
    return 0;
}
/*
5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
qs 1 5
cc 3 1
qs 1 5
cw 3 3
qs 1 5
qm 2 4
*/