QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99072 | #5504. Flower Garden | Al_fly | WA | 22ms | 75112kb | C++14 | 4.5kb | 2023-04-21 10:18:59 | 2023-04-21 10:19:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,t,lim,cnt,a[N],head[N],tot,sz[N],s[N],low[N],dfn[N],id[N],p[N],num,sum,tim;
bool vis[N],ans[N];
vector<int>son[N],g[N],re[N];
struct o{
int ne,to;
}e[N*40];
inline void add(int x,int y){
// cout<<x<<' '<<y<<'\n';
e[++tot].ne=head[x];
head[x]=tot;
e[tot].to=y;
}
inline void build1(int x,int l,int r){
if(l==r){cnt=max(cnt,x);return;}
int mid=(l+r)>>1;
add(x<<1,x),add(x<<1|1,x);
build1(x<<1,l,mid),build1(x<<1|1,mid+1,r);
}
inline void build2(int x,int l,int r){
id[x]=x+cnt;
if(l==r){a[id[x]]=l;add(id[x],x);return;}
int mid=(l+r)>>1;
build2(x<<1,l,mid),build2(x<<1|1,mid+1,r);
add(id[x],id[x<<1]),add(id[x],id[x<<1|1]);
}
inline void add1(int x,int l,int r,int ql,int qr){
if(l>qr||r<ql)return;
if(l>=ql&&r<=qr){
add(x,cnt);return;
}
int mid=(l+r)>>1;
add1(x<<1,l,mid,ql,qr),add1(x<<1|1,mid+1,r,ql,qr);
}
inline void add2(int x,int l,int r,int ql,int qr){
if(l>qr||r<ql)return;
if(l>=ql&&r<=qr){
add(cnt,id[x]);return;
}
int mid=(l+r)>>1;
add2(x<<1,l,mid,ql,qr),add2(x<<1|1,mid+1,r,ql,qr);
}
inline void tarjan(int x){
low[x]=dfn[x]=++tim,s[++num]=x;
for(int i=head[x];i;i=e[i].ne){
int to=e[i].to;
if(!dfn[to]){
tarjan(to);
low[x]=min(low[x],low[to]);
}else low[x]=min(low[x],dfn[to]);
}
if(low[x]==dfn[x]){
sum++;
while(s[num+1]!=x){
p[s[num]]=sum;
if(a[s[num]])g[sum].emplace_back(a[s[num]]),sz[sum]++;
num--;
}
}
}
inline void clear(){
for(int i=1;i<=cnt;i++)id[i]=ans[i]=vis[i]=a[i]=head[i]=p[i]=low[i]=dfn[i]=sz[i]=0,son[i].clear(),g[i].clear(),re[i].clear();
tot=cnt=sum=tim=num=0;
}
inline bool bfs1(int x){
queue<int>q;for(int i=1;i<=sum;i++)vis[i]=0;
q.push(x);int sum=0;vis[x]=1;
while(q.size()){
int x=q.front();q.pop();
sum+=sz[x];
for(int to:son[x]){
if(vis[to])continue;
q.push(to),vis[to]=1;
}
}
return sum<=lim+lim;
}
inline bool bfs2(int x){
queue<int>q;for(int i=1;i<=sum;i++)vis[i]=0;
q.push(x);int sum=0;vis[x]=1;
while(q.size()){
int x=q.front();q.pop();
sum+=sz[x];
for(int to:re[x]){
if(vis[to])continue;
q.push(to),vis[to]=1;
}
}
return sum<=lim+lim;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
lim=n;n*=3;
build1(1,1,n),build2(1,1,n);
cnt<<=1;
for(int i=1;i<=m;i++){
int l,r,ql,qr;
cin>>l>>r>>ql>>qr;cnt++;
add1(1,1,n,l,r),add2(1,1,n,ql,qr);
}
for(int i=1;i<=cnt;i++)if(!dfn[i])tarjan(i);
int res=0,np=0;
for(int i=1;i<=sum;i++){
res+=sz[i];
if(res>=lim&&res<=lim+lim){
np=i;break;
}
}
if(np){
for(int i=1;i<=np;i++){
for(int x:g[i])ans[x]=1;
}
cout<<"TAK"<<'\n';
for(int i=1;i<=n;i++)cout<<(ans[i]?'F':'R');
cout<<'\n';
clear();continue;
}
for(int x=1;x<=cnt;x++){
for(int i=head[x];i;i=e[i].ne){
int to=e[i].to;
if(p[to]!=p[x])son[p[x]].emplace_back(p[to]),re[p[to]].emplace_back(p[x]);
}
}
int pp=0;np=0;
for(int i=1;i<=sum;i++){
if(sz[i]>=lim){
if(!np)np=i;
else pp=i;
}
}
if(sz[np]>lim+lim){
cout<<"NIE"<<'\n';clear();continue;
}
if(bfs1(np)||(pp&&bfs1(pp))){
cout<<"TAK"<<'\n';
for(int i=1;i<=sum;i++){
if(!vis[i])continue;
for(int x:g[i])ans[x]=1;
}
for(int i=1;i<=n;i++)cout<<(ans[i]?'F':'R');
cout<<'\n';
}else if(bfs2(np)||(pp&&bfs2(pp))){
cout<<"TAK"<<'\n';
for(int i=1;i<=sum;i++){
if(!vis[i])continue;
for(int x:g[i])ans[x]=1;
}
for(int i=1;i<=n;i++)cout<<(ans[i]?'R':'F');
cout<<'\n';
}else cout<<"NIE"<<'\n';
clear();
}
return 0;
}
/*
1
1 3
1 1 2 2
2 2 3 3
3 3 1 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 22ms
memory: 75112kb
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 RRR TAK RRR
result:
wrong answer odpowiedz ma za malo/duzo roz!