QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#91808 | #5504. Flower Garden | Denisov | AC ✓ | 8320ms | 89368kb | C++20 | 10.5kb | 2023-03-29 16:56:02 | 2023-03-29 16:56:04 |
Judging History
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
inline bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
inline bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL
const int max_n = 5e5 + 18 * 1e5 * 4, inf = 1000111222;
template<typename Edge>
class GraphIterator {
public:
class OutgoingEdges {
public:
OutgoingEdges(const GraphIterator *g, int l, int r): g(g), l(l), r(r) {
}
const pair<int, Edge>* begin() const {
if (l == r) {
return 0;
}
return &g->edges[l];
}
const pair<int, Edge>* end() const {
if (l == r) {
return 0;
}
return &g->edges[r];
}
private:
int l, r;
const GraphIterator *g;
};
void clear() {
edges.clear();
start.clear();
prepared = false;
}
void add_edge(int from, const Edge &e) {
assert(!prepared && from >= 0);
edges.push_back({from, e});
}
void prepare() {
assert(!prepared);
int n = 0;
for (const auto &e : edges) {
n = max(n, e.first);
}
n += 2;
start.resize(n);
for (const auto &e : edges) {
++start[e.first + 1];
}
for (int i = 1; i < n; ++i) {
start[i] += start[i - 1];
}
auto counter = start;
for (int i = 0; i < (int)edges.size(); i++) {
assert(edges[i].first < len(counter));
if (start[edges[i].first] <= i && i < counter[edges[i].first]) {
continue;
}
int pos = counter[edges[i].first];
while (i != pos) {
assert(pos < (int)edges.size());
++counter[edges[i].first];
swap(edges[i], edges[pos]);
assert(edges[i].first < len(counter));
pos = counter[edges[i].first];
}
++counter[edges[i].first];
}
for (int i = 0; i < (int)edges.size(); i++) {
if (start[edges[i].first] <= i && i < start[edges[i].first + 1]) {
}
else {
assert(false);
}
}
/*for (auto &i : edges) {
LOG(i.first, i.second);
}
LOG("end");*/
prepared = true;
}
OutgoingEdges operator [] (int from) const {
assert(prepared);
if (from < 0 || from + 1 >= start.size()) {
return {this, 0, 0};
}
return {this, start[from], start[from + 1]};
}
vector<pair<int, Edge>> edges;
vector<int> start;
bool prepared = false;
};
GraphIterator<int> g, gr;
pii e[max_n];
pii e1[max_n];
int n, num = 0;
vector <int> t, t1;
inline void upd (int v, int tl, int tr, int l, int r, int id, bool from) {
if (l > r) return;
if (tl == l && tr == r) {
if (from) {
g.add_edge(id, t1[v]);
//e[num++] = {id, t1[v]};
}
else {
g.add_edge(t[v], id);
//e[num++] = {t[v], id};
}
return;
}
int tm = (tl + tr) >> 1;
upd(v << 1, tl, tm, l, min(r, tm), id, from);
upd(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r, id, from);
}
int mx;
inline int build (int v, int tl, int tr) {
umax(mx, 3 * n + v + 1);
t[v] = 3 * n + v;
if (tl == tr) {
g.add_edge(tr, 3 * n + v);
//e[num++] = {tr, 3 * n + v};
return 3 * n + v;
}
int tm = (tl + tr) >> 1;
int L = build(v << 1, tl, tm);
int R = build(v << 1 | 1, tm + 1, tr);
g.add_edge(L, 3 * n + v);
g.add_edge(R, 3 * n + v);
//e[num++] = {L, 3 * n + v};
//e[num++] = {R, 3 * n + v};
return 3 * n + v;
}
inline int build1 (int v, int tl, int tr) {
t1[v] = mx++;
if (tl == tr) {
g.add_edge(t1[v], tr);
//e[num++] = {t1[v], tr};
return t1[v];
}
int tm = (tl + tr) >> 1;
int L = build1(v << 1, tl, tm);
int R = build1(v << 1 | 1, tm + 1, tr);
g.add_edge(t1[v], L);
g.add_edge(t1[v], R);
//e[num++] = {t1[v],L};
//e[num++] = {t1[v],R};
return t1[v];
}
vector <int> used, order, cl, w, p, p1;
inline void dfs (int v) {
used[v] = 1;
auto cur = g[v];
for (auto to : cur) {
//if (e[i].first != v) break;
//int to = e[i].second;
if (!used[to.second]) {
dfs(to.second);
}
}
order.pb(v);
}
inline void dfs1 (int v, int c) {
used[v] = 2;
cl[v] = c;
w[c] += v < 3 * n;
auto cur = gr[v];
for (auto to : cur) {
//if (e1[i].first != v) break;
//int to = e1[i].second;
if (used[to.second] == 1) {
dfs1(to.second, c);
}
}
}
vector <int> ans;
inline void color (int v) {
ans[v] = 1;
auto cur = g[v];
for (auto to : cur) {
//if (e[i].first != v) break;
//int to = e[i].second;
if (!ans[to.second]) {
color(to.second);
}
}
}
inline void color1 (int v) {
ans[v] = 0;
auto cur = gr[v];
for (auto to : cur) {
//if (e[i].first != v) break;
//int to = e[i].second;
if (ans[to.second]) {
color1(to.second);
}
}
}
inline void test_case () {
ans.clear();
p.clear();
p1.clear();
t.clear();
t1.clear();
cl.clear();
w.clear();
used.clear();
g.clear();
gr.clear();
order.clear();
num = 0;
mx = 0;
int q;
cin >> n >> q;
t.resize(3 * 4 * n);
t1.resize(3 * 4 * n);
build(1, 0, 3 * n - 1);
build1(1, 0, 3 * n - 1);
for (int i = 0, a, b, c, d; i < q; i++) {
cin >> a >> b >> c >> d;
--a, --b, --c, --d;
int cur = mx;
upd(1, 0, 3 * n - 1, c, d, cur, false);
upd(1, 0, 3 * n - 1, a, b, cur, true);
++mx;
}
//LOG("here");
int N = mx;
for (auto &i : g.edges) {
gr.add_edge(i.second, i.first);
}
g.prepare();
gr.prepare();
/*for (int i = 0; i < num; i++) {
e1[i] = {e[i].second, e[i].first};
}
sort(e, e + num);
sort(e1, e1 + num);
p.resize(N);
p1.resize(N);
for (int i = 0, j = 0; i < N; i++) {
while (j < num && e[j].first < i) {
++j;
}
p[i] = j;
}
for (int i = 0, j = 0; i < N; i++) {
while (j < num && e1[j].first < i) {
++j;
}
p1[i] = j;
}*/
used.resize(N);
for (int i = 0; i < N; i++) {
if (!used[i]) {
dfs(i);
}
}
reverse(all(order));
w.resize(N);
cl.resize(N);
int cmp = 0;
for (int i : order) {
if (used[i] == 1) {
dfs1(i, cmp);
++cmp;
}
}
for (int i = 0; i < cmp; i++) {
if (w[i] > 2 * n) {
cout << "NIE\n";
return;
}
}
/*for (int it = 0; it < num; it++) {
pii i = e[it];
e[it] = {cl[i.first], cl[i.second]};
i = e1[it];
e1[it] = {cl[i.first], cl[i.second]};
}
sort(e, e + num);
sort(e1, e1 + num);
for (int i = 0, j = 0; i < cmp; i++) {
while (j < num && e[j].first < i) {
++j;
}
p[i] = j;
}
for (int i = 0, j = 0; i < cmp; i++) {
while (j < num && e1[j].first < i) {
++j;
}
p1[i] = j;
}*/
g.clear();
for (auto &i : gr.edges) {
g.add_edge(cl[i.second], cl[i.first]);
}
gr.clear();
for (auto &i : g.edges) {
gr.add_edge(i.second, i.first);
}
g.prepare();
gr.prepare();
ans.resize(cmp);
/// 1 - rose
/// 0 - violet
for (int i = 0; i < cmp; i++) {
if (w[i] > n) {
int cnt = 0;
color(i);
string answer(3 * n, 'F');
for (int j = 0; j < 3 * n; j++) {
if (ans[cl[j]]) {
answer[j] = 'R';
++cnt;
}
}
if (cnt >= n && cnt <= 2 * n) {
cout << "TAK\n";
cout << answer << '\n';
return;
}
cnt = 0;
ans.assign(cmp, 1);
color1(i);
answer = string(3 * n, 'R');
for (int j = 0; j < 3 * n; j++) {
if (!ans[cl[j]]) {
++cnt;
answer[j] = 'F';
}
}
if (!(cnt >= n && cnt <= 2 * n)) {
cout << "NIE\n";
return;
}
cout << "TAK\n";
cout << answer << '\n';
return;
}
}
order.clear();
used.clear();
used.resize(cmp);
for (int i = 0; i < cmp; i++) {
if (!used[i]) {
dfs(i);
}
}
int s = 0;
for (int v : order) {
s += w[v];
ans[v] = 1;
if (s >= n) {
assert(s <= 2 * n);
break;
}
}
assert(s >= n);
string answer(3 * n, 'F');
int cnt = 0;
for (int i = 0; i < 3 * n; i++) {
if (ans[cl[i]]) {
++cnt;
answer[i] = 'R';
}
}
assert(cnt >= n && cnt <= 2 * n);
cout << "TAK\n";
cout << answer << '\n';
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for (int test = 1; test <= t; test++) {
test_case();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3492kb
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 RFF NIE
result:
ok good!
Test #2:
score: 0
Accepted
time: 8320ms
memory: 89368kb
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: 6561ms
memory: 65724kb
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: 6475ms
memory: 65108kb
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: 926ms
memory: 3500kb
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 RFF NIE TAK RFF TAK RRF NIE TAK RFRRRFFFFFFF TAK FFRFFFFRFFRR NIE TAK RRFFFRFFFFFFFRR TAK FRRFRRFFFFFF TAK FFRFRRRFFFFF TAK FFRFFFFFFFFRRRR TAK FFRRRRFFFFFF TAK RRRRFFFRFFFFFFF NIE TAK RFRFRRFFFFFF NIE TAK RRFFFRRFF NIE TAK FFFFFFRRR TAK FRRRFFFFFRFF TAK FRRRFRRRRRRFFFF TAK RRRRFFFFFFFF NIE TAK ...
result:
ok good!
Test #6:
score: 0
Accepted
time: 5633ms
memory: 79172kb
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!