• UVa 1455 Kingdom 线段树 并查集


    题意:

    平面上有(n)个点,有一种操作和一种查询:

    • (road \, A \, B):在(a)(b)两点之间加一条边
    • (line C):询问直线(y=C)经过的连通分量的个数以及这些连通分量点的总数

    分析:

    其实横坐标是没用的,首先可以先将纵坐标离散化。
    用并查集维护点的连通性,连通分量的大小以及连通分量中纵坐标的最大值和最小值。

    线段树中维护的是每条直线穿过的连通分量的个数以及点的个数之和。
    当两个连通分量合并时,先把两个小连通分量从线段树中删去,然后再把合并之后的大连通分量加进去。
    修改是区间修改,查询是单点查询。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 100000 + 10;
    const int maxnode = maxn * 4;
    
    int n;
    int a[maxn], tot, b[maxn];
    
    int pa[maxn], sz[maxn], miny[maxn], maxy[maxn];
    int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }
    
    int states[maxnode], cities[maxnode];
    
    char cmd[15];
    
    void pushdown(int o, int* add) {
    	if(add[o] != 0) {
    		add[o<<1] += add[o];
    		add[o<<1|1] += add[o];
    		add[o] = 0;
    	}
    }
    
    void pushdown(int o) {
    	pushdown(o, states);
    	pushdown(o, cities);
    }
    
    void update(int o, int L, int R, int qL, int qR, int v1, int v2) {
    	if(qL <= L && R <= qR) {
    		states[o] += v1;
    		cities[o] += v2;
    		return;
    	}
    	pushdown(o);
    	int M = (L + R) / 2;
    	if(qL <= M) update(o<<1, L, M, qL, qR, v1, v2);
    	if(qR > M) update(o<<1|1, M+1, R, qL, qR, v1, v2);
    }
    
    void query(int o, int L, int R, int pos) {
    	if(L == R) {
    		printf("%d %d
    ", states[o], cities[o]);
    		return;
    	}
    	pushdown(o);
    	int M = (L + R) / 2;
    	if(pos <= M) query(o<<1, L, M, pos);
    	else query(o<<1|1, M+1, R, pos);
    }
    
    int main()
    {
    	int T; scanf("%d", &T);
    	while(T--) {
    		scanf("%d", &n);
    		for(int i = 1; i <= n; i++) {
    			pa[i] = i; sz[i] = 1;
    			int x; scanf("%d%d", &x, a + i);
    			b[i - 1] = a[i];
    		}
    		sort(b, b + n);
    		tot = unique(b, b + n) - b;
    		for(int i = 1; i <= n; i++) {
    			a[i] = lower_bound(b, b + tot, a[i]) - b;
    			miny[i] = maxy[i] = a[i];
    		}
    
    		memset(states, 0, sizeof(states));
    		memset(cities, 0, sizeof(cities));
    		int m; scanf("%d", &m);
    		while(m--) {
    			scanf("%s", cmd);
    			if(cmd[0] == 'r') {
    				int a, b; scanf("%d%d", &a, &b);
    				a++; b++;
    				int fa = findset(a), fb = findset(b);
    				if(fa == fb) continue;
    				int L = miny[fa], R = maxy[fa];
    				if(L < R) update(1, 1, n, L + 1, R, -1, -sz[fa]);
    				L = miny[fb], R = maxy[fb];
    				if(L < R) update(1, 1, n, L + 1, R, -1, -sz[fb]);
    				//Union
    				pa[fb] = fa;
    				sz[fa] += sz[fb];
    				miny[fa] = min(miny[fa], miny[fb]);
    				maxy[fa] = max(maxy[fa], maxy[fb]);
    				L = miny[fa], R = maxy[fa];
    				if(L < R) update(1, 1, n, L + 1, R, 1, sz[fa]);
    			} else {
    				double ty; scanf("%lf", &ty);
    				int y = (int)ty + 1;
    				y = lower_bound(b, b + tot, y) - b;
    				query(1, 1, n, y);
    			}
    		}
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    linux学习 建立静态库,动态库,写简单的makefile
    C++中顶层const和底层const
    BDB (Berkeley DB)数据库简单介绍(转载)
    Java中Map的使用
    Spring MVC 3 深入总结
    nvl,空时的推断和取值
    java堆栈 (转)
    mybatis--面向接口编程
    HDU 4888
    socket编程——一个简单的样例
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/5701292.html
Copyright © 2020-2023  润新知