QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602970#8719. 后继lllei#AC ✓423ms149880kbC++202.1kb2024-10-01 13:50:292024-10-01 13:50:31

Judging History

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

  • [2024-10-01 13:50:31]
  • 评测
  • 测评结果:AC
  • 用时:423ms
  • 内存:149880kb
  • [2024-10-01 13:50:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;



int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;

	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	const int N = 31 * n + 1;
	int tot = 1;
	vector tr(N, array<int, 2>{-1, -1});
	vector<int> id(N, -1);
	id[0] = 0;

	auto insert = [&](int x, int p) {
		int u = 0;
		for (int i = 29; i >= 0; --i) {
			int t = ((x >> i) & 1);
			if (tr[u][t] == -1) {
				tr[u][t] = tot++;
			}
			u = tr[u][t];
			id[u] = p;
		}
	};

	for (int i = 0; i < n; ++i) {
		insert(a[i], i);
	}
	vector<bool> vis(31);
	vector<array<int, 2>> b;
	auto dfs = [&](auto &&self, int u, int dep) -> void {
		if (u == -1) {
			return;
		}
		if (tr[u][0] != -1 && tr[u][1] != -1 && !vis[dep]) {
			vis[dep] = true;
			b.push_back({u, dep});
		}

		self(self, tr[u][0], dep + 1);
		self(self, tr[u][1], dep + 1);
	};

	dfs(dfs, 0, 0);

	sort(b.begin(), b.end(), [&](const auto &a, const auto &b) {
		return a[1] > b[1];
	});
	while (m--) {
		int ans = 0;
		auto get = [&](auto &&self, int u, int dep) -> int {
			if (dep == 30) {
				return id[u];
			}
			int t = 29 - dep;
			int now = (ans >> t) & 1;
			if (now == 1) {
				if (tr[u][0] != -1) {
					return self(self, tr[u][0], dep + 1);
				} else {
					return self(self, tr[u][1], dep + 1);
				}
			} else {
				if (tr[u][1] != -1) {
					return self(self, tr[u][1], dep + 1);
				} else {
					return self(self, tr[u][0], dep + 1);
				}
			}
		};

		auto answer = [&](int x) {
			cout << "! " << x << endl;
		};

		auto query = [&](int x) {
			cout << "? " << x + 1 << endl;
			int y;
			cin >> y;
			return y - 1;
		};

		for (const auto [u, dep] : b) {
			auto p = get(get, tr[u][0], dep + 1);
			int p1 = query(p);

			int mask = 0;
			for (int j = 0; j < dep + 1; ++j) {
				mask |= (1 << (29 - j));
			}
			if (p1 != -2 && (a[p1] & mask) == (a[id[tr[u][1]]] & mask)) {
			} else {
				ans |= (1 << (29 - dep));
			}
		}

		answer(ans);
	}
	return 0;
}

詳細信息

Test #1:

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

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: 0ms
memory: 3596kb

input:

1 1
0

output:

! 0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3828kb

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: 64ms
memory: 3524kb

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: 423ms
memory: 149864kb

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: 275ms
memory: 149880kb

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