CodeForces 1416D Graph and Queries

洛谷传送门

CF 传送门

思路

考虑离线,按时间倒序进行操作,删边变成加边。

然而若按时间倒序进行操作,就不知道哪些点已经 \(p_u = 0\) 了。.

可以按加边的顺序建出 \(\mathrm{Kruskal}\) 重构树。则可以倍增找到在 \(t\) 时刻连通的祖先,它的所有叶子子结点即为在 \(t\) 时刻与 \(u\) 连通的点。

区间求最大值,单点修改,线段树即可。

时间复杂度 \(O(m \log m + (n + q) \log n)\)

代码

code
/*

p_b_p_b txdy
AThousandSuns txdy
Wu_Ren txdy
Appleblue17 txdy

*/

#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 500100;
const int logn = 22;
const int inf = 0x3f3f3f3f;

int n, m, q, a[maxn], head[maxn], len, fa[maxn];
int f[maxn][logn], val[maxn];
int times, dfn[maxn], rnk[maxn], mnd[maxn], mxd[maxn];
bool mk[maxn];
pii G[maxn], gg[maxn], tree[maxn << 2];

struct edge {
	int to, next;
} edges[maxn << 1];

struct query {
	int op, x, t;
} qq[maxn];

void add_edge(int u, int v) {
	edges[++len].to = v;
	edges[len].next = head[u];
	head[u] = len;
}

int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void dfs(int u, int fa) {
	bool flag = 1;
	mnd[u] = inf;
	mxd[u] = -inf;
	for (int i = head[u]; i; i = edges[i].next) {
		int v = edges[i].to;
		if (v == fa) {
			continue;
		}
		f[v][0] = u;
		dfs(v, u);
		mnd[u] = min(mnd[u], mnd[v]);
		mxd[u] = max(mxd[u], mxd[v]);
		flag = 0;
	}
	if (flag) {
		mnd[u] = mxd[u] = dfn[u] = ++times;
		rnk[times] = u;
	}
}

int jump(int x, int k) {
	for (int i = 19; ~i; --i) {
		if (f[x][i] && val[f[x][i]] <= k) {
			x = f[x][i];
		}
	}
	return x;
}

void pushup(int x) {
	tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
}

void build(int rt, int l, int r) {
	if (l == r) {
		tree[rt] = make_pair(a[rnk[l]], l);
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}

void update(int rt, int l, int r, int x, int y) {
	if (l == r) {
		tree[rt].fst = y;
		return;
	}
	int mid = (l + r) >> 1;
	if (x <= mid) {
		update(rt << 1, l, mid, x, y);
	} else {
		update(rt << 1 | 1, mid + 1, r, x, y);
	}
	pushup(rt);
}

pii query(int rt, int l, int r, int ql, int qr) {
	if (ql <= l && r <= qr) {
		return tree[rt];
	}
	pii res = make_pair(-1e9, -1);
	int mid = (l + r) >> 1;
	if (ql <= mid) {
		res = max(res, query(rt << 1, l, mid, ql, qr));
	}
	if (qr > mid) {
		res = max(res, query(rt << 1 | 1, mid + 1, r, ql, qr));
	}
	return res;
}

void solve() {
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	for (int i = 1; i <= n * 2; ++i) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &G[i].fst, &G[i].scd);
		mk[i] = 1;
	}
	for (int i = 1; i <= q; ++i) {
		scanf("%d%d", &qq[i].op, &qq[i].x);
		if (qq[i].op == 2) {
			mk[qq[i].x] = 0;
		}
	}
	int tote = 0;
	for (int i = 1; i <= m; ++i) {
		if (mk[i]) {
			gg[++tote] = G[i];
		}
	}
	for (int i = q; i; --i) {
		if (qq[i].op == 2) {
			gg[++tote] = G[qq[i].x];
		} else {
			qq[i].t = tote;
		}
	}
	int totn = n;
	for (int i = 1; i <= tote; ++i) {
		int u = gg[i].fst, v = gg[i].scd;
		int x = find(u), y = find(v);
		if (x != y) {
			int z = ++totn;
			fa[x] = fa[y] = z;
			add_edge(z, x);
			add_edge(z, y);
			val[z] = i;
		}
	}
	for (int i = 1; i <= totn; ++i) {
		if (fa[i] == i) {
			dfs(i, -1);
		}
	}
	for (int j = 1; (1 << j) <= totn; ++j) {
		for (int i = 1; i <= totn; ++i) {
			f[i][j] = f[f[i][j - 1]][j - 1];
		}
	}
	build(1, 1, n);
	for (int i = 1; i <= q; ++i) {
		int op = qq[i].op, x = qq[i].x;
		if (op == 1) {
			int u = jump(x, qq[i].t);
			pii res = query(1, 1, n, mnd[u], mxd[u]);
			printf("%d\n", res.fst);
			update(1, 1, n, res.scd, 0);
		}
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}
posted @ 2022-07-28 21:04  zltzlt  阅读(28)  评论(0编辑  收藏  举报