题解
把没有门的点缩成一个点
如果\(i->i + 1\)的钥匙大于\(i\),那么\(i\)不可以到\(i + 1\),连一条\(i\)到\(i + 1\)的边
如果\(i->i + 1\)的钥匙小于\(i\),那么\(i + 1\)不可以到\(i\),连一条\(i + 1\)到\(i\)的边
按照拓扑序计算,每次暴力向左和向右扩张,每次扩张碰到的区间和当前这个点扩张的区间没有重叠的部分,于是复杂度就相当于把一条链相邻两个点一个个合并起来,是\(O(n)\)的
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define MAXN 1000005
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;
}E[MAXN];
int N,M,P;
int a[MAXN],head[MAXN],sumE,L[MAXN],R[MAXN],id[MAXN],tot,deg[MAXN];
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
queue<int> Q;
void BFS() {
for(int i = 1 ; i <= tot ; ++i) {
if(!deg[i]) Q.push(i);
}
while(!Q.empty()) {
int u = Q.front();Q.pop();
while(1) {
bool flag = 0;
while(L[u] != 1 && a[L[u] - 1] >= L[u] && a[L[u] - 1] <= R[u]) {flag = 1;L[u] = L[id[L[u]] - 1];}
while(R[u] != N && a[R[u]] >= L[u] && a[R[u]] <= R[u]) {flag = 1;R[u] = R[id[R[u]] + 1];}
if(!flag) break;
}
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(!(--deg[v])) {
Q.push(v);
}
}
}
}
void Solve() {
read(N);read(M);read(P);
int x,y;
for(int i = 1 ; i <= M ; ++i) {
read(x);read(y);
a[x] = y;
}
int p = 1;
while(p <= N) {
id[p] = ++tot;L[tot] = p;
while(p != N && !a[p]) {++p;id[p] = tot;}
R[tot] = p;
++p;
}
for(int i = 1 ; i <= N ; ++i) {
if(!a[i]) continue;
if(a[i] > i) {add(id[i],id[i + 1]);deg[id[i + 1]]++;}
else {add(id[i + 1],id[i]);deg[id[i]]++;}
}
BFS();
int s,t;
for(int i = 1 ; i <= P ; ++i) {
read(s);read(t);
if(t >= L[id[s]] && t <= R[id[s]]) puts("YES");
else puts("NO");
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}