結果

問題 No.3444 Interval Xor MST
コンテスト
ユーザー 👑 potato167
提出日時 2026-01-04 12:04:34
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 995 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,943 ms
コンパイル使用メモリ 211,920 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-02-06 20:51:17
合計ジャッジ時間 3,521 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;ll f(ll n){
    ll res = 0;
    ll a = 1;
    while (n != 1){
        res += a * (n / 2);
        n -= n / 2;
        a *= 2;
    }
    return res;
}

ll h(ll n, ll m){
    if (n == 1) return 0;
    ll L = m, R = m + n - 1;
    ll d = 31;
    while ((L & (1ll << d)) == (R & (1ll << d))) d--;
    ll mask = ((1ll << (d + 1)) - 1);
    L &= mask;
    R &= mask;
    ll l = (1ll << d) - L;
    ll r = (R + 1 - (1ll << d));
    ll ans = f(r) + f(l);
    ans += (1ll << d);
    d--;
    while (d >= 0){
        if (l + r > (2ll << d)){
            break;
        }
        if (l > (1ll << d)) l -= (1ll << d);
        else if (r > (1ll << d)) r -= (1ll << d);
        else ans += (1ll << d);
        d--;
    }
    return ans;
}

int main(){
	std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int T;
    cin >> T;
    while (T--){
        int N, M;
        cin >> N >> M;
        cout << h(N, M) << "\n";
    }
}
0