题意:在坐标上给n个点,r的操作是将两个点连起来,l的操作是问你y=u的这条线连接的集合块数和这些集合内的点的个数
思路:很麻烦的一道题,在网上看了题意和做法后,开始了一下午的调bug过程,做法很好懂,我开了两个线段树,一个维护点代表的直线的集合个数,另一个则是路过集合内的点的个数,然后集合的判断直接用并查集就行了,这是两个核心,然后就是自己瞎写的了,代码丑的可以而且好像除了本人别人看着可能要骂人了,有兴趣研究的可以留言我来解答,那难的部分其实就是并查集合并时该怎么将这两个要维护的值弄进线段树中,而且这题我用的离散化处理的,好像可以不用,但感觉空间上有点过意不去,所以代码看着真是丑,然后说说合并的操作,先是集合的个数,令左边上下限分别为L1,R1,右边上限是L2,R2,下面都是这样,然后L1到R1的集合个数全部减1,也就是区间更新,然后R1到R2也是一样,然后整个大区间+1,现在看若左边区间个数是1,右边也是1,那么合并后还是1,第二个线段树的操作与这个相似,统计一下就可以了,注意细节,这里献上九野聚聚的样例,也是这个样例终结了我一下午的找BUG旅程
- 3
- 11
- 1 7
- 5 7
- 8 6
- 3 5
- 5 5
- 2 3
- 10 3
- 7 2
- 4 1
- 11 1
- 4 6
- 21
- road 0 1
- road 3 5
- line 6.5
- road 4 2
- road 3 8
- road 4 7
- road 6 9
- road 4 1
- road 2 7
- line 4.5
- line 6.5
- line 3.5
- line 2.5
- line 5.5
- road 10 0
- line 5.5
- line 6.5
- road 0 3
- line 1.5
- line 6.5
- line 2.5
- ans:
- 0 0
- 2 8
- 1 5
- 2 8
- 3 10
- 1 5
- 1 6
- 1 6
- 2 11
- 1 9
- 2 11
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=100010;
int f[maxn*2],r[maxn*2],L[maxn*2],R[maxn*2],t[maxn*4],num2[maxn*8],num1[maxn*8],Q[maxn*2][2],Lazy1[maxn*8],Lazy2[maxn*8];
int len;
double pos1[maxn*2];
char ch[200010][10];
int find1(int x){
if(x!=f[x]) f[x]=find1(f[x]);
return f[x];
}
void pushdown_1(int node){
if(Lazy1[node]){
Lazy1[node<<1]+=Lazy1[node];
Lazy1[node<<1|1]+=Lazy1[node];
num1[node<<1]+=Lazy1[node];
num1[node<<1|1]+=Lazy1[node];
Lazy1[node]=0;
}
}
void pushdown_2(int node){
if(Lazy2[node]){
Lazy2[node<<1]+=Lazy2[node];
Lazy2[node<<1|1]+=Lazy2[node];
num2[node<<1]+=Lazy2[node];
num2[node<<1|1]+=Lazy2[node];
Lazy2[node]=0;
}
}
void buildtree(int le,int ri,int node){
num2[node]=num1[node]=Lazy1[node]=Lazy2[node]=0;
if(le==ri) return ;
int t=(le+ri)>>1;
buildtree(le,t,node<<1);
buildtree(t+1,ri,node<<1|1);
}
void update_1(int l,int r,int x,int le,int ri,int node){
if(l<=le&&ri<=r){
Lazy1[node]+=x;
num1[node]+=x;
return ;
}
pushdown_1(node);
int t=(le+ri)>>1;
if(l<=t) update_1(l,r,x,le,t,node<<1);
if(r>t) update_1(l,r,x,t+1,ri,node<<1|1);
}
void update_2(int l,int r,int x,int le,int ri,int node){
if(l<=le&&ri<=r){
Lazy2[node]+=x;
num2[node]+=x;
return ;
}
pushdown_2(node);
int t=(le+ri)>>1;
if(l<=t) update_2(l,r,x,le,t,node<<1);
if(r>t) update_2(l,r,x,t+1,ri,node<<1|1);
}
int query_1(int pos,int le,int ri,int node){
if(le==ri) return num1[node];
pushdown_1(node);
int t=(le+ri)>>1;
int ans;
if(pos<=t) ans=query_1(pos,le,t,node<<1);
else ans=query_1(pos,t+1,ri,node<<1|1);
return ans;
}
int query_2(int pos,int le,int ri,int node){
if(le==ri) return num2[node];
pushdown_2(node);
int t=(le+ri)>>1;
int ans;
if(pos<=t) ans=query_2(pos,le,t,node<<1);
else ans=query_2(pos,t+1,ri,node<<1|1);
return ans;
}
void unite(int a,int b){
int aa=find1(a);
int bb=find1(b);
if(aa==bb) return ;
if(L[aa]>=L[bb]&&R[aa]<=R[bb]){
int l1=lower_bound(t,t+len,L[aa])-t+1;
int r1=lower_bound(t,t+len,R[aa])-t+1;
int l2=lower_bound(t,t+len,L[bb])-t+1;
int r2=lower_bound(t,t+len,R[bb])-t+1;
update_1(l1,r1,-1,1,len,1);
update_1(l2,r2,-1,1,len,1);
update_1(l2,r2,1,1,len,1);
update_2(l1,r1,-r[aa],1,len,1);
update_2(l2,r2,-r[bb],1,len,1);
update_2(l2,r2,r[aa]+r[bb],1,len,1);
}else if(L[aa]>=L[bb]&&R[aa]>R[bb]){
int l1=lower_bound(t,t+len,L[aa])-t+1;
int r1=lower_bound(t,t+len,R[aa])-t+1;
int l2=lower_bound(t,t+len,L[bb])-t+1;
int r2=lower_bound(t,t+len,R[bb])-t+1;
update_1(l1,r1,-1,1,len,1);
update_1(l2,r2,-1,1,len,1);
update_1(l2,r1,1,1,len,1);
update_2(l1,r1,-r[aa],1,len,1);
update_2(l2,r2,-r[bb],1,len,1);
update_2(l2,r1,r[aa]+r[bb],1,len,1);
}else if(L[aa]<L[bb]&&R[aa]<=R[bb]){
int l1=lower_bound(t,t+len,L[aa])-t+1;
int r1=lower_bound(t,t+len,R[aa])-t+1;
int l2=lower_bound(t,t+len,L[bb])-t+1;
int r2=lower_bound(t,t+len,R[bb])-t+1;
update_1(l1,r1,-1,1,len,1);
update_1(l2,r2,-1,1,len,1);
update_1(l1,r2,1,1,len,1);
update_2(l1,r1,-r[aa],1,len,1);
update_2(l2,r2,-r[bb],1,len,1);
update_2(l1,r2,r[aa]+r[bb],1,len,1);
}else if(L[aa]<L[bb]&&R[aa]>R[bb]){
int l1=lower_bound(t,t+len,L[aa])-t+1;
int r1=lower_bound(t,t+len,R[aa])-t+1;
int l2=lower_bound(t,t+len,L[bb])-t+1;
int r2=lower_bound(t,t+len,R[bb])-t+1;
update_1(l1,r1,-1,1,len,1);
update_1(l2,r2,-1,1,len,1);
update_1(l1,r1,1,1,len,1);
update_2(l1,r1,-r[aa],1,len,1);
update_2(l2,r2,-r[bb],1,len,1);
update_2(l1,r1,r[aa]+r[bb],1,len,1);
}
f[aa]=bb;r[bb]+=r[aa];L[bb]=min(L[bb],L[aa]);R[bb]=max(R[bb],R[aa]);
}
struct edge{
int x,y;
}id[maxn];
int main(){
int T,n,m,u,v;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d%d",&id[i].x,&id[i].y);
int k=0;
for(int i=0;i<n;i++){
id[i].y*=2;
f[i]=i;r[i]=1;t[k++]=id[i].y;L[i]=id[i].y;R[i]=id[i].y;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%s",ch[i]);
if(ch[i][0]=='r') scanf("%d%d",&Q[i][0],&Q[i][1]);
else if(ch[i][0]=='l'){
scanf("%lf",&pos1[i]);
Q[i][0]=(int)2*pos1[i];
t[k++]=Q[i][0];
}
}
sort(t,t+k);
len=unique(t,t+k)-t;
buildtree(1,len,1);
for(int i=0;i<m;i++){
if(ch[i][0]=='r') unite(Q[i][0],Q[i][1]);
else if(ch[i][0]=='l'){
int ll=lower_bound(t,t+len,Q[i][0])-t+1;
int ans1=query_1(ll,1,len,1);
int ans2=query_2(ll,1,len,1);
printf("%d %d\n",ans1,ans2);
}
}
}
return 0;
}