P4899 [IOI2018] werewolf 狼人
给定一个有nnn个点mmm条边的无向图,有QQQ个询问
每次输入S,E,L,RS, E, L, RS,E,L,R,表示你在SSS点出发,要到EEE点,且初始时你是人形态,你只能走[L,n][L, n][L,n]的点,
但是我们要以狼的形态走到终点,这个时候只能走[1,R][1, R][1,R]之间的点,
也就是说我们要在[L,R][L, R][L,R]的某个点变身,问能否从S−>ES->ES−>E。
一个比较容易想到的做法:
显然,初始状态我们要满足S∈[L,n],E∈[1,R]S \in [L, n], E \in[1, R]S∈[L,n],E∈[1,R],否则就没有讨论的意义了。
假设满足S∈[L,n],E∈[1,R]S \in[L, n], E \in[1,R]S∈[L,n],E∈[1,R],
我们对每条边按照{u,v,w=min(u,v)}\{u, v, w = min(u, v)\}{u,v,w=min(u,v)}这样的方式,跑一个降序kruskalkruskalkruskal树,然后从SSS点出发,向上跳,找到使点权不小于LLL,能到达的点uuu,
同样的我们对每条边按照{u,v,w=max(u,v}\{u, v, w = max(u, v\}{u,v,w=max(u,v}的方式,跑一个升序kruskalkruskalkruskal树,然后从EEE出发,向上跳,找到使点权不大于RRR,能到达的点vvv,
接下来我们只需判断uuu所代表的点集SuS_uSu, vvv所代表的点集SvS_vSv,判断这两个点集有没有交集即可。
设图一进栈dfsdfsdfs序为L1L1L1,出栈dfsdfsdfs序为R1R1R1,同理图二为L2,R2L2, R2L2,R2,
设x[i]x[i]x[i]为在图一dfsdfsdfs序为iii的点编号,y[i]y[i]y[i]为,点编号为iii的点在图二的dfsdfsdfs序,接下来我们只要在区间[L1u,R1u][L1_u, R1_u][L1u,R1u]的主席树上查找区间[L2v,R2v][L2_v, R2_v][L2v,R2v]是否有值即可。
#include <bits/stdc++.h>using namespace std;const int N = 4e5 + 10;struct Res {int u, v, w;bool operator < (const Res &t) const {return w < t.w;}
}edge[N];int n, m, Q, x[N], y[N];int root[N], ls[N << 5], rs[N<< 5], sum[N << 5], num;void update(int &rt, int pre, int l, int r, int x, int v) {rt = ++num;ls[rt] = ls[pre], rs[rt] = rs[pre], sum[rt] = sum[pre] + v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x, v);}else {update(rs[rt], rs[pre], mid + 1, r, x, v);}
}int query(int rt1, int rt2, int l, int r, int L, int R) {// cout << l << " " << r << "\n";if (l >= L && r <= R) {return sum[rt2] - sum[rt1];}int ans = 0, mid = l + r >> 1;if (L <= mid) {ans += query(ls[rt1], ls[rt2], l, mid, L, R);}if (R > mid) {ans += query(rs[rt1], rs[rt2], mid + 1, r, L, R);}return ans;
}struct Kruskal {int head[N], to[N], nex[N], cnt = 1;int ff[N], value[N], fa[N][21], l[N], r[N], rk[N], tot, nn;void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;}int find(int rt) {return ff[rt] == rt ? rt : ff[rt] = find(ff[rt]);}void dfs(int rt, int f) {fa[rt][0] = f, l[rt] = ++tot, rk[tot] = rt;for (int i = 1; i <= 20; i++) {fa[rt][i] = fa[fa[rt][i - 1]][i - 1];}for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs(to[i], rt);}r[rt] = tot;}void kruskal(int rev) {sort(edge + 1, edge + 1 + m);if (rev) {reverse(edge + 1, edge + 1 + m);}for (int i = 1; i < N; i++) {ff[i] = i;}nn = n;for (int i = 1, cur = 1; i <= m && cur < n; i++) {int u = find(edge[i].u), v = find(edge[i].v);if (u != v) {cur++, nn++;ff[u] = ff[v] = nn;value[nn] = edge[i].w;add(nn, u), add(nn, v);if (u <= n) {value[u] = edge[i].w;}if (v <= n) {value[v] = edge[i].w;}}}dfs(nn, 0);}bool judge(int u, int x, int flag) {return flag ? value[u] >= x : value[u] <= x;}int work(int u, int x, int flag) {for (int i = 20; i >= 0; i--) {if (fa[u][i] && judge(fa[u][i], x, flag)) {u = fa[u][i];}}return u;}
}a, b;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d %d", &n, &m, &Q);for (int i = 1; i <= m; i++) {scanf("%d %d", &edge[i].u, &edge[i].v);edge[i].u++, edge[i].v++;}for (int i = 1; i <= m; i++) {edge[i].w = min(edge[i].v, edge[i].u);}a.kruskal(1);for (int i = 1; i <= m; i++) {edge[i].w = max(edge[i].v, edge[i].u);}b.kruskal(0);for (int i = 1; i < 2 * n; i++) {x[i] = a.rk[i];y[i] = b.l[i];}for (int i = 1; i < 2 * n; i++) {root[i] = root[i - 1];if (x[i] <= n) {update(root[i], root[i - 1], 1, 2 * n, y[x[i]], 1);}}while (Q--) {int u, v, L, R;scanf("%d %d %d %d", &u, &v, &L, &R);u++, v++, L++, R++;u = a.work(u, L, 1), v = b.work(v, R, 0);int l1 = a.l[u], r1 = a.r[u], l2 = b.l[v], r2 = b.r[v];printf("%d\n", query(root[l1 - 1], root[r1], 1, 2 * n, l2, r2) != 0);}return 0;
}