QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99332 | #5504. Flower Garden | Fanch100 | WA | 6318ms | 135556kb | C++14 | 5.0kb | 2023-04-21 21:45:58 | 2023-04-21 21:46:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int RN = 100010;
const int N = 500010;
const int M = N*10;
int n, Q;
int head[N], pre[M], ver[M], tot, from[M];
void add(int x,int y){
ver[++tot]=y; pre[tot]=head[x]; head[x]=tot; from[tot]=x;
// if (tot>=M-10000) printf("tot=%d\n",tot);
// printf("x=%d y=%d\n",x,y);
}
int idx;
int spl[N];
int w[N];
int ls[N], rs[N];
void build(int &p,int l,int r,bool z){
if (l==r) {
if (!w[l]) w[l]=++idx, spl[idx]=l;
p=w[l];
return;
}
p=++idx;
// printf("p=%d l=%d r=%d\n",p,l,r);
int mid=(l+r)>>1;
build(ls[p],l,mid,z); build(rs[p],mid+1,r,z);
if (!(z&1)) add(ls[p],p),add(rs[p],p);
else add(p,ls[p]),add(p,rs[p]);
}
void update(int p,int l,int r,int pl,int pr,int d,bool z){
if (l<=pl && pr<=r){
if (!(z&1)) add(p,d);
else add(d,p);
return;
}
int mid=(pl+pr)>>1;
if (mid>=l) update(ls[p],l,r,pl,mid,d,z);
if (mid+1<=r) update(rs[p],l,r,mid+1,pr,d,z);
}
int dfn[N], low[N], tp, st[N], top, scc[N], sc, siz[N];
bool ins[N];
vector<int> inc[N];
void tarjan(int x){
dfn[x]=low[x]=++tp;
st[++top]=x; ins[x]=1;
for (int i=head[x]; i; i=pre[i]){
int v=ver[i];
if (!dfn[v]){
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (ins[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x]){
sc++;
while(1){
int qh=st[top--];
scc[qh]=sc;
if (spl[qh]) siz[sc]++, inc[sc].push_back(spl[qh]);
ins[qh]=0;
if (qh==x) break;
}
}
}
vector<int> G0[N], G1[N];
set<pair<int,int> > s;
int dg[N];
bool vis[N];
vector<int> ans;
int bfs(int x){
for (int i=1;i<=sc;++i) vis[i]=0; ans.clear();
queue<int> q;
q.push(x);
while(q.size()){
int x=q.front(); q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i : inc[x]) ans.push_back(i);
for (int v : G0[x]) q.push(v);
}
if (ans.size()<=n/3*2) return 0;
for (int i=1;i<=sc;++i) vis[i]=0; ans.clear();
// vis[x]=0;
q.push(x);
while(q.size()){
int x=q.front(); q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i : inc[x]) ans.push_back(i);
for (int v : G1[x]) q.push(v);
}
if (ans.size()<=n/3*2) return 1;
return 2;
}
void clear(){for (int i=1;i<=idx;++i) dfn[i]=dg[i]=spl[i]=w[i]=siz[i]=head[i]=0, G0[i].clear(), G1[i].clear(), inc[i].clear(); tp=0; tot=0; idx=0; sc=0; s.clear();}
bool anss[N], flag;
void solve(){
cin>>n>>Q; n*=3;
int nn=n/3;
tot=0; idx=0;
int rt0=0, rt1=0;
build(rt0,1,n,0);
build(rt1,1,n,1);
// printf("tot=%d\n",tot);
while(Q--){
int l1,r1,l2,r2; cin>>l1>>r1>>l2>>r2;
// if (Q==100000-1 && l1==15207) flag=1;
++idx;
update(rt0,l1,r1,1,n,idx,0);
update(rt1,l2,r2,1,n,idx,1);
// printf("l=%d r=%d l=%d r=%d\n",l1,r1,l2,r2);
}
// printf("idx=%d tot=%d\n",idx,tot);
for (int i=1;i<=idx;++i){
if (!dfn[i]) tarjan(i);
}
// puts("Yes");
for (int i=1;i<=tot;++i){
if (scc[from[i]]!=scc[ver[i]]){
int x=scc[from[i]], y=scc[ver[i]];
if (s.find({x,y})!=s.end()) continue;
s.insert({x,y});
G0[y].push_back(x); G1[x].push_back(y);
dg[y]++;
// printf("un x=%d y=%d dg=%d\n",x,y,dg[y]);
}
}
// for (int i=1;i<=idx;++i) printf("i=%d sc=%d\n",i,scc[i]);
// for (int i=1;i<=sc;++i) {
// printf("i=%d dg=%d siz=%d\n",i,dg[i],siz[i]);
// for (int j : inc[i]) printf("%d ",j); puts("");
// }
for (int i=1;i<=sc;++i){
if (siz[i]>2*nn){
puts("NIE");
clear();
return;
}
if (siz[i]>=nn){
int tmp=bfs(i);
if (tmp!=2){
puts("TAK");
// printf("tmp=%d i=%d\n",tmp,i);
for (int j=1;j<=n;++j) anss[j]=(tmp^1);
for (int j : ans) anss[j]=tmp;
for (int j=1;j<=n;++j) printf(anss[j]?"F":"R"); puts("");
clear();
return;
}
}
}
// puts("Yes");
for (int i=1;i<=n;++i) anss[i]=1;
queue<int> q; ans.clear();
for (int i=1;i<=n;++i){
if (!dg[i]) q.push(i);
}
while(q.size()){
int x=q.front(); q.pop();
for (int i : inc[x]) ans.push_back(i);
if (ans.size()>=nn && ans.size()<=nn*2){
puts("TAK");
clear();
for (int j : ans) anss[j]=0;
for (int j=1;j<=n;++j) printf(anss[j]?"F":"R"); puts("");
return;
}
for (int v : G1[x]){
if (--dg[v]==0) q.push(v);
}
}
clear();
puts("NIE");
}
int main(){
// freopen("data.in","r",stdin);
int t; cin>>t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 54800kb
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: 3205ms
memory: 120692kb
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: 6318ms
memory: 135556kb
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: 5246ms
memory: 127652kb
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: -100
Wrong Answer
time: 1752ms
memory: 54624kb
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 RRF NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE TAK RRFFFRRFF NIE NIE NIE TAK FRRRFRRRRRRFFFF NIE NIE NIE NIE TAK RRRFFFFFFFRF NIE TAK FFFFFFFFRFRRRFR NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE TAK FFRFRRFFR NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE...
result:
wrong answer zla odpowiedz!