跳到正文

题解

N方过十万,暴力碾标算

qiuqiu_luogu约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年6月9日
收录
2026年6月9日
更新
2026年11月11日
篇幅
约 4 分钟
题目
P11831

版本

  1. v1当前

本题考查 bitset 维护连通性和循环。

但是 O(nq)O(nq) 有点卡常,注意到 bitset 当中的 11 比较稀疏,且遇到 11 会直接退出。

所以我们可以使用 __builtin_expect 优化编译器行为,具体可查询 OI-Wiki。

C++
#include<bits/stdc++.h>

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
//#define int long long
#ifdef __unix__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#else
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#endif
int read() {
	int x = 0, f = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar();
	return x * f;
}
inline void write(int x) {
	static int sum[35]; int top = 0;
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) { putchar('0'); return; }
	while (x) { sum[top++] = x % 10; x /= 10; }
	while (top) putchar('0' + sum[--top]);
}
using namespace std;

const int N=1e5+20;

bitset<N>s[N];
vector<int>p[N];

int n,m,q,go[N],a[N],b[N];
void one(){
	n=read(),m=read(),q=read();
	for(int i=1,u,v;i<=m;i++){
		u=read(),v=read();
		p[u].push_back(v);
	}
	for(int i=n;i>=1;i--){
		s[i][i]=1;
		for(int v:p[i]){
			s[i]|=s[v];
		}
	}
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read(),go[b[i]]=i;
	int opt,s1,s2,s3;
	while(q--){
		opt=read();
		s1=read(),s2=read();
		if(opt==1){
			swap(a[s1],a[s2]);
		}
		if(opt==2){
			swap(b[s1],b[s2]);
			go[b[s1]]=s1;
			go[b[s2]]=s2;
		}
		if(likely(opt==3)){
			s3=read();
			int out=0;
			for(int i=n;i>=1;i--){
				int u=go[i];
				if(unlikely(s[s1][u]&&s2<=a[u]&&a[u]<=s3)){
					out=i;
					break;
				}
			}
			write(out),putchar('\n');
		}
	}
	for(int i=1;i<=n;i++)p[i].clear(),s[i].reset();
}

signed main(){
	read();
	int T=read();
	while(T--){
		one();
	}
	return 0;
}

提交记录,有点极限。

原文