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\n", 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;
}

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/5701292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值