結果

問題 No.2300 Substring OR Sum
ユーザー HIcoderHIcoder
提出日時 2023-07-06 18:27:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 963 ms
コンパイル使用メモリ 105,084 KB
実行使用メモリ 26,468 KB
最終ジャッジ日時 2023-09-27 19:20:41
合計ジャッジ時間 3,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 34 ms
6,648 KB
testcase_04 AC 28 ms
5,964 KB
testcase_05 AC 33 ms
6,776 KB
testcase_06 AC 17 ms
5,008 KB
testcase_07 AC 115 ms
14,656 KB
testcase_08 AC 48 ms
7,996 KB
testcase_09 AC 39 ms
6,896 KB
testcase_10 AC 96 ms
12,336 KB
testcase_11 AC 114 ms
13,584 KB
testcase_12 AC 124 ms
14,712 KB
testcase_13 AC 113 ms
26,468 KB
testcase_14 AC 30 ms
4,376 KB
testcase_15 AC 54 ms
8,216 KB
testcase_16 AC 92 ms
12,168 KB
testcase_17 AC 116 ms
14,484 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
AC
[L,R]にひとつでも2^kのbitが1含まれていたら 答えにカウントされる.
bitごとに考えて(貢献度をとる)和をとれば答え


*/
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<ll,ll> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const double PI=acos(-1);

ll mod_pow(ll x,ll y,ll mod){
    ll power=x;
    ll ret = 1;
    while(y>0){
        if(y&1){
           ret = ret*power%mod;
        }
        power = power*power%mod;
        y = y>>1;
    }
    return ret;

}

int main()
{
    int N;
    cin>>N;
    vector<int> A(N+1);
    for(int i=1;i<=N;i++){
        cin>>A[i];
    }    
    vector<vector<int>> d(30);


    for(int k=0;k<30;k++){
        d[k].push_back(0);
        for(int i=1;i<=N;i++){
            if(((A[i]>>k)&1)==1){
                d[k].push_back(i);
            }
        }
        d[k].push_back(N+1);
    }   

    vector<ll> pow2(30,0);
    pow2[0]=1;
    for(int i=1;i<30;i++){
        pow2[i]=pow2[i-1]*2;
    }

  
    auto f=[&](ll len){

        ll res=1LL*len;
        res*=(len+1);
        res/=2;

        return res;

    };

    ll ans=0;

    for(int k=0;k<30;k++){
        
        ll sumrange=f(N);

        for(int j=0;j+1<d[k].size();j++){
            int c1=d[k][j]+1;
            int c2=d[k][j+1]-1;
            //(d[k][j],d[k][j+1])=[ d[k][j]+1,d[k][j+1]-1]=[c1,c2]
            if(c1>c2) continue;
            int len=c2-c1+1;

            ll range=f(len);

            sumrange-=range;
            

        }

        //cout<<"k="<<k<<",sumrange="<<sumrange<<endl;

        ans+=sumrange*pow2[k];
       
    }

    cout<<ans<<endl;





}
0