跳到正文

题解

题解:P16679 [CSPro 27] 吉祥物投票

kevinZ99约 26 分钟
原文

这是 2026年10月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月25日
收录
2026年10月25日
更新
2026年11月21日
篇幅
约 26 分钟
获赞
1
题目
P16679

版本

  1. v2当前
  2. v1

这不是黄题,建议降红。模拟赛场了。

注意到区间覆盖,其他操作不会影响区间关系,于是颜色均摊珂朵莉树。

于是注意到一四无操作是简单的。二三操作我们考虑启发式合并即可,注意交换操作由于编号问题,需要存储当前编号的吉祥物实际存在哪一个平衡树中,但是维护答案由于需要输出最小的编号所以需要维护真实编号。

ACcode
C++
//Author:kevinZ99
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define fst first
#define sed second
#define pref static inline
using namespace std;using hint=__int128;using uhint=unsigned __int128;
using us=unsigned short;using ui=unsigned int;using ll=long long;using ull=unsigned long long;
using db=double;using ldb=long double;using pll=pair<ll,ll>;
using pil=pair<int,ll>;using pli=pair<ll,int>;using vi=vector<int>;using vl=vector<ll>;
using vpi=vector<pair<int,int>>;using vpl=vector<pll>;using vpil=vector<pil>;using vpli=vector<pli>;
ostream& out128(ostream&,uhint,bool);ostream& operator<<(ostream&,hint);ostream& operator<<(ostream&,uhint);
namespace mystl{
	const int SI=1<<22,SO=1<<20;pref size_t fr(void*p,size_t n){
#ifdef __linux__
		return fread_unlocked(p,1,n,stdin);
#else
		return fread(p,1,n,stdin);
#endif
	}
	unsigned char mp[1<<16];struct Init{Init(){memset(mp,255,sizeof mp);for(unsigned i=0;i<10;++i)for(unsigned j=0;j<10;++j){unsigned char s[2]={static_cast<unsigned char>('0'+i),static_cast<unsigned char>('0'+j)};uint16_t x;memcpy(&x,s,2);mp[x]=static_cast<unsigned char>(i*10+j);}}}init;
	pref unsigned pv(const char*p){uint16_t x;memcpy(&x,p,2);return mp[x];}
	struct IB:streambuf{
		char b[SI];bool eofed=false;IB(){setg(b,b,b);}
		int_type underflow()override{if(gptr()<egptr())return traits_type::to_int_type(*gptr());size_t n=fr(b,SI);if(!n)return traits_type::eof();setg(b,b,b+n);return traits_type::to_int_type(*gptr());}
		inline void commit(char*p){gbump(static_cast<int>(p-gptr()));}
		inline bool refill(char*&p,char*&e){commit(p);if(traits_type::eq_int_type(underflow(),traits_type::eof())){eofed=true;return false;}p=gptr();e=egptr();return true;}
		inline bool head(char*&p,char*&e,bool&neg){eofed=false;for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e)break;if(!refill(p,e))return false;}neg=false;if(*p=='-'||*p=='+'){neg=*p=='-';++p;if(p==e&&!refill(p,e))return false;}return true;}
		template<class T,class U>inline bool slow(T&x,bool neg,char*&p,char*&e){U y=0;bool ok=false;for(;;){while(p<e){unsigned c=static_cast<unsigned char>(*p)-static_cast<unsigned>('0');if(c>9){commit(p);if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}ok=true;y=static_cast<U>(y*10+c);++p;}if(!refill(p,e)){if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}}}
		template<class T,class U,int K>inline bool num(T&x){x=0;char*p=gptr(),*e=egptr();bool neg;if(!head(p,e,neg))return false;if(e-p<2*K+1)return slow<T,U>(x,neg,p,e);U y=0;bool ok=false;for(int i=0;i<K;++i){unsigned v=pv(p);if(v>=255)break;ok=true;y=static_cast<U>(y*100+static_cast<U>(v));p+=2;}unsigned c=static_cast<unsigned char>(*p)-static_cast<unsigned>('0');if(c<=9){ok=true;y=static_cast<U>(y*10+c);++p;}commit(p);if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}
		inline bool read(short&x){return num<short,unsigned short,5>(x);}inline bool read(us&x){return num<us,us,5>(x);}inline bool read(int&x){return num<int,unsigned,5>(x);}inline bool read(unsigned&x){return num<unsigned,unsigned,5>(x);}inline bool read(long&x){return sizeof(long)<=4?num<long,unsigned long,5>(x):num<long,unsigned long,10>(x);}inline bool read(unsigned long&x){return sizeof(unsigned long)<=4?num<unsigned long,unsigned long,5>(x):num<unsigned long,unsigned long,10>(x);}inline bool read(ll&x){return num<ll,ull,10>(x);}inline bool read(ull&x){return num<ull,ull,10>(x);}inline bool read(hint&x){return num<hint,uhint,20>(x);}inline bool read(uhint&x){return num<uhint,uhint,20>(x);}
		inline bool read(char&x){x=0;eofed=false;char*p=gptr(),*e=egptr();for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e){x=*p++;commit(p);return true;}if(!refill(p,e))return false;}}
		inline bool read(string&x){eofed=false;x.clear();char*p=gptr(),*e=egptr();for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e)break;if(!refill(p,e))return false;}for(;;){char*q=p;while(q<e&&static_cast<unsigned char>(*q)>32)++q;x.append(p,static_cast<size_t>(q-p));p=q;if(p<e){commit(p);return true;}if(!refill(p,e))return true;}}
		template<class T>inline bool fp(T&x){x=0;string s;if(!read(s))return false;return cv(s.c_str(),x);}static inline bool cv(const char*s,float&x){char*e=nullptr;x=strtof(s,&e);return e!=s&&*e==0;}static inline bool cv(const char*s,double&x){char*e=nullptr;x=strtod(s,&e);return e!=s&&*e==0;}static inline bool cv(const char*s,long double&x){char*e=nullptr;x=strtold(s,&e);return e!=s&&*e==0;}
	}ib;
	struct OB:streambuf{
		char b[SO];OB(){setp(b,b+SO);}~OB(){sync();}
		int sync()override{size_t n=static_cast<size_t>(pptr()-pbase());if(n&&fwrite(b,1,n,stdout)!=n)return -1;setp(b,b+SO);return 0;}
		int_type overflow(int_type c=traits_type::eof())override{if(sync())return traits_type::eof();if(!traits_type::eq_int_type(c,traits_type::eof()))*pptr()=traits_type::to_char_type(c),pbump(1);return traits_type::not_eof(c);}
		inline void pc(char c){if(pptr()==epptr()&&traits_type::eq_int_type(overflow(),traits_type::eof()))return;*pptr()=c;pbump(1);}inline void ps(const char*s,size_t n){while(n){size_t k=static_cast<size_t>(epptr()-pptr());if(!k){if(traits_type::eq_int_type(overflow(),traits_type::eof()))return;k=static_cast<size_t>(epptr()-pptr());}k=min(k,n);memcpy(pptr(),s,k);pbump(static_cast<int>(k));s+=k;n-=k;}}
	}ob;
	struct FastIn:istream{
		FastIn():istream(&ib){}using istream::operator>>;template<class T>inline FastIn&go(T&x){if(!ib.read(x))setstate(ib.eofed?ios::eofbit|ios::failbit:ios::failbit);else if(ib.eofed)setstate(ios::eofbit);return *this;}inline FastIn&operator>>(short&x){return go(x);}inline FastIn&operator>>(us&x){return go(x);}inline FastIn&operator>>(int&x){return go(x);}inline FastIn&operator>>(unsigned&x){return go(x);}inline FastIn&operator>>(long&x){return go(x);}inline FastIn&operator>>(unsigned long&x){return go(x);}inline FastIn&operator>>(ll&x){return go(x);}inline FastIn&operator>>(ull&x){return go(x);}inline FastIn&operator>>(hint&x){return go(x);}inline FastIn&operator>>(uhint&x){return go(x);}inline FastIn&operator>>(char&x){return go(x);}inline FastIn&operator>>(string&x){return go(x);}inline FastIn&operator>>(float&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(double&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(long double&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(bool&x){int y=0;go(y);x=y!=0;return *this;}
	}fin;
	struct FastOut:ostream{
		FastOut():ostream(&ob){}using ostream::operator<<;template<class U>inline FastOut&uo(U x){char s[64];int n=0;do{s[n++]=static_cast<char>('0'+static_cast<unsigned>(x%10));x/=10;}while(x);while(n)ob.pc(s[--n]);return *this;}template<class T>inline FastOut&so(T x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}typedef typename make_unsigned<T>::type U;U y;if(x<0){ob.pc('-');y=U(0)-static_cast<U>(x);}else y=static_cast<U>(x);return uo(y);}template<class T>inline FastOut&un(T x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}return uo(x);}
		inline FastOut&operator<<(short x){return so(x);}inline FastOut&operator<<(us x){return un(x);}inline FastOut&operator<<(int x){return so(x);}inline FastOut&operator<<(unsigned x){return un(x);}inline FastOut&operator<<(long x){return so(x);}inline FastOut&operator<<(unsigned long x){return un(x);}inline FastOut&operator<<(ll x){return so(x);}inline FastOut&operator<<(ull x){return un(x);}inline FastOut&operator<<(bool x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}ob.pc(x?'1':'0');return *this;}inline FastOut&operator<<(char c){if(width()!=0){static_cast<ostream&>(*this)<<c;return *this;}ob.pc(c);return *this;}inline FastOut&operator<<(const char*s){if(width()!=0){static_cast<ostream&>(*this)<<s;return *this;}ob.ps(s,strlen(s));return *this;}inline FastOut&operator<<(const string&s){if(width()!=0){static_cast<ostream&>(*this)<<s;return *this;}ob.ps(s.data(),s.size());return *this;}inline FastOut&operator<<(float x){static_cast<ostream&>(*this)<<x;return *this;}inline FastOut&operator<<(double x){static_cast<ostream&>(*this)<<x;return *this;}inline FastOut&operator<<(long double x){static_cast<ostream&>(*this)<<x;return *this;}
		inline FastOut&operator<<(hint x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}uhint y;if(x<0){ob.pc('-');y=uhint(0)-static_cast<uhint>(x);}else y=static_cast<uhint>(x);return uo(y);}inline FastOut&operator<<(uhint x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}return uo(x);}FastOut&operator<<(ios_base&(*f)(ios_base&)){f(*this);return *this;}FastOut&operator<<(ostream&(*f)(ostream&)){f(*this);return *this;}FastOut&operator<<(ios&(*f)(ios&)){f(*this);return *this;}
	}fout;
}
ostream& out128(ostream&out,uhint x,bool neg){char s[48];int n=0;do{s[n++]=static_cast<char>('0'+static_cast<unsigned>(x%10));x/=10;}while(x);streamsize w=out.width(0),k=max<streamsize>(0,w-n-neg);char f=out.fill();ios::fmtflags a=out.flags()&ios::adjustfield;if(a==ios::left){if(neg)out.put('-');while(n)out.put(s[--n]);while(k--)out.put(f);}else if(a==ios::internal&&neg){out.put('-');while(k--)out.put(f);while(n)out.put(s[--n]);}else{while(k--)out.put(f);if(neg)out.put('-');while(n)out.put(s[--n]);}return out;}
ostream& operator<<(ostream&out,hint x){bool neg=x<0;return out128(out,neg?uhint(0)-static_cast<uhint>(x):static_cast<uhint>(x),neg);}ostream& operator<<(ostream&out,uhint x){return out128(out,x,false);}
#define cin mystl::fin
#define cout mystl::fout
namespace my{
	const int N=1e5+5,inf=1000000000;template<typename T>pref T Min(T x,T y){return x<y?x:y;}template<typename T>pref T Max(T x,T y){return x>y?x:y;}template<typename T>pref T Abs(T x){return x<0?-x:x;}
	set<pair<int,int>>s[N];
	struct node{
		int l,r,c;
		inline friend bool operator < (node x,node y){
			return x.l<y.l;
		}
	};
	struct pii{
		int fst,sed;
		inline friend bool operator < (pii x,pii y){
			return (x.fst==y.fst)?(x.sed<y.sed):(x.fst>y.fst);
		}
	};
	int id[N],mp[N];
	set<node>all;
	set<pii>ans;
	int n,m,q,got[N];
	pref void swapid(int x,int y){
		// x->id[x]
		// y->id[y]
		ans.erase({got[x],x});
		ans.erase({got[y],y});
		swap(got[x],got[y]);
		ans.insert({got[x],x});
		ans.insert({got[y],y});
		swap(id[x],id[y]);
		mp[id[x]]=x;
		mp[id[y]]=y;
	}
	pref void erase(int l,int r,int c){
		s[c].erase({l,r});
		all.erase({l,r,c});
		ans.erase({got[mp[c]],mp[c]});
		got[mp[c]]-=(r-l+1);
		ans.insert({got[mp[c]],mp[c]});
	}
	pref set<node>::iterator insert(int l,int r,int c){
		s[c].insert({l,r});
		auto ret=all.insert({l,r,c}).fst;
//		cout<<"INSERT:"<<c<<' '<<id[c]<<'\n';
		ans.erase({got[mp[c]],mp[c]});
		got[mp[c]]+=(r-l+1);
		ans.insert({got[mp[c]],mp[c]});
		return ret;
	}
	pref set<node>::iterator split(int x){
		auto it=all.lower_bound({x,-1,0});
		if(it->l==x&&it!=all.end())return it;
		it--;
		if(it->r<x)return all.end();
		int lf=it->l,rf=it->r,cf=it->c;
		erase(lf,rf,cf);
		insert(lf,x-1,cf);
		return insert(x,rf,cf);
	}
	pref void SOLVE(){
		cin>>n>>m>>q;
		s[0].insert({1,n}),got[0]=n;
		all.insert({1,n,0});
		ans.insert({n,0});
		up(i,1,m)ans.insert({0,i}),id[i]=mp[i]=i;
		int op;
		while(q--){
			cin>>op;
			if(op==1){
				int l,r,x;cin>>l>>r>>x;
				x=id[x];
				auto itr=split(r+1),itl=split(l);
				vector<node>t;
				for(auto it=itl;it!=itr;it++)
					t.push_back(*it);
				for(auto v:t)
					erase(v.l,v.r,v.c);
				insert(l,r,x);
			}
			if(op==2){
				int tx,tw,x,w;cin>>tx>>tw;
				x=id[tx],w=id[tw];
				if(s[x].size()>s[w].size()){
					vector<node>t;
					for(auto v:s[w])t.push_back({v.fst,v.sed,w});
					for(auto v:t)erase(v.l,v.r,v.c);
//					cout<<"AFTER ERASE\n";
//					up(i,0,m)cout<<id[i]<<" \n"[i==m];
//					cout<<"ANS:\n";
//					for(auto v:ans)cout<<v.fst<<' '<<v.sed<<'\n';
//					cout<<"ALL:\n";
//					for(auto v:all)cout<<v.l<<' '<<v.r<<' '<<v.c<<'\n';
//					cout<<'\n';
					for(auto v:t)insert(v.l,v.r,x);
					swapid(tx,tw);
				}else {
					vector<node>t;
					for(auto v:s[x])t.push_back({v.fst,v.sed,x});
					for(auto v:t)erase(v.l,v.r,v.c);
					for(auto v:t)insert(v.l,v.r,w);
				}
			}
			if(op==3){
				int x,y;cin>>x>>y;
				swapid(x,y);
			}
			if(op==4){
				int w;cin>>w;
				cout<<got[w]<<'\n';
			}
			if(op==5){
				auto it=ans.begin();
				if(it->sed&&it->fst)cout<<it->sed<<'\n';
				else if(!it->fst)cout<<"0\n";
				else{
					it++;
					if(it->fst)cout<<it->sed<<'\n';
					else cout<<"0\n";
				}
			}
//			up(i,0,m)cout<<id[i]<<" \n"[i==m];
//			cout<<"ANS:\n";
//			for(auto v:ans)cout<<v.fst<<' '<<v.sed<<'\n';
//			cout<<"ALL:\n";
//			for(auto v:all)cout<<v.l<<' '<<v.r<<' '<<v.c<<'\n';
//			cout<<'\n';
		}
	}
}
int main(){
//	freopen("s.in","r",stdin);
//	freopen("my.out","w",stdout);
	my::SOLVE();return 0;
}