QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#520006#5504. Flower Gardendo_it_tomorrowRE 3ms88888kbC++144.8kb2024-08-15 10:13:142024-08-15 10:13:14

Judging History

你现在查看的是最新测评结果

  • [2024-08-15 10:13:14]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:88888kb
  • [2024-08-15 10:13:14]
  • 提交

answer

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int N=1e6+5;
int n,q,cnt,p[N],dfn[N],low[N];
bool vis[N];
vector<int> v[N];
struct SEG{
    int in[N],out[N];
    // void build(int k,int l,int r){
    //     in[k]=++cnt;
    //     out[k]=++cnt;
    //     if(l==r){
    //         p[l]=k;
    //         v[in[k]].push_back(out[k]);
    //         v[out[k]].push_back(in[k]);
    //         return;
    //     }
    //     int mid=(l+r)/2;
    //     build(k*2,l,mid);
    //     build(k*2+1,mid+1,r);
    //     v[in[k*2]].push_back(in[k]);
    //     v[in[k*2+1]].push_back(in[k]);
    //     v[out[k]].push_back(out[k*2]);
    //     v[out[k]].push_back(out[k*2+1]);
    // }
    void build(int l,int r,int x) {
        in[x]=++cnt;
        out[x]=++cnt;
        if(l==r) {
            p[l]=x;
            v[in[x]].push_back(out[x]);
            v[out[x]].push_back(in[x]);
            return ;
        }
        int mid=l+r>>1;
        build(l,mid,x<<1), build(mid+1,r,x<<1|1);
        v[in[x<<1]].push_back(in[x]);
        v[in[x<<1|1]].push_back(in[x]);
        v[out[x]].push_back(out[x<<1]);
        v[out[x]].push_back(out[x<<1|1]);
    }
    void change1(int k,int l,int r,int x,int y,int id){
        if(r<x||y<l){
            return;
        }
        if(x<=l&&r<=y){
            v[in[k]].push_back(id);
            return;
        }
        int mid=(l+r)/2;
        change1(k*2,l,mid,x,y,id);
        change1(k*2+1,mid+1,r,x,y,id);
    }
    void change2(int k,int l,int r,int x,int y,int id){
        if(r<x||y<l){
            return;
        }
        if(x<=l&&r<=y){
            v[id].push_back(out[k]);
            return;
        }
        int mid=(l+r)/2;
        change2(k*2,l,mid,x,y,id);
        change2(k*2+1,mid+1,r,x,y,id);
    }
}tree;
stack<int> s;
int czc,num,bl[N];
void tarjan(int x) {
    dfn[x]=low[x]=++czc,s.push(x);
    for(int y:v[x]) {
        if(!dfn[y]) {
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(!vis[y]) low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]) {
        ++num;
        int top;
        do{
            top=s.top(); s.pop();
            bl[top]=num;
            vis[top]=1;
        }while(top!=x);
    }
    return ;
}

int siz[N],du[N];
vector<int> g1[N],g2[N];
void find1(int x){
    vis[x]=1;
    for(int i:g1[x]){
        if(!vis[i]){
            find1(i);
        }
    }
}
void find2(int x){
    vis[x]=1;
    for(int i:g2[x]){
        if(!vis[i]){
            find2(i);
        }
    }
}
void print(){
    cout<<"TAK\n";
    for(int i=1;i<=n;i++) {
        if(vis[bl[tree.in[p[i]]]]==1){
            cout<<'R';
        }
        else{
            cout<<'F';
        }
    }
    cout<<'\n';
}
void solve(){
    cin>>n>>q,n*=3;
    tree.build(1,n,1);
    for(int i=1,a,b,c,d;i<=q;i++){
        cin>>a>>b>>c>>d;
        int p1=++cnt,p2=++cnt;
        tree.change1(1,1,n,a,b,p1);
        tree.change2(1,1,n,c,d,p2);
        v[p1].push_back(p2);
    }
    for(int i=1;i<=cnt;i++){
        if(!dfn[i]){
            tarjan(i);
        }
    }
    for(int i=1;i<=n;i++){
        siz[bl[tree.in[p[i]]]]++;
    }
    for(int i=1;i<=cnt;i++){
        for(int j:v[i]){
            if(bl[i]==bl[j]){
                continue;
            }
            g1[bl[i]].push_back(bl[j]);
            g2[bl[j]].push_back(bl[i]);
            du[bl[j]]++;
        }
    }
    int id=0;
    for(int i=1;i<=num;i++){
        if(siz[i]>n/3){
            id=i;
        }
    }
    for(int i=1;i<=num;i++){
        vis[i]=0;
    }
    int cnt=0;
    if(id){
        find1(id);
        for(int i=1;i<=num;i++){
            if(vis[i]){
                cnt+=siz[i];
            }
        }
        if(cnt>=n/3&&cnt<=2*n/3){
            print();
            return ;
        }
        for(int i=1;i<=num;i++){
            vis[i]=0;
        }
        find2(id);
        cnt=0;
        for(int i=1;i<=num;i++){
            if(vis[i]){
                cnt+=siz[i];
            }
        }
        if(cnt>=n/3&&cnt<=2*n/3){
            print();
            return ;
        }
        cout<<"NIE\n";
        return ;
    }
    queue<int>q;
    for(int i=1;i<=num;i++){
        if(!du[i]){
            q.push(i);
        }
    }
    while(!q.empty()){
        int top=q.front();q.pop();
        cnt+=siz[top];
        vis[top]=1;
        if(cnt>=n/3){
            break;
        }
        for(int i:g1[top]){
            du[i]--;
            if(!du[i]){
                q.push(i);
            }
        }
    }
    print();
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    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: 3ms
memory: 88888kb

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
RFF
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...

output:


result: