結果

問題 No.3444 Interval Xor MST
コンテスト
ユーザー Kude
提出日時 2026-02-07 00:10:34
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,827 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,830 ms
コンパイル使用メモリ 358,904 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-07 00:10:41
合計ジャッジ時間 6,852 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  VL costs(35);
  for (int i = 1; i < 35; i++) {
    costs[i] = 2 * costs[i-1] + (1LL << (i - 1));
  }
  auto f = [&](ll n) {
    if (n == 0) return 0LL;
    int k = __lg(n);
    ll ans = 0;
    rep(i, k + 1) if (n >> i & 1) {
      ans += costs[i];
    }
    int pre = k;
    rrep(i, k) if (n >> i & 1) ans += 1LL << pre, pre = i;
    return ans;
  };
  while (tt--) {
    ll n, m;
    cin >> n >> m;
    ll l = m, r = n + m;
    int k = __lg(l ^ r);
    ll c = r & ~((1LL << k) - 1);
    ll ans = f(r - c) + f(c - l);
    if (c < r) {
      ll k2 = 1LL << k;
      ll v = l ^ k2;
      ans += k2;
      if (v >= r) {
        ll mn = 1e18;
        ll now = 0;
        for (int i = k - 1; i >= 0; i--) {
          if (v >> i & 1) {
            ll nv = v ^ (1LL << i);
            if (nv < r) {
              chmin(mn, now + (1LL << i));
            } else {
              now += 1LL << i;
              v = nv;
            }
          }
        }
        ans += mn;
      }
    }
    cout << ans << '\n';
  }
}
0