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

loj#2049. 「HNOI2016」网络(set 树剖 暴力)

程序员文章站 2022-12-14 21:55:29
题意 "题目链接" Sol 下面的代码是$O(nlog^3n)$的暴力。 因为从一个点向上只会跳$logn$次,所以可以暴力的把未经过的处理出来然后每个点开个multiset维护最大值 cpp include define Pair pair define MP(x, y) make_pair(x, ......

题意

sol

下面的代码是\(o(nlog^3n)\)的暴力。

因为从一个点向上只会跳\(logn\)次,所以可以暴力的把未经过的处理出来然后每个点开个multiset维护最大值

#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 = 2e5 + 10, ss = maxn * 4, 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 n, q, fa[maxn], siz[maxn], son[maxn], id[maxn], top[maxn], dep[maxn], times;
vector<int> v[maxn];
void dfs1(int x, int _fa) {
    siz[x] = 1; dep[x] = dep[_fa] + 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] = ++times;
    if(!son[x]) return ;
    dfs2(son[x], topf);
    for(auto &to : v[x]) {
        if(top[to]) continue;
        dfs2(to, to);
    }
}
multiset<int> s[ss];
struct query {
    int a, b, v;
}q[maxn];
vector<pair> line[maxn];
int ls[ss], rs[ss], root, tot;
void erase(multiset<int> &s, int v) {
    auto it = s.find(v);
    if(it != s.end()) s.erase(it);
}
void get(vector<pair> &v, int x, int y) {
    vector<pair> tmp;
    while(top[x] ^ top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        tmp.push_back({id[top[x]], id[x]});
        x = fa[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    tmp.push_back({id[x], id[y]});
    sort(tmp.begin(), tmp.end());
    int las = 1;
    for(auto x : tmp) {
        if(las <= x.fi - 1) v.push_back({las, x.fi - 1});
        las = x.se + 1;
    }
    if(las <= n) v.push_back({las, n});
}

int mx(multiset<int> &s) {
    if(s.empty()) return -1;
    auto it = s.end(); it--;
    return *it;
}
void intadd(int &k, int l, int r, int ql, int qr, int v, int opt) {
    if(!k) k = ++tot;
    if(ql <= l && r <= qr) {
        if(opt == 1) s[k].insert(v); 
        else erase(s[k], v);
        return ;
    }
    int mid = l + r >> 1;
    if(ql <= mid) intadd(ls[k], l, mid, ql, qr, v, opt);
    if(qr  > mid) intadd(rs[k], mid + 1, r, ql, qr, v, opt);
}
int query(int k, int l, int r, int p) {
    if(!k) return -1;
    int ans = mx(s[k]), mid = l + r >> 1;
    if(l == r) return mx(s[k]);
    if(p <= mid) chmax(ans, query(ls[k], l, mid, p));
    else chmax(ans, query(rs[k], mid + 1, r, p));
    return ans;
}
void treeadd(int x, int y, int v, int opt) {
    while(top[x] ^ top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        intadd(root, 1, n, id[top[x]], id[x], v, opt);
        x = fa[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    intadd(root, 1, n, id[x], id[y], v, opt);
}
void add(int ti, int opt) {
    int x = q[ti].a, y = q[ti].b, v = q[ti].v;
    if(opt == 1) get(line[ti], x, y);
    for(auto x : line[ti]) 
        intadd(root, 1, n, x.fi, x.se, v, opt);
}
signed main() {
//  fin(a); fout(b);
    n = read(); q = 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 <= q; i++) {
        int opt = read();
        if(opt == 0) {
            int a = read(), b = read(), v = read(); q[i] = {a, b, v};
            add(i, 1); 
        } else if(opt == 1) {
            int ti = read();
            add(ti, -1); 
        } else if(opt == 2) {
            int x = read();
            printf("%d\n", query(root, 1, n, id[x]));
        }
    }
    return 0;
}