提出 #23368014


ソースコード 拡げる

#include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
template< typename T, int MAX_LOG >
struct BinaryTrie {
  BinaryTrie *nxt[2];
  T lazy;
  int exist;
  bool fill;
  vector< int > accept;

  BinaryTrie() : exist(0), lazy(0), nxt{nullptr, nullptr} {}

  void add(const T &bit, int bit_index, int id) {
    propagate(bit_index);
    if(bit_index == -1) {
      ++exist;
      accept.push_back(id);
    } else {
      auto &to = nxt[(bit >> bit_index) & 1];
      if(!to) to = new BinaryTrie();
      to->add(bit, bit_index - 1, id);
      ++exist;
    }
  }

  void add(const T &bit, int id) {
    add(bit, MAX_LOG, id);
  }

  void add(const T &bit) {
    add(bit, exist);
  }

  void del(const T &bit, int bit_index) {
    propagate(bit_index);
    if(bit_index == -1) {
      exist--;
    } else {
      nxt[(bit >> bit_index) & 1]->del(bit, bit_index - 1);
      exist--;
    }
  }

  void del(const T &bit) {
    del(bit, MAX_LOG);
  }


  pair< T, BinaryTrie * > max_element(int bit_index) {
    propagate(bit_index);
    if(bit_index == -1) return {0, this};
    if(nxt[1] && nxt[1]->size()) {
      auto ret = nxt[1]->max_element(bit_index - 1);
      ret.first |= T(1) << bit_index;
      return ret;
    } else {
      return nxt[0]->max_element(bit_index - 1);
    }
  }

  pair< T, BinaryTrie * > min_element(int bit_index) {
    propagate(bit_index);
    if(bit_index == -1) return {0, this};
    if(nxt[0] && nxt[0]->size()) {
      return nxt[0]->min_element(bit_index - 1);
    } else {
      auto ret = nxt[1]->min_element(bit_index - 1);
      ret.first |= T(1) << bit_index;
      return ret;
    }
  }

  T mex_query(int bit_index) { // distinct-values
    propagate(bit_index);
    if(bit_index == -1 || !nxt[0]) return 0;
    if(nxt[0]->size() == (T(1) << bit_index)) {
      T ret = T(1) << bit_index;
      if(nxt[1]) ret |= nxt[1]->mex_query(bit_index - 1);
      return ret;
    } else {
      return nxt[0]->mex_query(bit_index - 1);
    }
  }

  int64_t count_less(const T &bit, int bit_index) {
    propagate(bit_index);
    if(bit_index == -1) return 0;
    int64_t ret = 0;
    if((bit >> bit_index) & 1) {
      if(nxt[0]) ret += nxt[0]->size();
      if(nxt[1]) ret += nxt[1]->count_less(bit, bit_index - 1);
    } else {
      if(nxt[0]) ret += nxt[0]->count_less(bit, bit_index - 1);
    }
    return ret;
  }

  pair< T, BinaryTrie * > get_kth(int64_t k, int bit_index) { // 1-indexed
    propagate(bit_index);
    if(bit_index == -1) return {0, this};
    if((nxt[0] ? nxt[0]->size() : 0) < k) {
      auto ret = nxt[1]->get_kth(k - (nxt[0] ? nxt[0]->size() : 0), bit_index - 1);
      ret.first |= T(1) << bit_index;
      return ret;
    } else {
      return nxt[0]->get_kth(k, bit_index - 1);
    }
  }

  pair< T, BinaryTrie * > max_element() {
    assert(exist);
    return max_element(MAX_LOG);
  }

  pair< T, BinaryTrie * > min_element() {
    assert(exist);
    return min_element(MAX_LOG);
  }

  T mex_query() {
    return mex_query(MAX_LOG);
  }

  int size() const {
    return exist;
  }

  void xorpush(const T &bit) {
    lazy ^= bit;
  }

  int64_t count_less(const T &bit) {
    return count_less(bit, MAX_LOG);
  }

  pair< T, BinaryTrie * > get_kth(int64_t k) {
    assert(0 < k && k <= size());
    return get_kth(k, MAX_LOG);
  }

  void propagate(int bit_index) {
    if((lazy >> bit_index) & 1) swap(nxt[0], nxt[1]);
    if(nxt[0]) nxt[0]->lazy ^= lazy;
    if(nxt[1]) nxt[1]->lazy ^= lazy;
    lazy = 0;
  }
};


