跳到正文

个人记录

一年补一题,一题补一年(3)| [省选联考 2025] 追忆

MatrixGroup约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年3月1日
收录
2026年3月1日
更新
2026年11月18日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

题目链接

写的 yrq 做法。

好像考试的时候求完 DAG 可达性就把 bitset 扔掉了,加上数据结构水平低下根本编不出来东西。

已严肃学习。

C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,del##i##verme=int(n);i<del##i##verme;++i)
#define rep1(i,n) for(int i=1,parano##i##a=int(n);i<=parano##i##a;++i)
#define per(i,n) for(int i=int(n)-1;i>=0;--i)
#define per1(i,n) for(int i=int(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define y0 LingLuo
#define y1 VividCycle
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
unsigned time_related_rand()
{
	return unsigned(std::chrono::steady_clock::now().time_since_epoch().count());
}
const int LEN=1570;
struct Bitset{
	ull w[LEN];
	void Reset()
	{
		memset(w,0,sizeof(w));
	}
	void Set()
	{
		memset(w,0xff,sizeof(w));
	}
	void Init(int L,int R)
	{
		for(int i=0;i<LEN;++i)
		{
			int lt=i<<6,rt=i<<6|63;
			if(lt>R||L>rt) w[i]=0;
			else if(L<=lt&&rt<=R) w[i]=ull(-1);
			else
			{
				w[i]=0;
				for(int j=0;j<64;++j) if(L<=(lt+j)&&(lt+j)<=R) w[i]|=1ull<<j;
			}
		}
	}
	void Set(int x)
	{
		w[x>>6]|=1ull<<(x&63);
	}
	void Reset(int x)
	{
		w[x>>6]&=~(1ull<<(x&63));
	}
	void Flip(int x)
	{
		w[x>>6]^=1ull<<(x&63);
	}
	bool query(int x)
	{
		return bool((w[x>>6]>>(x&63))&1);
	}
};
void Set_XOR(Bitset&A,const Bitset&B,const Bitset&C)
{
	rep(i,LEN) A.w[i]=B.w[i]^C.w[i];
}
void Let_OR(Bitset&A,const Bitset&B)
{
	rep(i,LEN) A.w[i]|=B.w[i];
}
void Let_AND(Bitset&A,const Bitset&B)
{
	rep(i,LEN) A.w[i]&=B.w[i];
}
int c,t,n,m,q,u,v,cb;
vector<int> to[100005];
Bitset reach[100005];
int a[100005],b[100005],ra[100005],rb[100005];
const int B=318;
Bitset ga[1005],gb[1005];
int block[100005],bl[1005],br[1005];
int op,l,r,x;
Bitset Answer,tmp1,tmp2;
void Find_a(Bitset&res,int val)
{
	if(val>n)
	{
		res.Reset();return ;
	}
	int blk=block[val];
	if(br[blk]-val<val-bl[blk])
	{
		res=ga[blk];
		for(int i=val;i<=br[blk];++i) res.Set(ra[i]);
	}
	else
	{
		res=ga[blk-1];
		for(int i=bl[blk];i<val;++i) res.Reset(ra[i]);
	}
}
void Q()
{
	cin>>n>>m>>q;
	rep1(i,n)
	{
		to[i].clear();reach[i].Init(i,i);
	}
	rep1(i,m)
	{
		cin>>u>>v;to[u].pb(v);
	}
	per1(i,n)
	{
		for(int j:to[i]) Let_OR(reach[i],reach[j]);
	}
	rep1(i,n)
	{
		cin>>a[i];ra[a[i]]=i;
	}
	rep1(i,n)
	{
		cin>>b[i];rb[b[i]]=i;
	}
	rep1(i,n)
	{
		block[i]=(i-1)/B+1;
		if(block[i]!=block[i-1]) bl[block[i]]=i;
		br[block[i]]=i;
	}
	cb=block[n];
	rep(i,cb+1)
	{
		ga[i].Reset();gb[i].Reset();
		rep1(j,n)
		{
			if(block[a[j]]>i) ga[i].Set(j);
			if(block[b[j]]>i) gb[i].Set(j);
		}
	}
	while(q--)
	{
		cin>>op;
		if(op==1)
		{
			cin>>l>>r;
			if(l==r) continue;
			swap(a[l],a[r]);swap(ra[a[l]],ra[a[r]]);
			if(a[l]>a[r]) swap(l,r);
			for(int i=block[a[l]];i<block[a[r]];++i)
			{
				ga[i].Flip(l);ga[i].Flip(r);
			}
		}
		else if(op==2)
		{
			cin>>l>>r;
			if(l==r) continue;
			swap(b[l],b[r]);swap(rb[b[l]],rb[b[r]]);
			if(b[l]>b[r]) swap(l,r);
			for(int i=block[b[l]];i<block[b[r]];++i)
			{
				gb[i].Flip(l);gb[i].Flip(r);
			}
		}
		else
		{
			cin>>x>>l>>r;
			Find_a(tmp1,l);
			Find_a(tmp2,r+1);
			Set_XOR(Answer,tmp1,tmp2);
			Let_AND(Answer,reach[x]);
			int p=-1;
			for(int i=0;i<LEN;++i)
			{
				while(gb[p+1].w[i]&Answer.w[i]) ++p;
			}
			if(p==-1)
			{
				cout<<"0\n";
			}
			else
			{
				for(int i=br[p+1];;--i) if(Answer.query(rb[i]))
				{
					cout<<i<<"\n";break;
				}
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin>>c>>t;
	while(t--)Q();
	return 0;
}
/* things to check
1.  int overflow or long long memory need
2.  recursion/array/binary search/dp/loop bounds
3.  precision
4.  special cases(n=1,bounds)
5.  delete debug statements
6.  initialize(especially multi-tests)
7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
8.  keep it simple and stupid
9.  do not delete, use // instead
10. operator priority
11. is there anything extra to output?
12. ...
*/

/* something to think about
1. greedy? dp? searching? dp with matrix/ segment tree? binary search?
2. If contains "not", why not ?????? or few affect?
*/
原文