結果

問題 No.2165 Let's Play Nim!
ユーザー t98slidert98slider
提出日時 2022-12-16 03:20:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,748 bytes
コンパイル時間 1,872 ms
コンパイル使用メモリ 180,376 KB
実行使用メモリ 25,476 KB
平均クエリ数 18.33
最終ジャッジ日時 2024-04-27 07:30:54
合計ジャッジ時間 10,962 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 23 ms
24,940 KB
testcase_31 RE -
testcase_32 RE -
evil_2_big_1.txt RE -
evil_2_big_2.txt RE -
evil_2_big_3.txt RE -
evil_big_1.txt RE -
evil_big_2.txt RE -
evil_big_3.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = [&](){
        int v2 = BT.xor_min(v);
        int p = S.lower_bound(make_pair(v2, -1)) -> second;
        cout << p + 1 << " " << a[p] - (v ^ a[p]) << endl;
        update(p, a[p] - (v ^ a[p]));
    };
    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);
    }
}
0