QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#421290#8719. 后继hazeRE 0ms0kbC++233.1kb2024-05-25 16:11:452024-05-25 16:11:45

Judging History

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

  • [2024-05-25 16:11:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-05-25 16:11:45]
  • 提交

answer

//
// Created by DELLPC on 24-5-19.
//
#include "bits/stdc++.h"

using namespace std;

using i64 = long long;
using i32 = unsigned int;


i32 ask(int x) {
    cout << "? " << x << endl;
    i32 res;
    cin >> res;
    return res;
}

void anw(int x) {
    cout << "! " << x << endl;
}


class Tired {
private:
    struct Node {
//        int l, r;
        int pre;
        int id;
        int ch[2];
        int val;

        Node() : pre(0), id(0), val(0) {
            ch[0] = ch[1] = 0;
        }
    };

    vector<Node> tree;
    vector<int> a;
    vector<Node *> v;
    int tot = 0;
    int ans = 0;

    void insert(int x, int id) {
        int p = 0;
        int pre = 0;
        for (int i = 30; i >= 0; i--) {
            int c = (x >> i) & 1;
//            cerr << "c: " << c << endl;

//            cerr << "pre: " << pre << endl;
            if (!tree[p].ch[c]) {
                tree[p].ch[c] = ++tot;
                tree[tot].pre = pre;
            }
            if (tree[p].ch[0] && tree[p].ch[1]) {
                if (!v[i]) {
                    v[i] = &tree[p];
                }
            }
            pre |= (c << i);

            p = tree[p].ch[c];
        }
        tree[p].id = id;
        tree[p].val = x;

    }


    pair<int, int> getmax(int x, int i) {

        if (i < 0) {
//            cerr << "x: " << x << endl;
//            cerr << "tree[x].id: " << tree[x].id << endl;
            return {tree[x].id, x};
        }
//        cerr << (ans >> i) << endl;
        if (tree[x].ch[1 & (ans >> i)]) {
            return getmax(tree[x].ch[1 & (ans >> i)], i - 1);
        } else {
            return getmax(tree[x].ch[!(1 & (ans >> i))], i - 1);
        }
    }

public:
    Tired(int n, vector<int> &aa) : tree(n * 31 + 10), a(aa), v(31) {
        for (int i = 1; i <= n; i++) {
            insert(a[i], i);
        }
    }

    void run() {
//        cerr << bitset<32>(271581184) << endl;
        ans = 0;
        for (int i = 0; i <= 31; i++) {
            if (v[i]) {
                auto [len, x] = getmax(v[i]->ch[0], i - 1);
                int ans1 = ask(len);
//                cerr << "ans1: " << ans1 << endl;
                if (ans1 == -1 || (a[ans1] ^ v[i]->pre) >= (1 << (i))) {
                    ans |= 1 << i;
                }
//                cerr << "ans: " << bitset<32>(ans) << endl;
//                cerr << a[ans1] << endl;
//                cerr << bitset<32>(a[ans1]) << endl;
//                cerr << v[i]->pre << endl;
//                cerr << bitset<32>(v[i]->pre) << endl;
//                cerr << (a[ans1] ^ v[i]->pre) << endl;
//                cerr << bitset<32>(a[ans1] ^ v[i]->pre) << endl;

            }
        }
        anw(ans);

    };
//00010000001100000000000000000000
//00000000000100000000000000000000
};

int main() {

    i64 n, m;

    cin >> n >> m;

    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    Tired t(n, a);
    while (m--) {
        t.run();
    }
// 5 1
// 1 2 3 4 5

}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

5 1
1 2 3 4 5
1
5
2

output:

? 2
? 1
? 3

result: