結果

問題 No.1240 Or Sum of Xor Pair
ユーザー carrot46carrot46
提出日時 2020-09-26 11:46:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 1,830 ms
コンパイル使用メモリ 175,996 KB
実行使用メモリ 21,060 KB
最終ジャッジ日時 2023-09-11 07:40:13
合計ジャッジ時間 5,590 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
20,876 KB
testcase_01 AC 15 ms
20,880 KB
testcase_02 AC 15 ms
20,952 KB
testcase_03 AC 17 ms
21,004 KB
testcase_04 AC 16 ms
20,876 KB
testcase_05 AC 16 ms
20,944 KB
testcase_06 AC 17 ms
21,044 KB
testcase_07 AC 17 ms
20,972 KB
testcase_08 AC 16 ms
20,972 KB
testcase_09 AC 16 ms
20,768 KB
testcase_10 AC 22 ms
20,852 KB
testcase_11 AC 22 ms
20,876 KB
testcase_12 AC 28 ms
20,768 KB
testcase_13 AC 27 ms
20,912 KB
testcase_14 AC 27 ms
20,840 KB
testcase_15 AC 75 ms
20,736 KB
testcase_16 AC 73 ms
20,784 KB
testcase_17 AC 69 ms
20,912 KB
testcase_18 AC 71 ms
20,744 KB
testcase_19 AC 69 ms
20,772 KB
testcase_20 AC 76 ms
20,740 KB
testcase_21 AC 74 ms
20,836 KB
testcase_22 AC 72 ms
20,880 KB
testcase_23 AC 69 ms
20,732 KB
testcase_24 AC 72 ms
20,776 KB
testcase_25 AC 63 ms
20,948 KB
testcase_26 AC 74 ms
20,816 KB
testcase_27 AC 16 ms
21,060 KB
testcase_28 AC 15 ms
20,736 KB
testcase_29 AC 43 ms
20,896 KB
testcase_30 AC 30 ms
20,840 KB
testcase_31 AC 32 ms
20,976 KB
testcase_32 AC 50 ms
20,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

ll res(0);
using Pint = pair<int, int>;
const int MAX_N = 200005;
vec a(MAX_N, 0), sum(MAX_N + 1, 0);
vector<vector<int> > bit_sum(18, vector<int>(MAX_N + 1, 0));

void calc1(Pint p){
    res += (sum[p.se] - sum[p.fi]) * (p.se - p.fi - 1);
    rep(i, 18) {
        ll num = bit_sum[i][p.se] - bit_sum[i][p.fi];
        res -= (1LL<<i) * (num * (num - 1) / 2);
    }
}

void calc2(Pint p, Pint q){
    res += (sum[p.se] - sum[p.fi]) * (q.se - q.fi) + (sum[q.se] - sum[q.fi]) * (p.se - p.fi);
    rep(i, 18){
        res -= (1LL<<i) * (bit_sum[i][p.se] - bit_sum[i][p.fi]) * (bit_sum[i][q.se] - bit_sum[i][q.fi]);
    }
}

void dfs(int bit, Pint &p, Pint &q){
    if(p.fi == p.se || bit == -1) return;
    int p_cen = lower_bound(a.begin() + p.fi, a.begin() + p.se, (a[p.fi] & ~((1<<(bit+1)) - 1)) + (1<<bit)) - a.begin();
    Pint p0(p.fi, p_cen), p1(p_cen, p.se);
    if(K < (1<<bit)){
        dfs(bit - 1, p0, q);
        dfs(bit - 1, p1, q);
    }else if(K < (1<<(bit+1))){
        calc1(p0);
        calc1(p1);
        dfs(bit - 1, p0, p1);
    }else{
        if(q.se == q.fi) return;
        int q_cen = lower_bound(a.begin() + q.fi, a.begin() + q.se, (a[q.fi] & ~((1<<(bit+1)) - 1)) + (1<<bit)) - a.begin();
        Pint q0(q.fi, q_cen), q1(q_cen, q.se);
        if((K>>bit)&1){
            calc2(p0, q0);
            calc2(p1, q1);
            dfs(bit - 1, p0, q1);
            dfs(bit - 1, p1, q0);
        }else{
            dfs(bit - 1, p0, q0);
            dfs(bit - 1, p1, q1);
        }
    }
}

int main() {
    cin>>N>>K;
    rep(i, N) scanf("%d", &a[i]);
    a.resize(N);
    sort(ALL(a));
    rep(i, N) sum[i+1] = sum[i] + a[i];
    rep(i, 18) rep(j, N) bit_sum[i][j+1] = bit_sum[i][j] + ((a[j]>>i)&1);
    Pint p(0, N), q(N, N);
    dfs(18, p, q);
    cout<<res<<endl;
}
0