ll f(vector<ll> v, ll b) {
    //cerr << v.size() << " " << b << endl;
    if(v.empty()) return 0;
    if(b == -1) return 0;
    vector<ll> a[2];
    for(int i = 0; i < v.size(); i++) {
        ll idx = (v[i] >> b) & 1;
        a[idx].push_back(v[i]);
    }
    if(a[0].size() % 2 == 0) {
        for(auto &val : a[1]) {
            val ^= (1 << b);
        }
        ll ret = max(f(a[0], b-1), f(a[1], b-1));
        return ret;
    } else {
        BinaryTrie<ll, 30> Trie;
        //cerr << b << " " << a[0].size() << " " << a[1].size() << endl;
        for(auto tmp : a[0]) Trie.add(tmp);
        ll ret = INF;
        for(auto tmp : a[1]) {
            Trie.xorpush(tmp);
            chmin(ret, Trie.min_element().first);
            Trie.xorpush(tmp);
        }
        return ret;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    vector<ll> A(2 * N);
    for(int i = 0; i < 2 * N; i++) cin >> A[i];
    ll ans = f(A, 29);
    cout << ans << endl;
    return 0;
}

提出情報

提出日時
問題 D - XOR Game
ユーザ kort0n
言語 C++ (GCC 9.2.1)
得点 700
コード長 4922 Byte
結果 AC
実行時間 651 ms
メモリ 399248 KiB

コンパイルエラー

./Main.cpp: In function ‘ll f(std::vector<long long int>, ll)’:
./Main.cpp:180:22: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  180 |     for(int i = 0; i < v.size(); i++) {
      |                    ~~^~~~~~~~~~
./Main.cpp: In instantiation of ‘BinaryTrie<T, MAX_LOG>::BinaryTrie() [with T = long long int; int MAX_LOG = 30]’:
./Main.cpp:191:28:   required from here
./Main.cpp:32:7: warning: ‘BinaryTrie<long long int, 30>::exist’ will be initialized after [-Wreorder]
   32 |   int exist;
      |       ^~~~~
./Main.cpp:31:5: warning:   ‘long long int BinaryTrie<long long int, 30>::lazy’ [-Wreorder]
   31 |   T lazy;
      |     ^~~~
./Main.cpp:36:3: warning:   when initialized here [-Wreorder]
   36 |   BinaryTrie() : exist(0), lazy(0), nxt{nullptr, nullptr} {}
      |   ^~~~~~~~~~
./Main.cpp:31:5: warning: ‘BinaryTrie<long long int, 30>::lazy’ will be initialized after [-Wreorder]
   31 |   T lazy;
      |     ^~~~
./Main.cpp:30:15: warning:   ‘BinaryTrie<long long int, 30>* BinaryTrie<long long int, 30>::nxt [2]’ [-Wreorder]
   30 |   BinaryTrie *nxt[2];
      |               ^~~
./Main.cpp:36:3: warning:   when initialized here [-Wreorder]
   36 |   BinaryTrie() : exist(0), lazy(0), nxt{nullptr, nullptr} {}
      |   ^~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 700 / 700
結果
AC × 3
AC × 50
セット名 テストケース
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt, 01-044.txt, 01-045.txt, 01-046.txt, 01-047.txt
ケース名 結果 実行時間 メモリ
00-sample-001.txt AC 12 ms 3552 KiB
00-sample-002.txt AC 2 ms 3552 KiB
00-sample-003.txt AC 2 ms 3652 KiB
01-001.txt AC 2 ms 3504 KiB
01-002.txt AC 220 ms 109240 KiB
01-003.txt AC 7 ms 4536 KiB
01-004.txt AC 165 ms 92132 KiB
01-005.txt AC 137 ms 10992 KiB
01-006.txt AC 191 ms 99664 KiB
01-007.txt AC 37 ms 16640 KiB
01-008.txt AC 30 ms 6628 KiB
01-009.txt AC 14 ms 5532 KiB
01-010.txt AC 181 ms 69880 KiB
01-011.txt AC 149 ms 44428 KiB
01-012.txt AC 97 ms 40740 KiB
01-013.txt AC 371 ms 182672 KiB
01-014.txt AC 443 ms 175480 KiB
01-015.txt AC 372 ms 179872 KiB
01-016.txt AC 88 ms 14456 KiB
01-017.txt AC 91 ms 13756 KiB
01-018.txt AC 94 ms 14096 KiB
01-019.txt AC 88 ms 13672 KiB
01-020.txt AC 257 ms 63176 KiB
01-021.txt AC 304 ms 89124 KiB
01-022.txt AC 307 ms 94424 KiB
01-023.txt AC 330 ms 100148 KiB
01-024.txt AC 324 ms 101016 KiB
01-025.txt AC 331 ms 106936 KiB
01-026.txt AC 406 ms 150144 KiB
01-027.txt AC 387 ms 137628 KiB
01-028.txt AC 393 ms 144592 KiB
01-029.txt AC 436 ms 175052 KiB
01-030.txt AC 443 ms 174940 KiB
01-031.txt AC 438 ms 181824 KiB
01-032.txt AC 321 ms 22880 KiB
01-033.txt AC 606 ms 399248 KiB
01-034.txt AC 386 ms 256824 KiB
01-035.txt AC 651 ms 367944 KiB
01-036.txt AC 303 ms 193968 KiB
01-037.txt AC 304 ms 192848 KiB
01-038.txt AC 291 ms 186736 KiB
01-039.txt AC 296 ms 185504 KiB
01-040.txt AC 295 ms 185420 KiB
01-041.txt AC 297 ms 185684 KiB
01-042.txt AC 335 ms 184632 KiB
01-043.txt AC 319 ms 184660 KiB
01-044.txt AC 345 ms 187656 KiB
01-045.txt AC 411 ms 179848 KiB
01-046.txt AC 361 ms 181740 KiB
01-047.txt AC 419 ms 186664 KiB