QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423691#8719. 后继QuanQiuTongRE 1ms3864kbC++202.8kb2024-05-28 15:13:402024-05-28 15:13:40

Judging History

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

  • [2024-05-28 15:13:40]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3864kb
  • [2024-05-28 15:13:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define read() ({int x,c,f=1;while((c=getchar())<48||57<c)if(c=='-')f=-1;for(x=c^48;47<(c=getchar())&&c<58;)x=x*10+(c^48);x*f; })
#define fo(i, n) for (int i = 1, _##i(n); i <= _##i; ++i)
constexpr int N = (4e5 + 2) * 30;
int a[N];
struct Trie
{
    int ch[N][2], tot;
    int two[N];
    int val[N];
    int bro[N];
    void insert(int i)
    {
        int x = a[i];
        int u = 0;
        for (int i = 30; i--;)
        {
            int bit = (x >> i) & 1;
            if (!ch[u][bit])
                ch[u][bit] = ++tot;
            u = ch[u][bit];
        }
        val[u] = i;
    }
    void findTwo(int u, int dep)
    {
        if (ch[u][0] && ch[u][1] && !two[dep])
        {
            two[dep] = u;
            bro[ch[u][1]] = ch[u][0];
            bro[ch[u][0]] = ch[u][1];
        }
        if (ch[u][1])
            findTwo(ch[u][1], dep - 1);
        if (ch[u][0])
            findTwo(ch[u][0], dep - 1);
    }
    int maxson(int u)
    {
        if (ch[u][1])
            return maxson(ch[u][1]);
        if (ch[u][0])
            return maxson(ch[u][0]);
        return val[u];
    }
    int maxson(int i, int x)
    {
        int u = two[i];
        for (; i >= 0; i--)
        {
            int bit = (x >> i) & 1;
            if (ch[u][!bit])
                u = ch[u][!bit];
            else if (ch[u][bit])
                u = ch[u][bit];
            else
                return -1; // impossible
        }
        return val[u];
    }
    bool find(int x, int fath)
    {
        int u = 0;
        for (int i = 30; i--;)
        {
            if (u == fath)
                return true;
            int bit = (x >> i) & 1;
            if (!ch[u][bit])
                return false;
            u = ch[u][bit];
        }
        return false;
    }
    // void print(int u,int x)
    // {
    //     printf("# %d: %d\n", u, val[u]);
    //     if (ch[u][1])
    //         print(ch[u][1]);
    //     if (ch[u][0])
    //         print(ch[u][0]);
    // }
} t;

int ask(int x)
{
    printf("? %d\n", x);
    fflush(stdout);
    return read();
}
void ans(int x)
{
    printf("! %d\n", x);
    fflush(stdout);
}
void solve()
{
    int x = 0;
    for (int i = 0; i < 30; ++i)
    {
        if (!t.two[i])
            continue;

        int id = t.maxson(i, x);
        int ub = ask(id);
        if (ub == -1)
            continue;
        if (!t.find(a[ub], t.ch[t.two[i]][1]))
            x |= 1 << i;
        // printf("* [%d], x = %d\n", i, x);
    }
    ans(x);
}
int main()
{
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i)
        t.insert(a[i] = read());
    t.findTwo(0, 29);

    for (int i = m; i--;)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3864kb

input:

5 1
1 2 3 4 5
4
1
-1

output:

? 5
? 2
? 4
! 3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

1 1
0

output:

! 0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Runtime Error

input:

10 10
380864879 387438357 21484978 21099484 375657510 23189485 24467021 379687119 386094773 15156199

output:


result: