题解 [REOI-p1] 按摩

传送门

屑卡常题差不多得了

首先发现相邻点间连边后,原问题等价于动态图支持加删边,判断是否存在环
一个想法是 set 维护出相邻点间的边,然后线段树分治 + 可撤销并查集
这样是 \(O(n\log^2 n)\) 的,带 6 倍常数
赛时初始化少了挂了 3 个点然后又被卡常 2 个点

然后一个优化:对每行每列各建一个虚点,就不用动态维护相邻点间的边了
于是变成了 2 倍常数,并且不用 set 了

然后再来一个优化在这个题中用不了:

  • 关于所有查询在修改后的并查集:若对每个集合维护其根,合并时直接将一个根并到另一个根上
    然后听说可以证明在满足上述条件时复杂度是均摊 \(O(1)\)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define fir first
#define sec second
#define pb push_back
#define ll long long
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m, k;
#undef unix
int vln[N], vcol[N];
set<pair<int, int>> ln[N], col[N];
pair<int, int> pre[N], op[N];
map<pair<int, int>, int> mp, vis, id;
int unix[N], uniy[N], xsiz, ysiz, tot;

namespace seg{
	int dsu[N<<1], siz[N<<1];
	int tl[N<<2], tr[N<<2];
	vector<int> del[N<<2];
	vector<pair<int, int>> add[N<<2];
	inline int find(int p) {return dsu[p]==p?p:find(dsu[p]);}
	#define tl(p) tl[p]
	#define tr(p) tr[p]
	void build(int p, int l, int r) {
		tl(p)=l; tr(p)=r;
		if (l==r) return ;
		int mid=(l+r)>>1;
		build(p<<1, l, mid);
		build(p<<1|1, mid+1, r);
	}
	void upd(int p, int l, int r, pair<int, int> val) {
		if (l<=tl(p)&&r>=tr(p)) {add[p].pb(val); return ;}
		int mid=(tl(p)+tr(p))>>1;
		if (l<=mid) upd(p<<1, l, r, val);
		if (r>mid) upd(p<<1|1, l, r, val);
	}
	void query(int p, bool any) {
		for (auto it:add[p]) {
			int s=find(it.fir), t=find(it.sec);
			if (s==t) any=1;
			if (any) break;
			if (siz[s]>siz[t]) swap(s, t);
			del[p].pb(s);
			siz[dsu[s]=t]+=siz[s];
		}
		if (tl(p)==tr(p)) puts(any?"Yes":"No");
		else query(p<<1, any), query(p<<1|1, any);
		while (del[p].size()) {
			auto it=del[p].back();
			siz[find(it)]-=siz[it];
			dsu[it]=it;
			del[p].pop_back();
		}
	}
}

void add(int x, int y, int tim) {
	// cout<<"add: "<<x<<' '<<y<<' '<<tim<<endl;
	// assert(mp.find({x, y})==mp.end());
	mp[{x, y}]=tim;
	// cout<<"return"<<endl;
}
void del(int x, int y, int tim) {
	// cout<<"del: "<<x<<' '<<y<<' '<<tim<<endl;
	// assert(mp.find({x, y})!=mp.end());
	seg::upd(1, mp[{x, y}], tim-1, {x, y});
	mp.erase({x, y});
	// cout<<"return"<<endl;
}

