QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#83929 | #5504. Flower Garden | skittles1412 | WA | 19598ms | 305208kb | C++17 | 7.7kb | 2023-03-04 13:36:51 | 2023-03-04 13:36:52 |
Judging History
answer
#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << " ";
}
out << arr[i];
}
return out << "]";
}
struct N {
int u;
friend ostream& operator<<(ostream& out, const N& n) {
if (n.u < 0) {
return out << "!" << (~n.u);
}
return out << " " << n.u;
}
};
void rassert(bool b) {
if (!b) {
cout << (new int[5])[10];
}
}
struct ST {
int n;
vector<int> v;
ST(int n) : n(n), v(4 * n) {}
template <typename Cb1, typename Cb2>
void build(int o, int l, int r, const Cb1& cb1, const Cb2& cb2) {
if (l == r) {
v[o] = cb2(l);
return;
}
int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
build(lc, l, mid, cb1, cb2);
build(rc, mid + 1, r, cb1, cb2);
v[o] = cb1(v[lc], v[rc]);
}
template <typename Cb1, typename Cb2>
void build(const Cb1& cb1, const Cb2& cb2) {
build(1, 0, n - 1, cb1, cb2);
}
template <typename Cb>
void query(int o, int l, int r, int ql, int qr, const Cb& cb) const {
if (ql <= l && r <= qr) {
cb(v[o]);
return;
}
int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
if (ql <= mid) {
query(lc, l, mid, ql, qr, cb);
}
if (mid < qr) {
query(rc, mid + 1, r, ql, qr, cb);
}
}
template <typename Cb>
void query(int l, int r, const Cb& cb) const {
query(1, 0, n - 1, l, r, cb);
}
};
struct TS {
int n = 0;
vector<pair<int, int>> edges;
int new_var() {
return n++;
}
void implies(int u, int v) {
dbg(N {u}, N {v});
auto norm = [&](int u) -> int {
if (u < 0) {
return 2 * (~u) + 1;
}
return 2 * u;
};
u = norm(u);
v = norm(v);
edges.emplace_back(u, v);
edges.emplace_back(v ^ 1, u ^ 1);
// rassert(u < n && v < n);
}
};
void dfs(int u,
vector<bool>& vis,
vector<int>& st,
const vector<vector<int>>& graph) {
if (vis[u]) {
return;
}
vis[u] = true;
for (auto& v : graph[u]) {
dfs(v, vis, st, graph);
}
st.push_back(u);
}
struct SCC {
int n;
vector<int> comp;
vector<pair<int, int>> nedges;
vector<vector<int>> comps, graph, igraph;
SCC(int n, const vector<pair<int, int>>& edges)
: n(n), comp(n), graph(n), igraph(n) {
for (auto& [u, v] : edges) {
graph[u].push_back(v);
igraph[v].push_back(u);
}
vector<bool> vis(n);
vector<int> st;
for (int i = 0; i < n; i++) {
if (vis[i]) {
continue;
}
dfs(i, vis, st, graph);
}
reverse(begin(st), end(st));
fill(begin(vis), end(vis), false);
vector<int> cst;
for (auto& a : st) {
if (vis[a]) {
continue;
}
cst.clear();
dfs(a, vis, cst, graph);
for (auto& b : cst) {
comp[b] = sz(comps);
}
comps.push_back(cst);
}
for (auto& [u, v] : edges) {
nedges.emplace_back(comp[u], comp[v]);
}
sort(begin(nedges), end(nedges));
nedges.erase(unique(begin(nedges), end(nedges)), nedges.end());
nedges.erase(remove_if(begin(nedges), end(nedges),
[&](const auto& p) -> bool {
return p.first == p.second;
}),
nedges.end());
}
};
void solve() {
int n, q;
cin >> n >> q;
n *= 3;
int queries[q][4];
for (auto& [l0, r0, l1, r1] : queries) {
cin >> l0 >> r0 >> l1 >> r1;
l0--;
r0--;
l1--;
r1--;
}
TS ts;
for (int i = 0; i < n; i++) {
ts.new_var();
}
ST st0(n), st1(n);
st0.build(
[&](int lc, int rc) -> int {
int o = ts.new_var();
ts.implies(~lc, ~o);
ts.implies(~rc, ~o);
return o;
},
[&](int ind) -> int { return ~ind; });
st1.build(
[&](int lc, int rc) -> int {
int o = ts.new_var();
ts.implies(~lc, ~o);
ts.implies(~rc, ~o);
return o;
},
[&](int ind) -> int { return ind; });
for (auto& [l0, r0, l1, r1] : queries) {
dbg("Q");
int b = ts.new_var();
st0.query(l0, r0, [&](int o) -> void { ts.implies(~o, ~b); });
st1.query(l1, r1, [&](int o) -> void { ts.implies(~b, o); });
}
SCC scc(ts.n * 2, ts.edges);
auto& comps = scc.comps;
int m = sz(comps), ccnt[m] {};
for (int i = 0; i < m; i++) {
for (auto& a : comps[i]) {
ccnt[i] += a < 2 * n && a % 2 == 0;
}
dbg(i, ccnt[i], comps[i]);
}
int bcomp = -1;
for (int i = 0; i < m; i++) {
if (ccnt[i] >= n / 3 + 2) {
bcomp = i;
}
}
if (bcomp != -1) {
vector<int> ccomp;
for (auto& a : comps[bcomp]) {
if (a < 2 * n && a % 2 == 0) {
ccomp.push_back(a / 2);
}
}
bool found = false;
auto go = [&](const vector<vector<int>>& g, bool bxor) -> void {
if (found) {
return;
}
vector<int> vtot;
vector<bool> vis(ts.n * 2);
for (auto& a : ccomp) {
dfs(a, vis, vtot, g);
}
int tvis = 0;
for (int i = 0; i < n; i++) {
tvis += vis[2 * i];
}
if (n / 3 <= tvis && tvis <= 2 * n / 3) {
cout << "TAK" << endl;
found = true;
for (int i = 0; i < n; i++) {
cout << "RF"[vis[2 * i] ^ bxor];
}
cout << endl;
}
};
go(scc.graph, false);
go(scc.igraph, true);
if (!found) {
cout << "NIE" << endl;
}
return;
}
cout << "TAK" << endl;
bool ans[n] {};
int psum = 0;
for (int i = m - 1; i >= 0; i--) {
for (auto& a : comps[i]) {
if (a < 2 * n && a % 2 == 0) {
ans[a / 2] = true;
psum++;
}
}
if (psum < n / 3) {
continue;
}
assert(psum <= 2 * n / 3);
for (int j = 0; j < n; j++) {
cout << "RF"[ans[j]];
}
cout << endl;
return;
}
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
int tcs;
cin >> tcs;
while (tcs--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3468kb
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 RRF NIE
result:
ok good!
Test #2:
score: 0
Accepted
time: 19598ms
memory: 305208kb
input:
10 33333 100000 28701 40192 93418 95143 95902 97908 78378 78461 36823 44196 22268 23996 23977 24786 33315 48829 83965 90411 4923 8445 20235 21177 32543 47454 29598 35414 72477 73049 2014 12632 42163 46466 64305 65518 98825 99552 32331 41625 92772 96224 26500 54122 76990 77126 18249 20335 31165 36080...
output:
NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE
result:
ok good!
Test #3:
score: -100
Wrong Answer
time: 13189ms
memory: 220316kb
input:
10 33333 100000 15207 33614 66276 66276 97173 97173 67589 73960 19673 36626 65207 65207 89825 98169 27079 27079 56067 56966 7560 7560 18170 35477 18752 18752 32621 36748 34460 34460 61595 61700 14117 14117 32395 36710 9064 9064 13172 13172 1728 4640 40462 41878 47171 47171 76965 82414 5767 5767 9225...
output:
NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE
result:
wrong answer zla odpowiedz!