QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#466452 | #5504. Flower Garden | BalintR | WA | 2ms | 16056kb | C++20 | 5.8kb | 2024-07-07 20:32:44 | 2024-07-07 20:32:44 |
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++];
}
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++];
}
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;
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 16056kb
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:
NIE NIE
result:
wrong answer zla odpowiedz!