QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#83931 | #5504. Flower Garden | skittles1412 | AC ✓ | 17218ms | 305328kb | C++17 | 8.3kb | 2023-03-04 14:03:16 | 2023-03-04 14:03:19 |
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;
dbg(st);
for (auto& a : st) {
if (vis[a]) {
continue;
}
cst.clear();
dfs(a, vis, cst, igraph);
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() {
{
SCC scc(16, {{8, 6},
{8, 5},
{5, 14},
{14, 5},
{14, 3},
{3, 14},
{5, 13},
{6, 1},
{6, 3},
{3, 11},
{11, 13},
{1, 11}});
dbg(scc.comps);
// return;
}
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); });
}
for (auto& [u, v] : ts.edges) {
cerr << u << " " << v << endl;
}
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);
}
}
dbg(ccomp);
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 * 2, 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: 0ms
memory: 3592kb
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: 17218ms
memory: 305328kb
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: 0
Accepted
time: 12469ms
memory: 245624kb
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:
TAK FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...
result:
ok good!
Test #4:
score: 0
Accepted
time: 12365ms
memory: 241536kb
input:
10 33333 100000 2646 2646 6430 6446 82226 82231 15128 15132 877 877 85831 88474 51389 79196 37573 37573 38030 38030 14248 14280 63032 68489 81074 81074 46468 46468 7403 7487 19864 20058 97979 97979 71640 71833 8558 8558 12121 12434 82481 82481 32901 32901 1899 2162 65312 77886 75969 75974 87983 8798...
output:
TAK FFFFFFFFFFFFFFFFFFFFFFFFFFFFFRRFFFFRFFRRFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...
result:
ok good!
Test #5:
score: 0
Accepted
time: 3258ms
memory: 3540kb
input:
87005 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 1 1 1 3 2 3 1 2 1 1 2 2 2 2 1 1 1 2 1 3 1 1 1 1 1 3 4 20 3 5 6 12 4 5 7 11 1 1 2 2 3 4 7 12 3 5 10 10 3 5 8 8 4 4 9 11 4 4 7 7 1 1 9 10 3 4 6 9 3 5 11 12 3 3 7 9 3 5 2 2 4 5 2 2 1 1 7 11 1 1 10 10 3 5 7 8 4 4 2 2 1 1 2 2 4 5 8 10 4 12 11 ...
output:
TAK RRF NIE TAK RFF TAK FFR NIE TAK RFRRRRFRFFRR TAK FFRRFFRRRRRR NIE TAK FFFFRRRRRFRRRRR TAK FRRRRRRRFFFR TAK FFFRRRRRRRRF TAK FFFFFRRRRRRRRRR TAK FFRRRRRRRRFF TAK FFRRFFFRRRRRRRR NIE TAK FFFFRRRRRRRR NIE TAK RRFFFRRFF NIE TAK FFFRRRRRR TAK RRRRRRFFFRRF TAK RRFFRFRFFFFFRFF TAK RRRRRRRFFFFR NIE TAK ...
result:
ok good!
Test #6:
score: 0
Accepted
time: 10219ms
memory: 274476kb
input:
10 33333 99999 60859 99999 1 60858 78724 99999 1 78723 42986 99999 1 42985 35108 99999 1 35107 63158 99999 1 63157 86977 99999 1 86976 13576 99999 1 13575 48277 99999 1 48276 89102 99999 1 89101 92657 99999 1 92656 25093 99999 1 25092 2353 99999 1 2352 81748 99999 1 81747 51216 99999 1 51215 75815 9...
output:
NIE TAK RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
result:
ok good!