「HNOI/AHOI2018」游戏

传送门
Luogu

解题思路

这是一道 (O(n^2)) 暴力加上 ( ext{random_shuffle}) 优化 什么鬼 就可以 ( ext{AC}) 的题。
但还是要讲一下 (O(n)) 的正解。
算了我不讲了咕咕咕,可以去这里

细节注意事项

  • 有点难想,但是并不难写

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= (c == '-'), c = getchar();
	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
	s = f ? -s : s;
}

const int _ = 1000010;

int n, m, q, key[_];
int L[_], R[_], co[_], col = 1, dgr[_];
int tot, head[_], nxt[_], ver[_];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }

inline void expand(int x) {
	while (1) {
		int flag = 0;
		while (L[x] > 1 && L[x] <= key[L[x] - 1] && key[L[x] - 1] <= R[x])
			flag = 1, L[x] = L[co[L[x] - 1]];
		while (R[x] < n && L[x] <= key[R[x]] && key[R[x]] <= R[x])
			flag = 1, R[x] = R[co[R[x] + 1]];
		if (!flag) return;
	}
}

inline void toposort() {
	static queue < int > Q;
	for (rg int i = 1; i <= col; ++i)
		if (!dgr[i]) Q.push(i);
	while (!Q.empty()) {
		int u = Q.front(); Q.pop();
		expand(u);
		for (rg int i = head[u]; i; i = nxt[i])
			if (!--dgr[ver[i]]) Q.push(ver[i]);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	read(n), read(m), read(q);
	for (rg int x, y, i = 1; i <= m; ++i)
		read(x), read(y), key[x] = y;
	for (rg int i = 1; i <= n; ++i) {
		if (key[i - 1] != 0) ++col;
		co[i] = col, R[col] = i;
		if (!L[col]) L[col] = i;
	}
	for (rg int i = 1; i <= n; ++i) {
		if (key[i] == 0) continue;
		if (co[i] < co[key[i]])
			Add_edge(co[i], co[i] + 1), ++dgr[co[i] + 1];
		else
			Add_edge(co[i] + 1, co[i]), ++dgr[co[i]];
	}
	toposort();
	for (rg int s, t, i = 1; i <= q; ++i)
		read(s), read(t), puts(L[co[s]] <= t && t <= R[co[s]] ? "YES" : "NO");
	return 0;
}

完结撒花 (qwq)

原文地址:https://www.cnblogs.com/zsbzsb/p/11745862.html