結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

class UnionFind{
    private:
    vector<int> par,siz;
    public:
    UnionFind(int N){
        par.resize(N,-1);
        siz.resize(N,1);
    }
 
    int root(int x){ //連結成分の代表頂点を返す.
        if(par.at(x) == -1) return x;
        else return par.at(x) = root(par.at(x));
    }
    bool unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す.
        u = root(u),v = root(v);
        if(u == v) return false;
 
        if(siz.at(u) < siz.at(v)) swap(u,v); //Union by size.
        par.at(v) = u;
        siz.at(u) += siz.at(v);
        return true;
    }
    bool issame(int u, int v){ //同じ連結成分ならtrue.
        if(root(u) == root(v)) return true;
        else return false;
    }
    int size(int pos){return siz.at(root(pos));} //posの連結成分の大きさを返す.
};

long long solve(long long N,long long M){
    vector<long long> V(N);
    iota(V.begin(),V.end(),M);
    vector<tuple<long long,int,int>> edge;
    for(int i=0; i<N; i++) for(int k=i+1; k<N; k++) edge.push_back({V.at(i)^V.at(k),i,k});
    sort(edge.begin(),edge.end());
    UnionFind Z(N);
    long long answer = 0;
    for(auto [c,u,v] : edge){
        if(Z.size(0) == N) break;
        if(Z.unite(u,v)) answer += c;
    }
    return answer;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while(T--){
        long long N,M; cin >> N >> M;
        long long p2 = 1;
        long long answer = 0;
        for(int d=1; d<34; d++){
            long long d2 = 1LL<<d;
            long long l = M/d2,r = (M+N-1)/d2;
            long long L = M,R = N+M-1;
            if(l == r){
                d2 >>= 1;
                if((L&d2) != (R&d2)){
                    answer += d2,L %= d2,R %= d2;
                    while(L > R){
                        d2 >>= 1;
                        if((L&d2) != (R&d2)) answer += d2;
                        L %= d2,R %= d2;
                    }    
                }
                break;
            }
            answer += (r-l-1)*(d2>>1);
            L = M,R = (l+1)*d2-1;
            if((L&(d2>>1)) != (R&(d2>>1))) answer += (d2>>1);
            L = r*d2,R = M+N-1;
            if((L&(d2>>1)) != (R&(d2>>1))) answer += (d2>>1);
        }
        cout << answer << "\n";
    }
}
0