题解:P11340 [COI 2019] TENIS
题目大意
有
分析
发现很难形象刻画出哪些人可获得冠军,尝试观察冠军集合。
首先很容易发现每个场地中第一名的肯定在集合中,其次能在其他场地打败这些第一名的也要加入集合中......以此类推
不妨大胆猜测最后集合的构成,手搓后会发现最后的冠军集合在原来
那这样就很形象了,我们接下来就是想求出这个
考虑如何维护,每个人的最低排名和最高排名都是容易动态维护的,接下来套路性地将其视作一条线段,并在线段树上动态维护每条相邻两点之间的线出现的次数,我们查询的即为从左往右出现次数为
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5,MaxN=1e5;
int n,q;
int now_mi;
int a[5] [N+5];
int pos[5] [MaxN+5],lb[MaxN+5],rb[MaxN+5];
struct SGT{
int minn[(N<<2)+5],tag[(N<<2)+5];
void push_up(int now){
int lson=now<<1,rson=(now<<1)|1;
minn[now]=min(minn[lson],minn[rson]);
}
void push_down(int now){
int tt=tag[now];
if(tt==0)
return ;
int lson=now<<1,rson=(now<<1)|1;
minn[lson]+=tt,minn[rson]+=tt;
tag[lson]+=tt,tag[rson]+=tt;
tag[now]=0;
}
void update(int now,int L,int R,int x,int y,int val){
if(x>y)
return ;
if(x<=L&&R<=y){
minn[now]+=val;
tag[now]+=val;
return ;
}
push_down(now);
int mid=L+R>>1;
int lson=now<<1,rson=(now<<1)|1;
if(x<=mid)
update(lson,L,mid,x,y,val);
if(y>mid)
update(rson,mid+1,R,x,y,val);
push_up(now);
}
int query(int now,int L,int R){
if(L==R)
return L;
push_down(now);
int mid=L+R>>1;
int lson=now<<1,rson=(now<<1)|1;
if(minn[lson]==0)
return query(lson,L,mid);
return query(rson,mid+1,R);
}
}tr;
void change(int x){
if(lb[x])
tr.update(1,1,n,lb[x],rb[x]-1,-1);
lb[x]=min(min(pos[1] [x],pos[2] [x]),pos[3] [x]);
rb[x]=max(max(pos[1] [x],pos[2] [x]),pos[3] [x]);
tr.update(1,1,n,lb[x],rb[x]-1,1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i=1;i<=3;++i)
for(int j=1;j<=n;++j){
cin>>a[i] [j];
pos[i] [a[i] [j]]=j;
}
for(int i=1;i<=n;++i)
change(i);
now_mi=tr.query(1,1,n);
while(q--){
int op,p,x,y;
cin>>op;
if(op==1){
cin>>x;
if(rb[x]<=now_mi)
cout<<"DA\n";
else cout<<"NE\n";
}
else{
cin>>p>>x>>y;
swap(pos[p] [x],pos[p] [y]);
change(x);
change(y);
now_mi=tr.query(1,1,n);
}
}
return 0;
}