QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#521083 | #5504. Flower Garden | Z_drj | WA | 0ms | 28608kb | C++20 | 4.1kb | 2024-08-15 21:03:05 | 2024-08-15 21:03:06 |
Judging History
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, len;
Segment(int _l = 0, int _r = 0, int _len = 0) : l(_l), r(_r), len(_len) {}
}s[N];
int rtin, rtout;
int tot;
int ID[N];
void buildin(int &u, int l, int r) {
u = ++tot;
s[u].len = r - l + 1;
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; s[u].len = r - l + 1;
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) {
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 + u > 2 * n) continue;
vis[u] = true;
if ((cnt += 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(1, 1, n * 3, a, b, v), t.modifyin(1, 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, 0, sizeof(int) * t.tot);
cnt = 0; dfs(i);
if (cnt <= 2 * n) {
puts("TAK");
for (int j = 1; j <= n * 3; j++) {
printf("%c", vis[col[i]]? 'R': 'F');
}
puts("");
return;
}
}
}
if (!Topo()) {
puts("NIE");
} else {
puts("TAK");
for (int i = 1; i <= n * 3; 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: 0ms
memory: 28608kb
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 FFF NIE
result:
wrong answer odpowiedz ma za malo/duzo roz!