题目链接:点击打开链接
题意:给你n个点,m次询问,每次询问有两种:
1. 将城市v和u用一条道路连接起来
2.询问用一条水平线能穿过多少个州和这些周中包括的城市数。
由于水平线一定是v.5的形式,所以不妨将y坐标乘以2再建立线段树,建两棵线段树,一棵维护大洲的数量,一棵维护城市数。
合并问题交给并查集就好了。
另外值得一提的是,对于线段树区间加减这个问题, 其处理方法大家应该更深入的理解,而不要只是当成模板。 其处理手法其实暗含了处理标记冲突的方法,也就是等价转换后使得每时每刻每个结点的标记都只有一个。 由于是区间加减,方法就是下传标记的时候在标记上加减而非修改。
细节参见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 100000 + 10;
const int maxm = 2000000 + 10;
int T,n,m,sum[2][maxm<<2],addv[2][maxm<<2],p[maxn],tot[maxn],low[maxn],top[maxn];
struct node {
int x, y;
};
char s[10];
void PushUp(int id, int o) {
sum[id][o] = sum[id][o<<1] + sum[id][o<<1|1];
}
void pushdown(int id, int l, int r, int o) {
if(addv[id][o] != 0) {
addv[id][o<<1] += addv[id][o];
addv[id][o<<1|1] += addv[id][o];
int m = (l + r) >> 1;
sum[id][o<<1] += (m - l + 1) * addv[id][o];
sum[id][o<<1|1] += (r - m) * addv[id][o];
addv[id][o] = 0;
}
}
void build(int l, int r, int o) {
int m = (l + r) >> 1;
sum[0][o] = sum[1][o] = addv[0][o] = addv[1][o] = 0;
if(l == r) return ;
build(l, m, o<<1);
build(m+1, r, o<<1|1);
}
void update(int id, int L, int R, int v, int l, int r, int o) {
int m = (l + r) >> 1;
if(L <= l && r <= R) {
addv[id][o] += v;
sum[id][o] += (r - l + 1) * v;
return ;
}
pushdown(id, l, r, o);
if(L <= m) update(id, L, R, v, l, m, o<<1);
if(m < R) update(id, L, R, v, m+1, r, o<<1|1);
PushUp(id, o);
}
int query(int id, int L, int R, int l, int r, int o) {
int m = (l + r) >> 1;
if(L <= l && r <= R) {
return sum[id][o];
}
pushdown(id, l, r, o);
int ans = 0;
if(L <= m) ans += query(id, L, R, l, m, o<<1);
if(m < R) ans += query(id, L, R, m+1, r, o<<1|1);
PushUp(id, o);
return ans;
}
int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int x, y, u, v, maxy = 0;
for(int i=0;i<n;i++) p[i] = i, tot[i] = 1;
for(int i=0;i<n;i++) {
scanf("%d%d",&x,&y);
top[i] = y * 2;
low[i] = y * 2;
maxy = max(maxy, y);
}
maxy *= 2;
double C;
scanf("%d",&m);
build(0, maxy, 1);
while(m--) {
scanf("%s",s);
if(s[0] == 'r') {
scanf("%d%d",&u,&v);
int x = _find(u), y = _find(v);
if(x != y) {
if(tot[x] > 1) update(0, low[x], top[x], -1, 0, maxy, 1);
if(tot[x] > 1) update(1, low[x], top[x], -tot[x], 0, maxy, 1);
if(tot[y] > 1) update(0, low[y], top[y], -1, 0, maxy, 1);
if(tot[y] > 1) update(1, low[y], top[y], -tot[y], 0, maxy, 1);
p[x] = y;
top[y] = max(top[x], top[y]);
low[y] = min(low[x], low[y]);
tot[y] = tot[x] + tot[y];
update(0, low[y], top[y], 1, 0, maxy, 1);
update(1, low[y], top[y], tot[y], 0, maxy, 1);
}
}
else {
scanf("%lf",&C);
int v = (int)(C); v = v * 2 + 1;
if(v > maxy) printf("0 0\n");
else {
printf("%d %d\n",query(0,v,v,0,maxy,1),query(1,v,v,0,maxy,1));
}
}
}
}
return 0;
}