! AHOI / HNOI2018 game



What questions should see solution to a problem, so I gnaw food! ! ! ......

SOL:

We require pretreatment two arrays \ (l [i], r [i], i \) points to the left / right travel as far as that point, it is easy \ (O (1) \) answer questions

Of course, you can enumerate about, but in order to save time, we want to inherit information about points reachable

So it is very important to the enumeration order

Door \ ((the X-, the X-+ 1) \) , if key in \ ([1, x] \ ) , only in \ ([1, x] \ ) can be extended to \ ([x + 1, n ] \ ) , the first expansion \ (x + 1 \) re-expand \ (X \) , even side \ (. 1 + X → X \) ; conversely Similarly

A topological sort processing sequence, and then reach to enumerate

Initially plus point 0 degree is descending as we expand is to the left and then the right point point so quickly I will not tell you, in turn, overtime

Time complexity linear

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f==1?x:-x;
}
const int N=1e6+4;
int n,m,Q,key[N],l[N],r[N],deg[N];
vector<int>e[N];
inline void calc(int x){
	static int pl,pr;
	pl=pr=x;
	while(1){
		while(pl>1&&(!key[pl-1]||(pl<=key[pl-1]&&key[pl-1]<=pr)))pl=l[pl-1];
		while(pr<n&&(!key[pr]||(pl<=key[pr]&&key[pr]<=pr)))pr=r[pr+1];
		if(pl==l[x]&&pr==r[x])return;
		l[x]=pl;r[x]=pr;
	}
}
inline void topsort(){
	queue<int>q;
	for(int i=n;i;i--)if(!deg[i])q.push(i);
	while(!q.empty()){
		int x=q.front();q.pop();
		calc(x);
		for(auto v:e[x])
			if(!(--deg[v]))q.push(v);
	}
}
int main(){
	n=read();m=read();Q=read();
	for(int i=1;i<=n;i++)l[i]=r[i]=i;
	for(int i=1,x,y;i<=m;i++){
		x=read();key[x]=y=read();
		if(y<=x){e[x+1].push_back(x);deg[x]++;}
		else{e[x].push_back(x+1);deg[x+1]++;}
	}
	topsort();
	while(Q--){
		static int x,y;
		x=read();y=read();
		if(l[x]<=y&&y<=r[x])puts("YES");
		else puts("NO");
	}
	return (0-0);
}

Guess you like

Origin www.cnblogs.com/aurora2004/p/12581410.html