結果
| 問題 |
No.2165 Let's Play Nim!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-16 03:07:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,838 bytes |
| コンパイル時間 | 1,846 ms |
| コンパイル使用メモリ | 173,424 KB |
| 実行使用メモリ | 40,884 KB |
| 平均クエリ数 | 212.92 |
| 最終ジャッジ日時 | 2024-11-15 07:23:00 |
| 合計ジャッジ時間 | 93,232 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 TLE * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <int LOG = 30> struct BinaryTrie {
struct Node {
long long cnt = 0;
int ch[2] = {-1, -1};
};
std::vector<Node> ns;
BinaryTrie() : ns(1) {}
long long size() const { return ns[0].cnt; }
long long operator[](int k) const { return find_kth(k, 0); }
long long find_kth(long long k, long long xor_add = 0) const {
assert(0 <= k && k < size());
long long idx = 0, val = 0;
for(int i=LOG-1; i>=0; i--) {
long long c = xor_add >> i & 1;
long long low_ch = ns[idx].ch[c];
long long low_cnt = (low_ch >= 0 ? ns[low_ch].cnt : 0);
if (k < low_cnt) {
idx = low_ch;
} else {
k -= low_cnt;
idx = ns[idx].ch[c ^ 1];
val ^= 1LL << i;
}
assert(idx >= 0);
}
return val;
}
void add(long long val, long long cnt = 1) {
assert(0 <= val && val < (1LL << LOG));
int idx = 0;
for(int i=LOG-1; i>=0; i--) {
ns[idx].cnt += cnt;
assert(ns[idx].cnt >= 0);
int &nxt = ns[idx].ch[val >> i & 1];
if (nxt == -1) {
idx = nxt = ns.size();
ns.emplace_back();
} else {
idx = nxt;
}
}
ns[idx].cnt += cnt;
assert(ns[idx].cnt >= 0);
}
long long lower_bound(long long val, long long xor_add = 0) {
assert(0 <= val);
if (val >= (1LL << LOG)) return size();
int idx = 0;
long long cnt = 0;
for(int i=LOG-1; i>=0; i--) {
int b = val >> i & 1, c = xor_add >> i & 1;
int ch = ns[idx].ch[c];
cnt += (b & (ch >= 0) ? ns[ch].cnt : 0);
idx = ns[idx].ch[b ^ c];
if (idx < 0 or ns[idx].cnt == 0) break;
}
return cnt;
}
long long count(long long val) const {
assert(0 <= val && val < (1LL << LOG));
int idx = 0;
for(int i=LOG-1; i>=0; i--) {
idx = ns[idx].ch[val >> i & 1];
if (idx < 0 or ns[idx].cnt == 0) return 0;
}
return ns[idx].cnt;
}
long long count(long long L, long long R, long long xor_add = 0) {
assert(0 <= L && L <= R && R <= (1LL << LOG));
return lower_bound(R, xor_add) - lower_bound(L, xor_add);
}
long long xor_min(long long xor_add = 0) { return find_kth(0, xor_add); }
long long xor_max(long long xor_add = 0) { return find_kth(size() - 1, xor_add); }
};
int main(){
//ios::sync_with_stdio(false);
//cin.tie(0);
int n, v = 0, p, x, s;
cin >> n;
vector<int> a(n);
BinaryTrie<20> BT;
set<pair<int,int>> S;
for(int i = 0; i < n; i++){
cin >> a[i];
v ^= a[i];
//BT.add(a[i], 1);
//S.insert({a[i], i});
}
auto update = [&](int p, int x){
//S.erase(make_pair(a[p], p));
//BT.add(a[p], -1);
v ^= a[p];
a[p] -= x;
v ^= a[p];
/*if(a[p] >= 1){
S.insert(make_pair(a[p], p));
BT.add(a[p], 1);
}*/
};
auto my_turn = [&](){
for(int i = 0; i < n; i++){
if(a[i] == 0)continue;
if((a[i] ^ v) < a[i]){
cout << i + 1 << " " << a[i] - (v ^ a[i]) << endl;
update(i, a[i] - (v ^ a[i]));
return;
}
}
};
if(v == 0){
cout << 0 << endl;
cin >> p >> x;
update(--p, x);
}else{
cout << 1 << endl;
my_turn();
}
while(true){
cin >> s;
if(s == -1)break;
my_turn();
cin >> s;
if(s == -1)break;
cin >> p >> x;
update(--p, x);
}
}