signed main()
{
	n=read(); m=read();
	for (int i=1,x,y; i<=m; ++i) {
		x=read(); y=read();
		pre[i]={x, y};
		unix[++xsiz]=x; uniy[++ysiz]=y;
	}
	sort(pre+1, pre+m+1);
	m=unique(pre+1, pre+m+1)-pre-1;
	k=read();
	for (int i=1,x,y; i<=k; ++i) {
		x=read(); y=read();
		op[i]={x, y};
		unix[++xsiz]=x; uniy[++ysiz]=y;
	}
	sort(unix+1, unix+xsiz+1);
	sort(uniy+1, uniy+ysiz+1);
	xsiz=unique(unix+1, unix+xsiz+1)-unix-1;
	ysiz=unique(uniy+1, uniy+ysiz+1)-uniy-1;
	// cout<<"unix: "; for (int i=1; i<=xsiz; ++i) cout<<unix[i]<<' '; cout<<endl;
	// cout<<"uniy: "; for (int i=1; i<=ysiz; ++i) cout<<uniy[i]<<' '; cout<<endl;
	for (int i=1; i<=m; ++i) {
		pre[i].fir=lower_bound(unix+1, unix+xsiz+1, pre[i].fir)-unix;
		pre[i].sec=lower_bound(uniy+1, uniy+ysiz+1, pre[i].sec)-uniy;
		// cout<<"pre: ("<<pre[i].fir<<','<<pre[i].sec<<")"<<endl;
		id[pre[i]]=++tot; vis[pre[i]]=1;
		// ln[pre[i].sec].insert({pre[i].fir, tot});
		// col[pre[i].fir].insert({pre[i].sec, tot});
	}
	for (int i=1; i<=k; ++i) {
		op[i].fir=lower_bound(unix+1, unix+xsiz+1, op[i].fir)-unix;
		op[i].sec=lower_bound(uniy+1, uniy+ysiz+1, op[i].sec)-uniy;
		// cout<<"op: ("<<op[i].fir<<','<<op[i].sec<<")"<<endl;
		if (id.find(op[i])==id.end()) id[op[i]]=++tot;
	}
	for (int i=1; i<=ysiz; ++i) vln[i]=++tot;
	for (int i=1; i<=xsiz; ++i) vcol[i]=++tot;
	seg::build(1, 1, k);
	for (int i=1; i<=tot; ++i) seg::siz[seg::dsu[i]=i]=1;
	// for (int i=1,lst; i<=ysiz; ++i) if (ln[i].size())
	// 	for (auto& it:ln[i])
	// 		if (it==*ln[i].begin()) lst=it.sec;
	// 		else add(lst, it.sec, 1), lst=it.sec;
	// // cout<<"ln: "; for (auto it:ln[4]) cout<<"("<<it.fir<<','<<it.sec<<") "; cout<<endl;
	// for (int i=1,lst; i<=xsiz; ++i) if (col[i].size())
	// 	for (auto& it:col[i])
	// 		if (it==*col[i].begin()) lst=it.sec;
	// 		else add(lst, it.sec, 1), lst=it.sec;
	for (int i=1; i<=m; ++i) {
		add(id[pre[i]], vln[pre[i].sec], 1);
		add(id[pre[i]], vcol[pre[i].fir], 1);
	}
	for (int i=1; i<=k; ++i) {
		// cout<<endl;
		// cout<<"i: "<<i<<endl;
		int id=::id[op[i]];
		// cout<<"op: ("<<op[i].fir<<','<<op[i].sec<<")"<<endl;
		// cout<<"ln: "; for (auto it:ln[op[i].sec]) cout<<"("<<it.fir<<','<<it.sec<<") "; cout<<endl;
		// cout<<"col: "; for (auto it:col[op[i].fir]) cout<<"("<<it.fir<<','<<it.sec<<") "; cout<<endl;
		if (vis.find(op[i])!=vis.end()) {
			// cout<<"case 1"<<endl;
			// ln[op[i].sec].erase({op[i].fir, id});
			// col[op[i].fir].erase({op[i].sec, id});
			// set<pair<int, int>>::iterator it;
			// it=ln[op[i].sec].lower_bound({op[i].fir, 0});
			// if (it!=ln[op[i].sec].end()) del(id, it->sec, i);
			// if (it!=ln[op[i].sec].begin()) {
			// 	del((--it)->sec, id, i);
			// 	int t1=it->sec;
			// 	if (++it!=ln[op[i].sec].end()) add(t1, it->sec, i);
			// }
			// it=col[op[i].fir].lower_bound({op[i].sec, 0});
			// if (it!=col[op[i].fir].end()) del(id, it->sec, i);
			// if (it!=col[op[i].fir].begin()) {
			// 	del((--it)->sec, id, i);
			// 	int t1=it->sec;
			// 	if (++it!=col[op[i].fir].end()) add(t1, it->sec, i);
			// }
			del(id, vln[op[i].sec], i);
			del(id, vcol[op[i].fir], i);
			vis.erase(op[i]);
		}
		else {
			// // cout<<"case 2"<<endl;
			// set<pair<int, int>>::iterator it;
			// it=ln[op[i].sec].lower_bound({op[i].fir, 0});
			// // cout<<"find: "<<(it->sec)<<endl;
			// if (it!=ln[op[i].sec].begin() && it!=ln[op[i].sec].end()) {
			// 	int t1=(--it)->sec;
			// 	del(t1, (++it)->sec, i);
			// }
			// if (it!=ln[op[i].sec].end()) add(id, it->sec, i); //, cout<<"pos1"<<endl;
			// if (it!=ln[op[i].sec].begin()) add((--it)->sec, id, i); //, cout<<"pos2"<<endl;

			// it=col[op[i].fir].lower_bound({op[i].sec, 0});
			// if (it!=col[op[i].fir].begin() && it!=col[op[i].fir].end()) {
			// 	int t1=(--it)->sec;
			// 	del(t1, (++it)->sec, i);
			// }
			// if (it!=col[op[i].fir].end()) add(id, it->sec, i); //, cout<<"pos3"<<endl;
			// if (it!=col[op[i].fir].begin()) add((--it)->sec, id, i); //, cout<<"pos4"<<endl;
			// ln[op[i].sec].insert({op[i].fir, id});
			// col[op[i].fir].insert({op[i].sec, id});
			add(id, vln[op[i].sec], i);
			add(id, vcol[op[i].fir], i);
			vis[op[i]]=i;
		}
	}
	for (auto& it:mp) seg::upd(1, it.sec, k, it.fir);
	seg::query(1, 0);

	return 0;
}
posted @ 2022-08-01 21:14  Administrator-09  阅读(2)  评论(0编辑  收藏  举报