zoukankan      html  css  js  c++  java
  • bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集

    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=4399

    题解

    毒瘤题 (9) 种操作还有支持动态图的连通性

    仔细读题 $ c<=7$。

    哈只要加边那么动态图就变成了维护集合了。


    考虑每一个集合怎们维护。

    对于操作 (1),直接加入就可以了。

    对于操作 (2),合并两个集合的时候直接线段树合并。注意判断两个集合是否已经联通。

    对于操作 (3),我们取出小于 (x) 的数的个数,然后把小于 (x) 的数全部清空,把个数加到 (x) 上去。

    对于操作 (4),与 (3) 同理。

    对于操作 (5),线段树二分。

    对于操作 (6),如果直接维护积的话肯定会炸 long long。所以我们可以维护它们取对数以后的值,求和维护即可。比较的时候直接比较对数和就可以了。对数底数的话任选。

    对于操作 (7),可以直接维护出来。

    对于操作 (8, 9),动态图


    时间复杂度 (O(mlog max_{val}))

    注意:此题卡空间!!调整好变量类型和数组大小!!!

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 400000 + 7;
    const int INF = 1000000000;
    
    int n, m, nod;
    int rt[N], fa[N];
    
    struct Node {
    	int val, lc, rc;
    	bool tag;
    	double sum;
    } t[N * 17];
    
    inline void pushup(int o) {
    	if (t[o].tag) return t[o].val = t[o].sum = 0, (void)0;
    	t[o].val = t[t[o].lc].val + t[t[o].rc].val;
    	t[o].sum = t[t[o].lc].sum + t[t[o].rc].sum;
    }
    inline void pushdown(int o) {
    	if (!t[o].tag) return;
    	if (t[o].lc) t[t[o].lc].tag = 1, t[t[o].lc].val = 0, t[t[o].lc].sum = 0;
    	if (t[o].rc) t[t[o].rc].tag = 1, t[t[o].rc].val = 0, t[t[o].rc].sum = 0;
    	t[o].tag = 0;
    }
    inline void qadd(int &o, int L, int R, int x, int k) {
    	if (!k) return;
    	if (!o) o = ++nod;
    	if (L == R) return t[o].val += k, t[o].sum += k * log(L), (void)0;
    	int M = (L + R) >> 1;
    	pushdown(o);
    	if (x <= M) qadd(t[o].lc, L, M, x, k);
    	else qadd(t[o].rc, M + 1, R, x, k);
    	pushup(o);
    }
    inline void qset(int o, int L, int R, int l, int r) {
    	if (!o || !t[o].val || l > r) return;
    	if (l <= L && R <= r) return t[o].tag = 1, t[o].val = 0, t[o].sum = 0, (void)0;
    	int M = (L + R) >> 1;
    	pushdown(o);
    	if (l <= M) qset(t[o].lc, L, M, l, r);
    	if (r > M) qset(t[o].rc, M + 1, R, l, r);
    	pushup(o);
    }
    inline int qsum(int o, int L, int R, int l, int r) {
    	if (!o || !t[o].val || l > r) return 0;
    	if (l <= L && R <= r) return t[o].val;
    	int M = (L + R) >> 1;
    	pushdown(o);
    	if (r <= M) return qsum(t[o].lc, L, M, l, r);
    	if (l > M) return qsum(t[o].rc, M + 1, R, l, r);
    	return qsum(t[o].lc, L, M, l, r) + qsum(t[o].rc, M + 1, R, l, r);
    }
    inline int qval(int o, int L, int R, int k) {
    	if (L == R) return L;
    	int M = (L + R) >> 1;
    	pushdown(o);
    	if (k <= t[t[o].lc].val) return qval(t[o].lc, L, M, k);
    	else return qval(t[o].rc, M + 1, R, k - t[t[o].lc].val);
    }
    inline int merge(int o, int p) {
    	if (!o || !p) return o ^ p;
    	pushdown(o), pushdown(p);
    	t[o].lc = merge(t[o].lc, t[p].lc);
    	t[o].rc = merge(t[o].rc, t[p].rc);
    	t[o].val += t[p].val, t[o].sum += t[p].sum;
    	return o;
    }
    inline void newnode(int v) { qadd(rt[++n], 1, INF, v, 1), assert(t[rt[n]].val == 1); }
    
    inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
    
    inline void work() {
    	while (m--) {
    		int opt, x, y;
    		read(opt);
    		if (opt == 1) {
    			read(x);
    			newnode(x);
    			fa[n] = n;
    		} else if (opt == 2) {
    			read(x), read(y);
    			x = find(x), y = find(y);
    			if (x == y) continue;
    			fa[y] = x;
    			rt[x] = merge(rt[x], rt[y]);
    		} else if (opt == 3) {
    			read(x), read(y);
    			x = find(x);
    			int cnt = qsum(rt[x], 1, INF, 1, y - 1);
    			qset(rt[x], 1, INF, 1, y - 1);
    			qadd(rt[x], 1, INF, y, cnt);
    		} else if (opt == 4) {
    			read(x), read(y);
    			x = find(x);
    			int cnt = qsum(rt[x], 1, INF, y + 1, INF);
    			qset(rt[x], 1, INF, y + 1, INF);
    			qadd(rt[x], 1, INF, y, cnt);
    		} else if (opt == 5) {
    			read(x), read(y);
    			x = find(x);
    			printf("%d
    ", qval(rt[x], 1, INF, y));
    		} else if (opt == 6) {
    			read(x), read(y);
    			x = find(x), y = find(y);
    			if (t[rt[x]].sum > t[rt[y]].sum) puts("1");
    			else puts("0");
    		} else if (opt == 7) {
    			read(x);
    			x = find(x);
    			printf("%d
    ", t[rt[x]].val);
    		}
    	}
    }
    
    inline void init() {
    	read(m);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    作业七:用户体验设计案例分析
    作业五:需求分析
    作业四:结对编程 词频统计
    作业四:结对编程,词频统计
    作业三:词频统计
    作业2
    Github注册过程
    ArrayList的说明及简单用法
    Java类中成员变量、局部变量、静态变量的区别
    AspNetCore.SignalR的JwtBearer认证
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4399.html
Copyright © 2011-2022 走看看