QOJ4420 Range Reachability Query 题解
看到 DAG 可达性问题和这个数据范围就知道肯定只能是
直接处理可达点比较难转移,考虑记录一个点
用 bitset
那么转移就是
考虑现在复杂度瓶颈是预处理
直接在线维护
时间复杂度
我们发现不一定要把所有询问一起处理。其实分开处理也可以。每
实测
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 50003, B = 512;
int t, n, u, v, dd[N], d[N], m, qy, x[N], y[N], l, r, tot;
queue<int> q;
bitset<B> ck, can[N << 1], f[N];
vector<pii> g[N];
vector<int> cl[N << 1], cr[N << 1];
void topo() {
for (int i = 1; i <= n; i++) {
d[i] = dd[i];
if (!d[i]) q.push(i);
}
for (int u; !q.empty(); ) {
u = q.front();
q.pop();
for (pii e : g[u]) {
int v = e.fi, id = e.se;
f[v] |= (f[u] & can[id]);
d[v]--;
if (!d[v]) q.push(v);
}
}
}
void clear() {
for (int i = 1; i <= m + 1; i++) {
cl[i].clear(), cr[i].clear();
}
for (int i = 1; i <= n; i++) {
f[i] = 0;
}
ck = 0;
tot = 0;
}
void solve() {
cin >> n >> m >> qy;
for (int i = 1; i <= n; i++) {
g[i].clear();
dd[i] = 0;
}
for (int i = 1; i <= m; i++) {
cin >> u >> v;
g[u].push_back({v, i});
dd[v]++;
}
tot = 0;
for (int tq = 1; tq <= qy; tq++) {
cin >> x[tot] >> y[tot] >> l >> r;
cl[l].push_back(tot);
cr[r + 1].push_back(tot);
tot++;
if (tq == qy or tot == B) {
for (int i = 1; i <= m; i++) {
for (int j : cl[i]) ck[j] = 1;
for (int j : cr[i]) ck[j] = 0;
can[i] = ck;
}
for (int i = 0; i < tot; i++) {
f[x[i]][i] = 1;
}
topo();
for (int i = 0; i < tot; i++) {
if (f[y[i]][i]) cout << "YES\n";
else cout << "NO\n";
}
clear();
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) solve();
}