[Luogu] 网络

https://www.luogu.org/problemnew/show/P3250

树链剖分 + 线段树 + 优先队列
要求未被影响的请求中最大的
所以每次将每条路径在整棵树上的补集的每个节点的优先队列加入该路径的权值

mid 敲错调了一上午

气炸。。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>

using namespace std;
const int N = 1e5 + 10;

#define yxy getchar()
#define lson jd << 1
#define rson jd << 1 | 1  

#define RR freopen("gg.in", "r", stdin)

int n, Ty, Tim, how, ans;
int fa[N], topp[N], deep[N], size[N], son[N], tree[N];
priority_queue <int> Q1[N << 2], Q2[N << 2];
vector <int> vec[N << 1];
struct Node{int up, down;} D[N];
struct Node2{int l, r, val;} Ask[N << 1];

inline int read() {
    int x = 0; char c = yxy;
    while(c < '0' || c > '9') c = yxy;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
    return x;
}

void Dfs_son(int u, int f_, int dep) {
    fa[u] = f_;
    deep[u] = dep;
    size[u] = 1;
    int siz_ = vec[u].size();                                                                                                  
    for(int i = 0; i < siz_; i ++) {
        int v = vec[u][i];
        if(v != f_) {
            Dfs_son(v, u, dep + 1);
            size[u] += size[v];
            if(size[v] > size[son[u]]) son[u] = v;
        }
    }
} 

void Dfs_un(int u, int tp) {
    topp[u] = tp;
    tree[u] = ++ Tim;
    if(!son[u]) return ;
    Dfs_un(son[u], tp);
    int siz_ = vec[u].size();
    for(int i = 0; i < siz_; i ++) {
        int v = vec[u][i];
        if(v != fa[u] && v != son[u]) Dfs_un(v, v);
    }
}

inline bool cmp(Node a, Node b) {return a.up < b.up;}

void Sec_G(int l, int r, int jd, int x, int y, int yj) {
    if(x <= l && r <= y) {
        if(how == 1) Q1[jd].push(yj);
        else Q2[jd].push(yj);
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
    if(y > mid)  Sec_G(mid + 1, r, rson, x, y, yj);
}
 
inline void Sec_G_imp(int x, int y, int val) {
    int tp1 = topp[x], tp2 = topp[y], js(0);
    while(tp1 != tp2) {
        if(deep[tp1] < deep[tp2]) swap(x, y), swap(tp1, tp2);
        D[++ js].up = tree[tp1];
        D[js].down = tree[x];
        x = fa[tp1];
        tp1 = topp[x];
    }
    if(deep[x] < deep[y]) swap(x, y);
    D[++ js].up = tree[y]; D[js].down = tree[x];
    sort(D + 1, D + js + 1, cmp);
    int last = 0;
    for(int i = 1; i <= js; i ++) {
        int l_ = last + 1, r_ = D[i].up - 1;
        if(l_ <= r_) Sec_G(1, n, 1, l_, r_, val);
        last = D[i].down;
    }
    int l_ = last + 1, r_ = n;
    if(l_ <= r_) Sec_G(1, n, 1, l_, r_, val);
}

int get_answer(int jd) {
    int ret = -1;
    while(!Q2[jd].empty() && (Q1[jd].top() == Q2[jd].top())) Q1[jd].pop(), Q2[jd].pop();
    if(Q1[jd].empty()) return -1;
    return Q1[jd].top();
}

void Sec_A(int l, int r, int jd, int x) {
    ans = max(ans, get_answer(jd));
    if(l == r) return ;
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_A(l, mid, lson, x);
    else Sec_A(mid + 1, r, rson, x);
}

int main() {
    n = read();
    Ty = read();
    for(int i = 1; i < n; i ++) {
        int u = read(), v = read();
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    Dfs_son(1, 0, 1);
    Dfs_un(1, 1);
    for(int i = 1; i <= Ty; i ++) {
        int opt = read();
        if(opt == 0) { //添加 
            Ask[i].l = read(), Ask[i].r = read(), Ask[i].val = read();
            how = 1;
            Sec_G_imp(Ask[i].l, Ask[i].r, Ask[i].val);
        } else if(opt == 1) { //删除 
            int x = read();
            how = -1;
            Sec_G_imp(Ask[x].l, Ask[x].r, Ask[x].val);
        } else { //查询
            int x = read();
            ans = -1;
            Sec_A(1, n, 1, tree[x]);
            cout << ans << endl;
        }
    }
    return 0;
}

 

posted @ 2018-02-07 14:14  xayata  阅读(469)  评论(0编辑  收藏  举报