結果

問題 No.1240 Or Sum of Xor Pair
ユーザー carrot46carrot46
提出日時 2020-09-26 11:39:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 2,360 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 178,244 KB
実行使用メモリ 21,068 KB
最終ジャッジ日時 2023-09-11 07:34:00
合計ジャッジ時間 5,490 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
21,044 KB
testcase_01 AC 15 ms
20,856 KB
testcase_02 AC 15 ms
21,068 KB
testcase_03 AC 16 ms
20,744 KB
testcase_04 AC 16 ms
20,916 KB
testcase_05 AC 16 ms
20,960 KB
testcase_06 AC 17 ms
20,748 KB
testcase_07 AC 16 ms
20,876 KB
testcase_08 AC 17 ms
20,868 KB
testcase_09 AC 16 ms
20,880 KB
testcase_10 AC 22 ms
21,064 KB
testcase_11 AC 22 ms
20,948 KB
testcase_12 AC 28 ms
21,056 KB
testcase_13 AC 27 ms
20,928 KB
testcase_14 AC 27 ms
20,868 KB
testcase_15 AC 74 ms
20,872 KB
testcase_16 AC 72 ms
20,924 KB
testcase_17 AC 68 ms
21,068 KB
testcase_18 AC 69 ms
21,044 KB
testcase_19 AC 68 ms
20,872 KB
testcase_20 AC 75 ms
20,748 KB
testcase_21 AC 74 ms
20,880 KB
testcase_22 AC 70 ms
20,748 KB
testcase_23 AC 68 ms
20,872 KB
testcase_24 AC 71 ms
20,880 KB
testcase_25 AC 62 ms
20,752 KB
testcase_26 AC 73 ms
20,752 KB
testcase_27 AC 15 ms
20,816 KB
testcase_28 AC 15 ms
20,772 KB
testcase_29 AC 43 ms
20,924 KB
testcase_30 AC 30 ms
20,872 KB
testcase_31 AC 32 ms
20,796 KB
testcase_32 AC 51 ms
21,064 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(int l, int r){
    res += (sum[r] - sum[l]) * (r - l - 1);
    rep(i, 18) {
        ll num = bit_sum[i][r] - bit_sum[i][l];
        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();
    if(K < (1<<bit)){
        dfs(bit - 1, {p.fi, p_cen}, q);
        dfs(bit - 1, {p_cen, p.se}, q);
    }else if(K < (1<<(bit+1))){
        calc1(p.fi, p_cen);
        calc1(p_cen, p.se);
        dfs(bit - 1, {p.fi, p_cen}, {p_cen, p.se});
    }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();
        if((K>>bit)&1){
            calc2({p.fi, p_cen}, {q.fi, q_cen});
            calc2({p_cen, p.se}, {q_cen, q.se});
            dfs(bit - 1, {p.fi, p_cen}, {q_cen, q.se});
            dfs(bit - 1, {p_cen, p.se}, {q.fi, q_cen});
        }else{
            dfs(bit - 1, {p.fi, p_cen}, {q.fi, q_cen});
            dfs(bit - 1, {p_cen, p.se}, {q_cen, q.se});
        }
    }
}

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);
    dfs(18, make_pair(0, N), make_pair(N, N));
    cout<<res<<endl;
}
0