QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99016 | #5504. Flower Garden | Al_fly | RE | 4ms | 38744kb | C++14 | 4.4kb | 2023-04-21 08:47:23 | 2023-04-21 08:47:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+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<<1];
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){
a[x]=l,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;
if(l==r)return;
id[x]+=cnt;
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,vis[x]=1;
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 if(vis[to])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]++;
vis[s[num--]]=0;
}
}
}
inline void clear(){
for(int i=1;i<=cnt;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;cnt-=n;
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<=n;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]);
}
}
for(int i=1;i<=sum;i++){
if(sz[i]>=lim){
np=i;break;
}
}
if(sz[np]>lim+lim){
cout<<"NIE"<<'\n';clear();continue;
}
if(bfs1(np)){
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)){
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: 100
Accepted
time: 4ms
memory: 38744kb
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: -100
Runtime Error
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...