QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#730906#9570. Binary TreewxgmjfhyWA 1ms3796kbC++203.1kb2024-11-09 22:21:152024-11-09 22:21:15

Judging History

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

  • [2024-11-09 22:21:15]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3796kb
  • [2024-11-09 22:21:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
// #pragma GCC optimize(3)
#define int long long

// std::mt19937_64 rng {std::chrono::steady_clock::now().time_since_epoch().count()};

// #define LOCAL

int x;

inline int query(int a,int b){
    cout<<"? "<<a<<" "<<b<<endl;
    cin>>x;
    return x;
}

int n;

inline void solve(){
    cin>>n;

    vector<int>ls(n+1),rs(n+1),p(n+1);

    for(int i=1;i<=n;i++){
        cin>>ls[i]>>rs[i];
        if(ls[i])p[ls[i]]=i;
        if(rs[i])p[rs[i]]=i;
    }

    int rt=-1;
    for(int i=1;i<=n;i++){
        if(!p[i])rt=i;
    }

    int now=rt;
    while(1){
        if(!ls[now]&&!rs[now]){
            vector<int>v(1);

            int pos=now;
            while(1){
                v.push_back(pos);
                int fa=p[pos];
                if(!fa||(ls[fa]&&rs[fa]))break;
                pos=fa;
            }

            int m=v.size()-1;

            int l=1,r=m;
            while(1){
                int x=query(v[l],v[r]);

                if(x==0){
                    if(l+1==r){
                        cout<<"! "<<v[l]<<endl;
                        return;
                    }
                    r=l+r>>1;
                }else if(x==2){
                    if(l+1==r){
                        cout<<"! "<<v[r]<<endl;
                        return;
                    }
                    l=l+r>>1;
                }else{
                    assert(r-l&1^1);
                    cout<<"! "<<v[r+l>>1]<<endl;
                    return;
                }
            }
        }

        if(!ls[now]){
            now=rs[now];
            continue;
        }

        if(!rs[now]){
            now=ls[now];
            continue;
        }

        int x=query(ls[now],rs[now]);

        if(x==0){
            now=ls[now];
        }else if(x==2){
            now=rs[now];
        }else{
            vector<int>v(1);

            int pos=now;
            while(1){
                v.push_back(pos);
                int fa=p[pos];
                if(!fa||(ls[fa]&&rs[fa]))break;
                pos=fa;
            }

            int m=v.size()-1;

            int l=1,r=m;
            while(1){
                int x=query(v[l],v[r]);

                if(x==0){
                    if(l+1==r){
                        cout<<"! "<<v[l]<<endl;
                        return;
                    }
                    r=l+r>>1;
                }else if(x==2){
                    if(l+1==r){
                        cout<<"! "<<v[r]<<endl;
                        return;
                    }
                    l=l+r>>1;
                }else{
                    assert(r-l&1^1);
                    cout<<"! "<<v[r+l>>1]<<endl;
                    return;
                }
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif

    // cout<<setiosflags(ios::fixed)<<setprecision(10);

    int _=1;
    cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3796kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
0
1

output:

? 2 4
? 1 5
? 2 2

result:

wrong answer Too many queries (test case 1)