题目连接:https://vjudge.net/problem/UVALive-4730
思路:用并查集来维护每一个州的城市数目,最上面城市的y坐标maxy,最下面城市的y坐标miny,当在城市之间连接一条路时,如果这两个城市已经在同一个州内,就不用处理了,如果不在同一个州内,首先要现在线段树中删除这两个州的信息,注意如果这个州只有一个城市不用删除,因为线段树内并没有记录只有一个城市的州的信息,然后合并这两个州,再把合并之后的州的信息更新到线段树上,区间更新,查询的时候单点查询
#include <bits/stdc++.h>
using namespace std;
#define lson num << 1
#define rson num << 1 | 1
#define MAXN 2000006
struct node1
{
int l,r;
int state,city;
int lazy1,lazy2;
}tree[MAXN << 2];
void pushup(int num)
{
tree[num].state = tree[lson].state + tree[rson].state;
tree[num].city = tree[lson].city + tree[rson].city;
}
void pushdown(int num)
{
if(tree[num].lazy1) {
tree[lson].state += tree[num].lazy1;
tree[rson].state += tree[num].lazy1;
tree[lson].lazy1 += tree[num].lazy1;
tree[rson].lazy1 += tree[num].lazy1;
tree[num].lazy1 = 0;
}
if(tree[num].lazy2) {
tree[lson].city += tree[num].lazy2;
tree[rson].city += tree[num].lazy2;
tree[lson].lazy2 += tree[num].lazy2;
tree[rson].lazy2 += tree[num].lazy2;
tree[num].lazy2 = 0;
}
}
void build(int num,int l,int r)
{
tree[num].l = l;
tree[num].r = r;
tree[num].lazy1 = tree[num].lazy2 = 0;
tree[num].state = tree[num].city = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build(lson,l,mid);
build(rson,mid + 1,r);
}
void update(int num,int l,int r,int state,int city)
{
if(tree[num].l == l && tree[num].r == r) {
tree[num].state += state;
tree[num].city += city;
tree[num].lazy1 += state;
tree[num].lazy2 += city;
return;
}
pushdown(num);
int mid = (tree[num].l + tree[num].r) >> 1;
if(r <= mid) update(lson,l,r,state,city);
else if(l > mid) update(rson,l,r,state,city);
else {
update(lson,l,mid,state,city);
update(rson,mid + 1,r,state,city);
}
pushup(num);
}
int query(int num,int pos)
{
if(tree[num].l == pos && tree[num].r == pos) {
return num;
}
pushdown(num);
int mid = (tree[num].l + tree[num].r) >> 1;
if(pos <= mid) query(lson,pos);
else query(rson,pos);
}
struct node2
{
int maxy,miny;
int num,pre;
}un[MAXN];
int findset(int x)
{
if(x == un[x].pre) return x;
else return un[x].pre = findset(un[x].pre);
}
void join(int x,int y)
{
int fx = findset(x);
int fy = findset(y);
if(fx != fy) {
if(un[fx].num > 1) update(1,un[fx].miny,un[fx].maxy,-1,-un[fx].num);
if(un[fy].num > 1) update(1,un[fy].miny,un[fy].maxy,-1,-un[fy].num);
un[fy].pre = fx;
un[fx].num += un[fy].num;
un[fx].miny = min(un[fy].miny,un[fx].miny);
un[fx].maxy = max(un[fy].maxy,un[fx].maxy);
update(1,un[fx].miny,un[fx].maxy,1,un[fx].num);
}
}
int main(void)
{
int T,n,m;
int x,y,id;
double li;
int Max;
char op[10];
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
Max = 0;
for(int i = 0; i < n; i++) {
scanf("%d %d",&x,&y);
un[i].pre = i;
un[i].maxy = un[i].miny = (y << 1);
un[i].num = 1;
Max = max(Max,y << 1);
}
build(1,0,Max);
scanf("%d",&m);
while(m--) {
scanf("%s",&op);
if(op[0] == 'r') {
scanf("%d %d",&x,&y);
join(x,y);
}
else {
scanf("%lf",&li); li *= 2;
if((int)(li) > Max) printf("0 0\n");
else{
id = query(1,(int)li);
printf("%d %d\n",tree[id].state,tree[id].city);
}
}
}
}
return 0;
}