社区讨论

100pts 但是 hack TLE,求调

P5787【模板】线段树分治 / 二分图参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mk16aana
此快照首次捕获于
2026/01/05 21:04
2 个月前
此快照最后确认于
2026/01/09 13:30
2 个月前
查看原帖
CPP
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
//#include<ext/pb_ds/priority_queue.hpp>
#define umap unordered_map
#define mset multiset
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define yes printf("Yes\n")
#define no printf("No\n")
#define all(x) (x).begin(),(x).end()
#define tmax(a,b) (a)=max((a),(b))
#define tmin(a,b) (a)=min((a),(b))
#define dbg(x) cout<<"line "<<__LINE__<<" : "<<#x<<"="<<x<<"\n"
#define cout(x) cout<<fixed<<setprecision(x)

using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;typedef pair<int,int> PII;
typedef vector<int> vi;typedef vector<PII> vp;
typedef array<int,4> a4;typedef vector<a4> va4;

namespace IO{
    template<typename type>
    inline void read(type &x){
        x=0;bool flag(0);char ch=getchar();
        while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        flag?x=-x:0;
    }
    template<typename type>
    inline void write(type x,string end=""){
        x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
        do Stack[++top]=x%10,x/=10;while(x);
        while(top) putchar(Stack[top--]|48);
        int len=end.length();for(int i=0;i<len;++i)putchar(end[i]);
    }
    template<typename type,typename...T>inline void read(type &x,T&...y){read(x),read(y...);}
}using namespace IO;

const int N=2e5+5;
int n,m,k,fa[N<<1],dep[N<<1],ans,tot;
vi tr[N<<2];
PII st[N<<1],e[N];

inline int find(int x){
	while(fa[x]!=x){
		x=fa[x];
	}
	return x;
}
inline void merge(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx==fy) return;
	if(dep[fx]>dep[fy]) swap(fx,fy);
	fa[fx]=fy;
	st[++tot]={fx,dep[fx]==dep[fy]};
	if(dep[fx]==dep[fy]){
		dep[fx]++;
	}
}
#define lc (rt<<1)
#define rc (rt<<1|1)
void update(int rt,int l,int r,int L,int R,int edge){
	if(L>R) return;
	if(L<=l&&r<=R){
		tr[rt].pb(edge);
		return;
	}
	int mid=l+r>>1;
	if(R<=mid){
		update(lc,l,mid,L,R,edge);
	}else if(L>mid){
		update(rc,mid+1,r,L,R,edge);
	}else{
		update(lc,l,mid,L,mid,edge);
		update(rc,mid+1,r,mid+1,R,edge);
	}
}
void pop(int lst,int res){
	while(tot>lst){
		PII res=st[tot];
		tot--;
		fa[res.fi]=res.fi;
		dep[res.fi]-=res.se;
	}
	ans-=res;
}
void work(int rt,int l,int r){
	int lst=tot,res=0;
	for(auto t:tr[rt]){
		int x,y,fx,fy;tie(x,y)=e[t];
		fx=find(x),fy=find(y);
		if(fx==fy){
			res++,ans++;
		}
		merge(fx,y+n),merge(x+n,fy);
	}
	if(l==r){
		if(!ans){
			yes;
		}else{
			no;
		}
		pop(lst,res);
		return;
	}
	int mid=l+r>>1;
	work(lc,l,mid);
	work(rc,mid+1,r);
	pop(lst,res);
}

signed main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0),cout.tie(0);
	read(n,m,k);
	for(int i=1;i<=n*2;i++){
		fa[i]=i;
	}
	for(int i=1,x,y,l,r;i<=m;i++){
		read(x,y,l,r);
		e[i]={x,y};
		l++;
		update(1,1,k,l,r,i);
	}
	work(1,1,k);
	return 0;
}
/*

*/

回复

0 条回复,欢迎继续交流。

正在加载回复...