QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#422204 | #8719. 后继 | Cipherxzc | AC ✓ | 3350ms | 316756kb | C++20 | 1.9kb | 2024-05-26 23:03:33 | 2024-05-26 23:03:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct Trie{
struct node{
int son[2], id;
};
vector<node> trie;
int bit, tot;
Trie(int b = 0){
init(b);
}
void init(int b){
bit = b;
trie.clear();
trie.push_back({});
tot = 0;
}
inline void insert(int x, int id){
int p = 0;
for (int i = bit; i >= 0; i--){
int gg = (x >> i) & 1;
if (!trie[p].son[gg]){
trie[p].son[gg] = ++tot;
trie.push_back({});
}
p = trie[p].son[gg];
}
trie[p].id = id;
}
inline int query(int x, int b, int y){ // 前 b 位为 x 的数 xor y 的最大值
int p = 0;
for (int i = bit; i >= b; i--){
int gg = (x >> i - b) & 1;
p = trie[p].son[gg];
}
for (int i = b - 1; i >= 0; i--){
int gg = (y >> i) & 1;
if (trie[p].son[gg ^ 1]){
p = trie[p].son[gg ^ 1];
}else{
p = trie[p].son[gg];
}
}
return trie[p].id;
}
};
const int N = 4e5 + 5;
int n, m, a[N];
set<int> used1[35], used2[35];
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
Trie trie(30);
for (int i = 1; i <= n; i++){
cin >> a[i];
trie.insert(a[i], i);
}
for (int i = 1; i <= n; i++){
int x = 0;
for (int j = 29; j >= 0; j--){
x <<= 1;
if ((a[i] >> j) & 1){
x |= 1;
}
used1[j].insert(x);
}
}
for (int i = 29; i >= 0; i--){
for (int x : used1[i]){
if (used1[i].find(x ^ 1) != used1[i].end()){
used2[i].insert(x >> 1);
}
}
}
while (m--){
int x = 0;
for (int i = 0; i < 30; i++){
if (used2[i].empty()){
continue;
}
int gg = *used2[i].begin();
int x1 = gg << 1, x2 = x1 | 1, p;
int id = trie.query(x1, i, x);
cout << "? " << id << endl;
cin >> p;
//cout << x1 << ' ' << x2 << ' ' << a[p] << endl;
if (p == -1 || (a[p] >> i) != x2){
x ^= 1 << i;
}
}
cout << "! " << x << endl;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3492kb
input:
5 1 1 2 3 4 5 1 5 5
output:
? 2 ? 1 ? 1 ! 3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
1 1 0
output:
! 0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3504kb
input:
10 10 380864879 387438357 21484978 21099484 375657510 23189485 24467021 379687119 386094773 15156199 3 4 -1 1 7 -1 3 4 10 7 -1 -1 7 10 7 1 -1 -1 7 -1 7 2 3 -1 3 4 10 -1 2 2 3 4 10 -1 2 2 3 7 6 4 -1 -1 7 -1 7 2 3 -1 6 7 6 8 3 -1 3 7 8 9 6 8
output:
? 4 ? 6 ? 3 ? 5 ? 10 ? 3 ! 271581184 ? 4 ? 6 ? 3 ? 5 ? 10 ? 10 ! 296747008 ? 4 ? 6 ? 4 ? 5 ? 10 ? 10 ! 286523392 ? 4 ? 6 ? 4 ? 5 ? 10 ? 6 ! 278134784 ? 4 ? 6 ? 3 ? 5 ? 10 ? 10 ! 28311552 ? 4 ? 6 ? 3 ? 5 ? 10 ? 10 ! 28311552 ? 4 ? 6 ? 3 ? 5 ? 10 ? 10 ! 293601280 ? 4 ? 6 ? 4 ? 5 ? 10 ? 6 ! 278134784 ?...
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 47ms
memory: 3740kb
input:
100 3000 416322873 449728250 688705913 946343465 16202884 153238658 573284215 724198910 577719053 868106680 951494055 942341618 190594266 331719623 856324110 977865755 151782935 163752541 1565918 870244322 299691610 37854919 198293342 152446496 549402023 869857831 869628458 573984494 162791133 94423...
output:
? 11 ? 85 ? 88 ? 15 ? 49 ? 22 ? 19 ? 19 ? 19 ? 19 ? 19 ? 19 ? 41 ! 184027136 ? 11 ? 85 ? 88 ? 15 ? 49 ? 22 ? 19 ? 61 ? 55 ? 55 ? 55 ? 55 ? 55 ! 445644800 ? 11 ? 85 ? 88 ? 15 ? 49 ? 22 ? 19 ? 19 ? 77 ? 77 ? 57 ? 57 ? 70 ! 145555456 ? 11 ? 85 ? 88 ? 15 ? 49 ? 22 ? 19 ? 19 ? 77 ? 77 ? 77 ? 77 ? 41 ! 17...
result:
ok 3000 numbers
Test #5:
score: 0
Accepted
time: 3350ms
memory: 316756kb
input:
400000 3000 406697044 910508999 785308673 89872855 911511537 786066190 569035255 791369083 231783630 373589094 1005702647 785128281 910883228 612232307 428493618 691767283 288087749 380834477 1012471581 807583302 951439706 172399529 651163374 395700503 212381194 528978756 628513589 230244101 1046447...
output:
? 208131 ? 190730 ? 201285 ? 65179 ? 109637 ? 150514 ? 69846 ? 198186 ? 274803 ? 374076 ? 110252 ? 196742 ? 358945 ? 358945 ? 358945 ? 358945 ? 358945 ? 393313 ? 393313 ? 348261 ? 193410 ? 77860 ? 77860 ? 77860 ? 230877 ? 371824 ? 55602 ? 304394 ? 304394 ? 248810 ! 140702791 ? 208131 ? 190730 ? 2012...
result:
ok 3000 numbers
Test #6:
score: 0
Accepted
time: 2136ms
memory: 183564kb
input:
400000 3000 49852143 106643027 78538081 13535110 34673790 361240760 358287062 285333122 15578697 106130051 320170693 107023555 72813915 2799162 286900401 787319 332890808 75095435 110215229 74827421 107485420 300695656 71750101 285593446 37660664 397030779 392989831 16018850 14036294 361777963 11269...
output:
? 367047 ? 263139 ? 367047 ? 367047 ? 94916 ? 94916 ? 40340 ? 214138 ? 358798 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ? 38445 ! 113099425 ? 367047 ? 263139 ? 90385 ? 263139 ? 263139 ? 162978 ? 162978 ? 162978 ? 107911 ? 107911 ? 107911 ? 107911 ? 107911 ? 1079...
result:
ok 3000 numbers
Extra Test:
score: 0
Extra Test Passed