QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#466397 | #5504. Flower Garden | BalintR | ML | 0ms | 9768kb | C++20 | 5.0kb | 2024-07-07 19:43:41 | 2024-07-07 19:43:42 |
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 MN = 5e5 + 5;
int n, m;
vi adjList[MN], gAdjList[MN], rgAdjList[MN], groups[MN];
int gi, *gid;
string res;
namespace Graph {
int q, 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--;
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;
void dfs1(int n1){
if(sm + SZ(groups[n1]) > m+m) return;
sm += SZ(groups[n1]);
for(int a : groups[n1]) res[a] = 'F';
for(int n2 : gAdjList[n1]) if(!--inDeg[n2]) dfs1(n2);
}
void dfs2(int n1){
if(sm + SZ(groups[n1]) > m+m) return;
sm += SZ(groups[n1]);
for(int a : groups[n1]) res[a] = 'R';
for(int n2 : rgAdjList[n1]) if(!--outDeg[n2]) dfs2(n2);
}
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 << ~gid[n1] << ' ' << ~gid[n2] << endl;
}
m /= 3;
sm = 0;
res.assign(m*3, 'R');
FR(i, gi) if(!inDeg[i]) dfs1(i);
//dbg(sm);
if(sm >= m) return true;
sm = 0;
res.assign(m*3, 'F');
FR(i, gi) if(!outDeg[i]) dfs2(i);
//dbg(sm);
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';
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: 9768kb
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: -100
Memory Limit Exceeded
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...