zoukankan      html  css  js  c++  java
  • loj#2049. 「HNOI2016」网络(set 树剖 暴力)

    题意

    题目链接

    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 << '
    ';}
    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
    ", Query(root, 1, N, id[x]));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    fastDFS与nginx整合2
    fastDFS分布式文件系统
    NIO编程
    Nginx正向代理与反向代理
    JAVA序列化
    FileUpload问题
    测试覆盖率实现技术
    Hutool 功能特色:
    自建右键服务器
    20191123-SWITCH后面跟的参数不能为string类型
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10472881.html
Copyright © 2011-2022 走看看