QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#466455 | #5504. Flower Garden | BalintR | AC ✓ | 1917ms | 225652kb | C++20 | 5.8kb | 2024-07-07 20:33:56 | 2024-07-07 20:33:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cpx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}
template <typename T, typename U>
ostream& operator<<(ostream &os, pair<T, U> p){return os << "(" << p.fs << ", " << p.sn << ")";}
const int MQ = 1e5 + 5;
const int MN = 5e5 + 5;
int n, m, q;
pair<pii, pii> queries[MQ];
vi adjList[MN], gAdjList[MN], rgAdjList[MN], groups[MN];
int gi, *gid;
string res;
namespace Graph {
int tsz, qOffs;
struct Tree {
int offs;
int getNode(int i){
if(i >= tsz) return i-tsz;
else return offs+i;
}
vi query(int l, int r){
vi res;
for(l += tsz, r += tsz; l < r; l >>= 1, r >>= 1){
if(l & 1) res.pb(getNode(l++));
if(r & 1) res.pb(getNode(--r));
}
return res;
}
};
Tree upTree, downTree;
void genGraph(){
tsz = 2 << __lg(max(1, m-1));
upTree.offs = tsz;
downTree.offs = tsz*2;
qOffs = tsz*3;
FORR(i, tsz*2-1, 2){
adjList[upTree.getNode(i)].pb(upTree.getNode(i/2));
adjList[downTree.getNode(i/2)].pb(downTree.getNode(i));
}
cin >> q;
FR(i, q){
int a, b, c, d;
cin >> a >> b >> c >> d;
a--; c--;
queries[i] = {{a, b}, {c, d}};
//FOR(x, c, d) FOR(y, a, b) adjList[x].pb(y);
for(int n1 : upTree.query(c, d)) adjList[n1].pb(qOffs+i);
for(int n1 : downTree.query(a, b)) adjList[qOffs+i].pb(n1);
}
//n = m;
//return;
n = qOffs+q;
assert(n < MN);
}
}
namespace SCC {
vi stk;
int init[MN], lo[MN], ti;
void dfs(int n1){
stk.pb(n1);
init[n1] = lo[n1] = ++ti;
for(int n2 : adjList[n1]){
if(!init[n2]) dfs(n2);
if(init[n2] > 0) lo[n1] = min(lo[n1], lo[n2]);
}
if(init[n1] == lo[n1]){
while(true){
int n2 = stk.back();
stk.pop_back();
if(n2 < m) groups[gi].pb(n2);
init[n2] = ~gi;
if(n2 == n1) break;
}
gi++;
}
}
void genGroups(){
gid = init;
ti = 0;
FR(i, n) init[i] = 0;
FR(i, n) if(!init[i]) dfs(i);
assert(stk.empty());
}
}
namespace DAG {
int inDeg[MN], outDeg[MN], sm;
int qu[MN], ql, qr;
bool solve(){
//FR(i, gi) dbgArr(groups[i], SZ(groups[i]));
FR(i, gi) inDeg[i] = outDeg[i] = 0;
FR(n1, n) for(int n2 : adjList[n1]) if(gid[n1] != gid[n2]){
int a = ~gid[n1], b = ~gid[n2];
gAdjList[a].pb(b);
rgAdjList[b].pb(a);
outDeg[a]++;
inDeg[b]++;
//cerr << a << ' ' << b << endl;
}
m /= 3;
res.assign(m*3, 'R');
ql = qr = sm = 0;
FR(n1, n) if(!inDeg[n1]) qu[qr++] = n1;
while(ql < qr){
int n1 = qu[ql++];
if(sm + SZ(groups[n1]) > m+m) continue;
sm += SZ(groups[n1]);
for(int a : groups[n1]) res[a] = 'F';
for(int n2 : gAdjList[n1]) if(!--inDeg[n2]) qu[qr++] = n2;
}
if(sm >= m) return true;
res.assign(m*3, 'F');
ql = qr = sm = 0;
FR(n1, n) if(!outDeg[n1]) qu[qr++] = n1;
while(ql < qr){
int n1 = qu[ql++];
if(sm + SZ(groups[n1]) > m+m) continue;
sm += SZ(groups[n1]);
for(int a : groups[n1]) res[a] = 'R';
for(int n2 : rgAdjList[n1]) if(!--outDeg[n2]) qu[qr++] = n2;
}
if(sm >= m) return true;
return false;
}
}
void solve(){
cin >> m;
m *= 3;
Graph::genGraph();
SCC::genGroups();
if(!DAG::solve()) res = "";
}
int main(){
cin.sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while(t--){
solve();
if(res.empty()) cout << "NIE\n";
else {
cout << "TAK\n" << res << '\n';
m *= 3;
assert(m == SZ(res));
vi pref(m+1);
FR(i, m) pref[i+1] = pref[i] + res[i];
FR(i, q){
auto [a, b] = queries[i].fs;
auto [c, d] = queries[i].sn;
bool good1 = pref[b]-pref[a] == (b-a)*'R';
bool good2 = pref[d]-pref[c] == (d-c)*'F';
if(!good1 && !good2){
FR(j, q) cerr << queries[j] << '\n';
assert(false);
}
}
}
FR(i, n) adjList[i].clear();
FR(i, gi) gAdjList[i].clear(), rgAdjList[i].clear(), groups[i].clear();
gi = 0;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13792kb
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: 1882ms
memory: 107008kb
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: 1653ms
memory: 148768kb
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: 1608ms
memory: 139360kb
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 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFRFFFFFRFFRRFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...
result:
ok good!
Test #5:
score: 0
Accepted
time: 417ms
memory: 15940kb
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 FFFFFFFFRRRR NIE TAK FFFFRRRFFFFFFRR TAK FRRFRRFFFFFF TAK FFFFRRRRFFFF TAK FFFFFRFFFFFRRRR TAK FFRRRRFFFFFF TAK RRRRRFFFFFFFFFF NIE TAK FFFFRRRRFFFF NIE TAK FRFFFRRFF NIE TAK FFFFFRRRF TAK RRRRFFFFFFFF TAK FRRRFRRRRRRFFFF TAK RRRRFFFFFFFF NIE TAK ...
result:
ok good!
Test #6:
score: 0
Accepted
time: 1917ms
memory: 225652kb
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!