一次小 G 和小 H 在玩寻宝游戏,有 nnn 个房间排成一列,编号为 1,2,…,n,相邻房间之间都有 111 道门。其中一部分门上有锁(所以须要对应的钥匙才能开门),其他的门都能直接打开。html
如今小 G 告诉了小 H 每把锁的钥匙在哪一个房间里(每把锁有且只有一把钥匙),并做出 ppp 次指示:第 iii 次让小 H 从第 SiS_iSi 个房间出发,去第 TiT_iTi 个房间寻宝。可是小 G 有时会故意在指令里放入死路,而小 H 也不想浪费多余的体力去尝试,因而想事先调查清楚每次的指令是否存在一条通路。ios
你是否能为小 H 做出解答呢?dom
第一行三个整数nnn,mmm,ppp,表明共有 nnn 个房间,mmm 道门上了锁,以及 ppp 个询问。测试
接下来 mmm 行每行有两个整数xxx,yyy,表明第 xxx 到第 x+1x + 1x+1 个房间的门上有把锁,而且这把锁的钥匙被放在了第 yyy 个房间里。输入保证 xxx 不重复。优化
接下来 ppp 行,其中第 iii 行是两个整数 SiS_iSi,TiT_iTi,表明一次询问。ui
输出 mmm 行,每行一个大写的 YES
或 NO
分别表明能或不能到达。spa
5 4 5 1 3 2 2 3 1 4 4 2 5 3 5 4 5 2 1 3 1
YES NO YES YES NO
第一个询问 S=2S = 2S=二、T=5T = 5T=5 的一条可行路线是:2→3→2→1→2→3→4→52 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 52→3→2→1→2→3→4→5。code
此组样例知足特性:y≤xy \le xy≤x 恒成立htm
7 5 4 2 2 3 3 4 2 5 3 6 6 2 1 3 4 3 7 4 5
YES YES NO NO
第一个询问 222 和 111 房间之间没有锁因此为一条通路。blog
测试点编号 | n | m | 其余特性 |
---|---|---|---|
1 | ≤1000 \le 1000≤1000 | ≤1000 \le 1000≤1000 | 无 |
2 | ≤1000 \le 1000≤1000 | ≤1000 \le 1000≤1000 | 无 |
3 | ≤105 \le 10^5≤105 | ≤105 \le 10^5≤105 | y≤xy \le xy≤x 恒成立 |
4 | ≤105 \le 10^5≤105 | ≤105 \le 10^5≤105 | y≤xy \le xy≤x 恒成立 |
5 | ≤105 \le 10^5≤105 | ≤105 \le 10^5≤105 | 无 |
6 | ≤105 \le 10^5≤105 | ≤105 \le 10^5≤105 | 无 |
7 | ≤106 \le 10^6≤106 | ≤106 \le 10^6≤106 | y≤xy \le xy≤x 恒成立 |
8 | ≤106 \le 10^6≤106 | ≤106 \le 10^6≤106 | y≤xy \le xy≤x 恒成立 |
9 | ≤106 \le 10^6≤106 | ≤106 \le 10^6≤106 | 无 |
10 | ≤106 \le 10^6≤106 | ≤106 \le 10^6≤106 | 无 |
对于全部数据,保证 1≤n,p≤1061 \le n,p \le 10^61≤n,p≤106,0≤m<n0 \le m < n0≤m<n,1≤x,y,Si,Ti<n1 \le x, y, S_i,T_i < n1≤x,y,Si,Ti<n,保证 xxx 不重复。
因为本题输入文件较大,建议在程序中使用读入优化。
/* 一个点可以扩张的区间是由它周围的点转移过来的 随机化更新的顺序,每次更新进行左右扩张便可 */ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<algorithm> #define maxn 1000010 using namespace std; int n,L[maxn],R[maxn],p[maxn],rd[maxn]; int qread(){ int i=0,j=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')j=-1;ch=getchar();} while(ch<='9'&&ch>='0'){i=i*10+ch-'0';ch=getchar();} return i*j; } void extend(int i){ bool flag=1; while(flag){ flag=0; while(L[i]>1 && (p[L[i]-1]==0||(L[i]<=p[L[i]-1]&&p[L[i]-1]<=R[i]))) flag=1,L[i]=min(L[i],L[L[i]-1]),R[i]=max(R[i],R[R[i]-1]); while(R[i]<n && (p[R[i]]==0||(L[i]<=p[R[i]]&&p[R[i]]<=R[i]))) flag=1,L[i]=min(L[i],L[L[i]+1]),R[i]=max(R[i],R[R[i]+1]); } } int main(){ srand(time(0)); int m,q,x,y; n=qread();m=qread();q=qread(); for(int i=1;i<=m;i++){ x=qread(); p[x]=qread(); } for(int i=1;i<=n;i++)L[i]=R[i]=i; for(int i=1;i<=n;i++)rd[i]=i; random_shuffle(rd+1,rd+n+1); for(int i=1;i<=n;i++)extend(rd[i]); while(q--){ x=qread();y=qread(); if(L[x]<=y&&y<=R[x])puts("YES"); else puts("NO"); } return 0; }