loading

联合省选2025 追忆 recall

哎哟我怎么把这道题的做法给忘了。我好菜啊。

题意

给定一张 DAG,每个点有两种点权 \(a,b\),有 \(q\) 次操作:

  • 操作 1/2:给定参数 \(x,y\),交换 \(x,y\)\(a/b\) 权值
  • 操作 3:给定参数 \(l,r,x\),求满足“\(x\) 能通过有向边到达且 \(a_i\in[l,r]\)”的最大 \(b\) 值。

\(n,q\le 10^5,6\text{s}\)

分析

考虑到该问题严格强于 DAG 可达性,所以直接考虑 \(O(\frac{n^2}{\omega})\) 的做法。

先预处理出来 \(f_i\) 表示 \(i\) 到其他点的可达性。先不考虑修改,那么操作 3 所有可能的 \(i\) 就是 \(f_x\)\(\{i|l\le a_i\le r\}\) 的交集,设为 \(G\)。现在考虑修改 \(a\),那么我们直接对 \(a\) 分块即可,具体地,维护块间前缀和,那么整块的贡献直接一遍 bitset xor 即可解决,散块只需要根号次 flip 即可。

考虑如何求最大值。考虑二分答案,求出 \(b_i\ge v\) 的点集,然后让他与 \(G\) 取交,若有交则判定成功。但实际上我们没法直接二分,我们考虑对 \(b\) 再分块,先在整块上遍历一遍寻找答案所在块,再在定位到的那个块上遍历一遍找答案。但是这么做的复杂度是 \(O(\frac{n^2\sqrt n}{w})\) 的,继续优化,显然的,如果 \(G\)\(b_i\ge v\) 的点集无交,那么 \(G\) 也不可能跟 \(w>v\)\(b_i\ge w\) 的点集有交。借此我们可以考虑枚举 \(G\) 的每 \(\omega\) 位(这是一个整数),让它跟 \(b_i\ge v\) 的点集取交(显然这是 \(O(1)\) 的),如果有交那么直接 \(v\) 自加 1,否则接着考虑下一 \(\omega\) 位,直到所有 \(\frac{n}{\omega}\) 位全部考虑完为止,散块的话直接暴力枚举 \(b_i=某一个值\)\(i\) 是否在 \(G\) 内即可。

时间复杂度 \(O(n\sqrt n+\frac{n^2}{\omega})\)

由于我们的 bitset 需支持一些特殊操作,所以需要手写 bitset。

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;static char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e5+5,maxm=325,maxs=1565,inf=0x3f3f3f3f,B=320;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int ID;
int n,m,Q,siz,num;
int a[maxn],b[maxn];
int posa[maxn],posb[maxn];
vector<int>G[maxn];
il int L(int x){
	return (x-1)*B+1;
}
il int R(int x){
	return min(n,x*B);
}
il int bel(int x){
	return (x-1)/B+1;
}
struct mybit{
	u64 a[maxs];
	void reset(){
		rep(i,0,siz)a[i]=0;
	}
	void set(int x){
		a[x>>6]|=(1ull<<(x&63));
	}
	void flip(int x){
		a[x>>6]^=(1ull<<(x&63));
	}
	int get(int x){
		return (a[x>>6]>>(x&63))&1;
	}
	void operator&=(const mybit &p){
		rep(i,0,siz)a[i]&=p.a[i];
	}
	void operator|=(const mybit &p){
		rep(i,0,siz)a[i]|=p.a[i];
	}
	void operator^=(const mybit &p){
		rep(i,0,siz)a[i]^=p.a[i];
	}
};
//ga a[id]<=R(x) {id}
//gb b[id]>=L(x) {id}
mybit f[maxn],ga[maxm],gb[maxm],g;
void upd_a(int id,int x){
	rep(i,bel(x),num)ga[i].flip(id);
}
void upd_b(int id,int x){
	rep(i,1,bel(x))gb[i].flip(id);
}
void init();
inline void solve_the_problem(){
	n=rd(),m=rd(),Q=rd(),num=bel(n),siz=n>>6;
	init();
	rep(i,1,m){
		int x=rd(),y=rd();
		G[y].emplace_back(x);
	}
	rep(i,1,n)f[i].set(i);
	per(i,n,1)for(int u:G[i])f[u]|=f[i];
	rep(i,1,n)a[i]=rd(),upd_a(i,a[i]),posa[a[i]]=i;
	rep(i,1,n)b[i]=rd(),upd_b(i,b[i]),posb[b[i]]=i;
	while(Q--){
		int op=rd(),x=rd(),y,l,r;
		if(op==1){
			y=rd();
			upd_a(x,a[x]),upd_a(y,a[y]);
			swap(posa[a[x]],posa[a[y]]),swap(a[x],a[y]);
			upd_a(x,a[x]),upd_a(y,a[y]);
		}else if(op==2){
			y=rd();
			upd_b(x,b[x]),upd_b(y,b[y]);
			swap(posb[b[x]],posb[b[y]]),swap(b[x],b[y]);
			upd_b(x,b[x]),upd_b(y,b[y]);
		}else{
			l=rd(),r=rd();
			g.reset();
			if(bel(l)==bel(r)){
				rep(i,l,r)g.flip(posa[i]);
			}else{
				g=ga[bel(r)-1],g^=ga[bel(l)];
				rep(i,l,R(bel(l)))g.flip(posa[i]);
				rep(i,L(bel(r)),r)g.flip(posa[i]);
			}
			g&=f[x];
			int p=1;
			rep(i,0,siz){
				while(p<=num&&(g.a[i]&gb[p].a[i]))++p;
			}
			--p;
			int res=0;
			if(p){
				per(i,R(p),L(p))if(g.get(posb[i])){
					res=i;break;
				}
			}
			write(res);
		}
	}
}
void init(){
	rep(i,1,n)G[i].clear(),f[i].reset();
	rep(i,1,num)ga[i].reset(),gb[i].reset();
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	ID=rd();int _=rd();
	while(_--)solve_the_problem();
}
/*

*/

posted @ 2025-06-23 22:43  dcytrl  阅读(39)  评论(2)    收藏  举报