QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77869 | #5504. Flower Garden | 18Michael | AC ✓ | 2031ms | 137996kb | C++14 | 4.6kb | 2023-02-15 19:20:29 | 2023-02-15 19:20:32 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
const int MxV=1200002,MxE=50000002,MxN=100002;
int n,n2,n3,q,t,edge_t,dfn_t,st_t,rt_t,s,s2,cnt,e_t,Test_num;
bool Ok;
int lson[MxV],rson[MxV],val[MxV],la[MxV],num[MxN],dfn[MxV],low[MxV],rt[MxV],st[MxV],sum[MxV],lae[MxV],from[MxE],deg[MxV];
bool ok[MxV],ins[MxV],u[MxV];
vector<int> vec,vec1;
vector<int> vec2[MxV];
queue<int> Q,ept;
struct aaa
{
int to,nx;
}edge[MxE],e[MxE];
template<class T>void read(T &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0' || ch>'9')f|=(ch=='-'),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
x=f? -x:x;return ;
}
inline void add_edge(int x,int y)
{
edge[++edge_t]=(aaa){y,la[x]},la[from[edge_t]=x]=edge_t;
}
inline void add_e(int x,int y)
{
if(x^y)e[++e_t]=(aaa){y,lae[x]},++deg[y],lae[x]=e_t;
}
inline int newnode()
{
return ++t,la[t]=val[t]=dfn[t]=rt[t]=0,t;
}
inline void build(int k,int l,int r)
{
if(l==r)return (void)(val[num[l]=k]=1);
int mid=((l+r)>>1);
build(lson[k]=newnode(),l,mid),build(rson[k]=newnode(),mid+1,r),add_edge(k,lson[k]),add_edge(k,rson[k]);
}
inline void build2(int k,int l,int r)
{
if(l==r)return ;
int mid=((l+r)>>1);
build2(lson[k]=(l==mid? num[l]:newnode()),l,mid),build2(rson[k]=(mid+1==r? num[r]:newnode()),mid+1,r),add_edge(lson[k],k),add_edge(rson[k],k);
}
inline void add(int k,int l,int r,int l1,int r1,int x)
{
if(l>=l1 && r<=r1)return add_edge(x,k);
int mid=((l+r)>>1);
if(l1<=mid)add(lson[k],l,mid,l1,r1,x);
if(r1>mid)add(rson[k],mid+1,r,l1,r1,x);
}
inline void add2(int k,int l,int r,int l1,int r1,int x)
{
if(l>=l1 && r<=r1)return add_edge(k,x);
int mid=((l+r)>>1);
if(l1<=mid)add2(lson[k],l,mid,l1,r1,x);
if(r1>mid)add2(rson[k],mid+1,r,l1,r1,x);
}
inline void Tarjan(int x)
{
dfn[x]=low[x]=(++dfn_t),ins[st[++st_t]=x]=1;
for(int i=la[x],v;i;i=edge[i].nx)
{
v=edge[i].to;
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])for(sum[++rt_t]=0,vec2[rt_t].clear();st[st_t+1]!=x;--st_t)rt[st[st_t]]=rt_t,sum[rt_t]+=val[st[st_t]],vec2[rt_t].push_back(st[st_t]),ins[st[st_t]]=0;
}
inline void print()
{
Ok=1,puts("TAK");
for(int i=1;i<=n3;++i)putchar(u[num[i]]? 'F':'R');
puts("");
}
inline void solve1()
{
e_t=0,cnt=n3,Q=ept;
for(int i=1;i<=rt_t;++i)lae[i]=deg[i]=0;
for(int i=1;i<=edge_t;++i)add_e(rt[from[i]],rt[edge[i].to]);
for(int i=1;i<=rt_t;++i)if(!deg[i])Q.push(i),ins[i]=1;else ins[i]=0;
for(int i=1;i<=t;++i)u[i]=0;
for(int p;Q.size();)
{
p=Q.front(),Q.pop(),cnt-=sum[p];
for(int i=0;i<vec2[p].size();++i)u[vec2[p][i]]=1;
if(cnt>=n && cnt<=n2)return print();
for(int i=lae[p],v;i;i=e[i].nx)if(!(--deg[v=e[i].to]) && !ins[v])Q.push(v),ins[v]=1;
}
assert(0);
}
inline void dfs(int x)
{
ins[x]=1,cnt-=sum[x];
for(int i=0;i<vec2[x].size();++i)u[vec2[x][i]]=1;
for(int i=lae[x],v;i;i=e[i].nx)if(!ins[v=e[i].to])dfs(v);
}
inline void solve3()
{
for(int i=1;i<=t;++i)u[i]=0;
cnt=n3,e_t=0,Q=ept;
for(int i=1;i<=rt_t;++i)lae[i]=ins[i]=0;
for(int i=1;i<=edge_t;++i)add_e(rt[edge[i].to],rt[from[i]]);
for(int i=0;i<vec1.size();++i)dfs(vec1[i]);
if(cnt>=n && cnt<=n2)return print();
e_t=0;
for(int i=1;i<=rt_t;++i)lae[i]=deg[i]=0;
for(int i=1;i<=edge_t;++i)if(!u[from[i]] && !u[edge[i].to])add_e(rt[from[i]],rt[edge[i].to]);
for(int i=1;i<=rt_t;++i)if(!u[vec2[i][0]] && !ok[i] && !deg[i])Q.push(i),ins[i]=1;else ins[i]=0;
for(int p;Q.size();)
{
p=Q.front(),Q.pop(),cnt-=sum[p];
for(int i=0;i<vec2[p].size();++i)u[vec2[p][i]]=1;
if(cnt>=n && cnt<=n2)return print();
for(int i=lae[p],v;i;i=e[i].nx)if(!(--deg[v=e[i].to]) && !ok[v] && !ins[v])Q.push(v),ins[v]=1;
}
}
inline void solve2()
{
vec1.clear();
for(int i=0;i<vec.size();++i)vec1.push_back(vec[i]);
if(solve3(),Ok)return ;
for(int i=0;i<vec.size();++i)
{
for(int j=1;j<=t;++j)u[j]=0;
vec1.clear(),ok[vec[i]]=1;
if(vec.size()>1)vec1.push_back(vec[i^1]);
solve3(),ok[vec[i]]=0;
if(Ok)return ;
}
puts("NIE");
}
inline void solve()
{
read(n),read(q),n2=(n<<1),n3=n*3,edge_t=dfn_t=st_t=rt_t=t=Ok=0,vec.clear(),build(s=newnode(),1,n3),build2(s2=newnode(),1,n3);
for(int i=1,a,b,c,d;i<=q;++i)read(a),read(b),read(c),read(d),add(s,1,n3,a,b,newnode()),add2(s2,1,n3,c,d,t);
for(int i=1;i<=t;++i)ins[i]=st[i]=0;
st[t+1]=0;
for(int i=1;i<=t;++i)if(!dfn[i])Tarjan(i);
for(int i=1;i<=rt_t;++i)if(sum[i]>n)vec.push_back(i);
if(!vec.size())solve1();
else solve2();
}
int main()
{
read(Test_num);
while(Test_num--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 60828kb
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: 2031ms
memory: 116976kb
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: 1631ms
memory: 116076kb
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: 1455ms
memory: 120852kb
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: 0
Accepted
time: 292ms
memory: 60776kb
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 FFR NIE TAK RRRRRFRRFRFF TAK RFRFFFRRRRRR NIE TAK RRRRRRRRFFFFFRR TAK RRRRRRRRFFFF TAK FFRRRRRRRRFF TAK RRRRRRFFFFFRRRR TAK RRRRRRRRFFFF TAK RRRRRRRRRRFFFFF NIE TAK RRFRRRRRRFFF NIE TAK RRFFFRRFF NIE TAK RRRFFRRRF TAK RRRRRFRRFRFF TAK RRFFRFRFFFFFRFF TAK RRRRRRRRFFFF NIE TAK ...
result:
ok good!
Test #6:
score: 0
Accepted
time: 1200ms
memory: 137996kb
input:
10 33333 99999 60859 99999 1 60858 78724 99999 1 78723 42986 99999 1 42985 35108 99999 1 35107 63158 99999 1 63157 86977 99999 1 86976 13576 99999 1 13575 48277 99999 1 48276 89102 99999 1 89101 92657 99999 1 92656 25093 99999 1 25092 2353 99999 1 2352 81748 99999 1 81747 51216 99999 1 51215 75815 9...
output:
NIE TAK RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
result:
ok good!