QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99208 | #5504. Flower Garden | Johnsonloy | WA | 49ms | 284600kb | C++14 | 6.2kb | 2023-04-21 17:01:42 | 2023-04-21 17:01:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
const int maxn = 4e6 + 10;
namespace IO {
void openfile() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
}
inline int read() {
int x = 0, f = 0;
char c = getchar();
while (!isdigit(c))
f |= c == '-', c = getchar();
while (isdigit(c))
x = x * 10 + c - '0', c = getchar();
if (f)
x = -x;
return x;
}
} // namespace IO
using namespace IO;
int tot, n, m;
int rt1, rt2, top, cnt, tim;
int dfn[maxn], low[maxn], bl[maxn], vis[maxn], sta[maxn];
int x[maxn], y[maxn], bian, in[maxn], sz[maxn], ans[maxn];
vector<int> e[maxn], g[maxn], pos[maxn];
void add(int xx, int yy) {
e[xx].push_back(yy);
x[++bian] = xx, y[bian] = yy;
}
struct node {
int l, r, ls, rs;
} t[maxn];
void build1(int& rt, int l, int r) {
if (!rt)
rt = ++tot;
t[rt] = {l, r};
if (l == r)
return;
int mid = (l + r) >> 1;
build1(t[rt].ls, l, mid), build1(t[rt].rs, mid + 1, r);
add(rt, t[rt].ls), add(rt, t[rt].rs);
}
void build2(int& rt, int l, int r) {
if (!rt)
rt = ++tot;
t[rt] = {l, r};
if (l == r)
return add(rt - rt2 + 1, rt), void();
int mid = (l + r) >> 1;
build2(t[rt].ls, l, mid), build2(t[rt].rs, mid + 1, r);
add(t[rt].ls, rt), add(t[rt].rs, rt);
}
void init() {
for (int i = 1; i <= tot; i++)
t[i] = {0, 0, 0, 0}, dfn[i] = low[i] = bl[i] = in[i] = sz[i] = vis[i] = 0, e[i].clear(),
g[i].clear(), pos[i].clear();
tot = tim = bian = cnt = top = rt1 = rt2 = 0;
}
void jia1(int rt, int l, int r, int x, int y, int d) {
if (x <= l && r <= y)
return add(d, rt), void();
int mid = (l + r) >> 1;
if (x <= mid)
jia1(t[rt].ls, l, mid, x, y, d);
if (mid < y)
jia1(t[rt].rs, mid + 1, r, x, y, d);
}
void jia2(int rt, int l, int r, int x, int y, int d) {
if (x <= l && r <= y)
return add(rt, d), void();
int mid = (l + r) >> 1;
if (x <= mid)
jia2(t[rt].ls, l, mid, x, y, d);
if (mid < y)
jia2(t[rt].rs, mid + 1, r, x, y, d);
}
void tarjan(int x) {
vis[x] = 1, sta[++top] = x;
low[x] = dfn[x] = ++tim;
for (auto v : e[x]) {
if (!dfn[v])
tarjan(v), low[x] = min(low[x], low[v]);
else if (vis[v])
low[x] = min(low[x], dfn[v]);
}
if (dfn[x] == low[x]) {
int y;
cnt++;
do {
y = sta[top--];
vis[y] = 0;
bl[y] = cnt;
} while (x != y);
}
return;
}
void topo() {
queue<int> q;
for (int i = 1; i <= cnt; i++)
if (!in[i])
q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
sta[++top] = x;
for (auto v : e[x]) {
in[v]--;
if (!in[v])
q.push(v);
}
}
}
void dfs(int x) {
vis[x] = 1;
for (auto v : e[x])
if (!vis[v])
dfs(v);
}
void topo2(int x) {
queue<int> q;
q.push(x);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 1;
for (auto v : g[x])
if (!vis[v])
q.push(v);
}
}
void solve() {
n = read(), m = read();
build1(rt1, 1, n * 3), build2(rt2, 1, n * 3);
for (int i = 1; i <= m; i++) {
int l1 = read(), r1 = read(), l2 = read(), r2 = read();
int d1 = ++tot;
jia2(rt2, 1, n * 3, l1, r1, d1), jia1(rt1, 1, n * 3, l2, r2, d1);
}
for (int i = 1; i <= tot; i++)
if (!dfn[i])
tarjan(i);
for (int i = 1; i <= tot; i++)
e[i].clear(), g[i].clear();
for (int i = 1; i <= bian; i++) {
if (bl[x[i]] == bl[y[i]])
continue;
e[bl[x[i]]].push_back(bl[y[i]]);
g[bl[y[i]]].push_back(bl[x[i]]);
in[bl[y[i]]]++;
}
for (int i = 1; i < rt1; i++)
if (t[i].l == t[i].r)
++sz[bl[i]], pos[bl[i]].push_back(t[i].l);
topo();
int sum = 0, flag = 0;
for (int i = 1; i <= top; i++) {
sum += sz[i];
if (sum >= n && sum <= n * 2) {
flag = i;
break;
}
}
if (flag) {
for (int i = 1; i <= top; i++)
for (auto v : pos[i])
ans[v] = (i <= flag);
puts("TAK");
for (int i = 1; i <= n * 3; i++)
if (ans[i])
putchar('F');
else
putchar('R');
puts("");
return;
}
for (int i = 1; i <= top; i++)
if (sz[i] >= n) {
for (int i = 1; i <= top; i++)
vis[i] = 0;
dfs(i);
sum = 0;
for (int i = 1; i <= top; i++)
if (vis[i])
sum += sz[i];
if (sum >= n && sum <= n * 2) {
for (int i = 1; i <= top; i++)
for (auto v : pos[i])
ans[v] = vis[i];
puts("TAK");
for (int i = 1; i <= n * 3; ++i)
if (ans[i])
putchar('F');
else
putchar('R');
puts("");
return;
}
for (int i = 1; i <= top; i++)
vis[i] = 0;
topo2(i), sum = 0;
for (int i = 1; i <= top; i++)
if (vis[i])
sum += sz[i];
if (sum >= n && sum <= n * 2) {
for (int i = 1; i <= top; i++)
for (auto v : pos[i])
ans[v] = vis[i];
puts("TAK");
for (int i = 1; i <= n * 3; ++i)
if (ans[i])
putchar('F');
else
putchar('R');
puts("");
return;
}
}
puts("NIE");
}
signed main() {
openfile();
int T = read();
while (T--)
init(), solve();
return 0;
}
/*
1
5 2
5 7 12 13
6 13 1 11
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 49ms
memory: 284600kb
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:
NIE NIE
result:
wrong answer zla odpowiedz!