1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
const int maxn = 2e5 + 5; const int maxm = maxn << 1;
int n, Q, S, d; int a[maxn]; set< pair<int, int> > s;
int head[maxn], cnt; struct Edge { int to, next, w; }edg[maxn << 1]; void add(int u,int v,int w) { edg[++ cnt] = (Edge) { v, head[u], w }, head[u] = cnt; }
int mxk[maxn];
void dfs(int p,int pre,int mx) { mxk[p] = mx; for(int i = head[p];i;i = edg[i].next) { int to = edg[i].to, w = edg[i].w; if(to == pre) continue; dfs(to, p, max(mx, w)); } }
vector<int> vc[maxn];
int fa[maxn];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void merge(int u,int v) { int s1 = getfa(u), s2 = getfa(v); if(s1 == s2) return ; fa[s1] = s2; }
signed main() {
int i, j; r1(n, Q, S, d); for(i = 1; i <= n; ++ i) r1(a[i]), s.insert(make_pair(a[i], i)); for(i = 1; i <= n; ++ i) fa[i] = i; while(1) { for(i = 1; i <= n; ++ i) vc[i].clear(); int flag(1); for(i = 1; i <= n; ++ i) { if(getfa(i) != getfa(1)) flag = 0; vc[getfa(i)].push_back(i); } if(flag) break;
for(i = 1; i <= n; ++ i) if(vc[i].size()) { int x(0), y(0), w(2e9);
for(int v : vc[i]) s.erase(make_pair(a[v], v));
for(int v : vc[i]) { auto it = s.lower_bound(make_pair(a[v] + d, 0)); int tmp; if(it != s.end()) { tmp = abs(d - abs(a[v] - it->first)); if(tmp < w) x = v, y = it->second, w = tmp; } if(it != s.begin()) { -- it; tmp = abs(d - abs(a[v] - it->first)); if(tmp < w) x = v, y = it->second, w = tmp; }
it = s.lower_bound(make_pair(a[v] - d, 0));
if(it != s.end()) { tmp = abs(d - abs(a[v] - it->first)); if(tmp < w) x = v, y = it->second, w = tmp; } if(it != s.begin()) { -- it; tmp = abs(d - abs(a[v] - it->first)); if(tmp < w) x = v, y = it->second, w = tmp; }
} for(int v : vc[i]) s.insert(make_pair(a[v], v)); if(getfa(x) != getfa(y)) { add(x, y, w), add(y, x, w);
merge(x, y); } }
}
dfs(S, 0, 0); while(Q --) { int pos, K; r1(pos, K); if(mxk[pos] <= K) puts("YES"); else puts("NO"); } return 0; }
|