QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#521120#5504. Flower GardenZ_drjWA 3ms24276kbC++204.1kb2024-08-15 21:32:472024-08-15 21:32:50

Judging History

你现在查看的是最新测评结果

  • [2024-08-15 21:32:50]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:24276kb
  • [2024-08-15 21:32:47]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <algorithm>

using i64 = long long;

const int N = 1E6 + 5;

int n, q;

bool tag[N];
std::vector<int> g[N], G[N], rG[N];

#define ls(u) u << 1
#define rs(u) u << 1 | 1

struct SegmentTree {
	struct Segment {
		int l, r;
		Segment(int _l = 0, int _r = 0) : l(_l), r(_r) {}
	}s[N];

	int rtin, rtout;
	int tot;
	int ID[N];

	void buildin(int &u, int l, int r) {
		u = ++tot;
		if (l == r) {
			ID[l] = u, tag[u] = true;
			return;
		}
		int mid = l + r >> 1;
		buildin(s[u].l, l, mid), buildin(s[u].r, mid + 1, r);
		g[u].push_back(s[u].l), g[u].push_back(s[u].r);
	}

	void buildout(int &u, int l, int r) {
		if (l == r) {
			u = ID[l];
			return;
		}
		u = ++tot;
		int mid = l + r >> 1;
		buildout(s[u].l, l, mid), buildout(s[u].r, mid + 1, r);
		g[s[u].l].push_back(u), g[s[u].r].push_back(u);
	}

	void modifyin(int u, int l, int r, int ql, int qr, int x) {
		if (ql <= l && r <= qr) {
			g[ID[x]].push_back(u);
			return;
		}
		int mid = l + r >> 1;
		if (ql <= mid) {modifyin(s[u].l, l, mid, ql, qr, x);}
		if (qr > mid) {modifyin(s[u].r, mid + 1, r, ql, qr, x);}
	}
	
	void modifyout(int u, int l, int r, int ql, int qr, int x) {
		if (ql <= l && r <= qr) {
			g[u].push_back(ID[x]);
			return;
		}
		int mid = l + r >> 1;
		if (ql <= mid) {modifyout(s[u].l, l, mid, ql, qr, x);}
		if (qr > mid) {modifyout(s[u].r, mid + 1, r, ql, qr, x);}
	}

	void clear() {
		tot = 0;
	}
}t;

#undef ls
#undef rs

int dfn_cnt, scc_cnt;
int dfn[N], low[N];
int col[N], siz[N];

std::stack<int> s;
bool vis[N];

void tarjan(int u) {
	s.push(u); vis[u] = true;
	low[u] = dfn[u] = ++dfn_cnt;

	for (auto v : g[u]) {
		if (!dfn[v]) {
			tarjan(v);
			low[u] = std::min(low[u], low[v]);
		} else if (vis[v]) {
			low[u] = std::min(low[u], dfn[v]);
		}
	}

	if (low[u] == dfn[u]) {
		++scc_cnt;
		int x;
		do {
			x = s.top(); s.pop();
			vis[x] = false;
			col[x] = scc_cnt;
			siz[scc_cnt] += tag[x];
		} while (x != u);
	}
}

int cnt;
void dfs(int u) {
	if (vis[u]) {return;}
	vis[u] = true, cnt += siz[u];
	for (auto v : G[u]) {
		dfs(v);
	}
}

int in[N];

bool Topo() {
	memset(vis, 0, sizeof(vis));
	std::queue<int> q;

	for (int i = 1; i <= scc_cnt; i++) {
		if (!in[i]) {
			q.push(i);
		}
	}

	int cnt = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		if (cnt + siz[u] > 2 * n) continue;
		vis[u] = true;
		if ((cnt += siz[u]) > n) {
			return true;
		}
		for (auto v : rG[u]) {
			if (--in[v]) {
				q.push(v);
			}
		}
	}

	return false;
}

void clear() {
	for (int i = 1; i <= t.tot; i++) {
		g[i].clear(), G[i].clear(), rG[i].clear();
	}
	memset(dfn, 0, sizeof(int) * t.tot), memset(low, 0, sizeof(int) * t.tot);
	memset(col, 0, sizeof(int) * t.tot), memset(siz, 0, sizeof(int) * t.tot);
	memset(vis, 0, sizeof(int) * t.tot);
	t.clear();
}

void solve() {
	scanf("%d %d", &n, &q);

	t.buildin(t.rtin, 1, n * 3), t.buildout(t.rtout, 1, n * 3);

	for (int i = 1; i <= q; i++) {
		int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
		int v = ++t.tot;
		t.modifyout(t.rtout, 1, n * 3, a, b, v), t.modifyin(t.rtin, 1, n * 3, c, d, v);
	}

	for (int i = 1; i <= t.tot; i++) {
		if (!dfn[i]) {
			tarjan(i);
		}
	}

	for (int i = 1; i <= t.tot; i++) {
		for (auto v : g[i]) {
			if (col[i] != col[v]) {
				G[col[i]].push_back(col[v]);
				rG[col[v]].push_back(col[i]);
				++in[col[i]];
			}
		}
	}

	for (int i = 1; i <= scc_cnt; i++) {
		if (siz[i] >= n) {
			memset(vis + 1, 0, sizeof(int) * scc_cnt);
			cnt = 0; dfs(i);
			if (cnt <= 2 * n) {
				puts("TAK");
				for (int j = 1; j <= t.tot; j++) {
					if (tag[j]) {
						printf("%c", vis[col[j]]? 'R': 'F');
					}
				}
				puts("");
				return;
			}
		}
	}

	if (!Topo()) {
		puts("NIE");
	} else {
		puts("TAK");
		for (int i = 1; i <= t.tot; i++) {
			if (tag[i]) {
				printf("%c", vis[col[i]]? 'R': 'F');
			}
		}
		puts("");
	}
}

int main() {
	int T; scanf("%d", &T);
	while (T--) {
		solve();
		clear();
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 24276kb

input:

2
1 3
1 1 2 2
1 2 3 3
1 1 3 3
1 3
1 1 2 2
2 2 3 3
3 3 1 1

output:

TAK
FFR
NIE

result:

wrong answer odpowiedz nie spelnia wymogow!