結果

問題 No.404 部分門松列
ユーザー btkbtk
提出日時 2016-07-27 17:11:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,405 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 178,680 KB
実行使用メモリ 51,584 KB
最終ジャッジ日時 2023-09-11 02:33:50
合計ジャッジ時間 23,689 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,504 KB
testcase_01 AC 3 ms
9,508 KB
testcase_02 AC 3 ms
9,560 KB
testcase_03 AC 2 ms
9,516 KB
testcase_04 AC 3 ms
9,592 KB
testcase_05 AC 4 ms
9,560 KB
testcase_06 AC 3 ms
9,612 KB
testcase_07 AC 3 ms
9,540 KB
testcase_08 AC 3 ms
9,536 KB
testcase_09 AC 3 ms
9,532 KB
testcase_10 AC 4 ms
9,812 KB
testcase_11 AC 3 ms
9,536 KB
testcase_12 AC 4 ms
9,700 KB
testcase_13 AC 626 ms
34,524 KB
testcase_14 AC 902 ms
41,600 KB
testcase_15 AC 552 ms
28,320 KB
testcase_16 AC 353 ms
21,640 KB
testcase_17 AC 929 ms
42,108 KB
testcase_18 AC 572 ms
33,684 KB
testcase_19 AC 220 ms
19,552 KB
testcase_20 AC 848 ms
40,228 KB
testcase_21 AC 747 ms
35,836 KB
testcase_22 AC 784 ms
39,220 KB
testcase_23 AC 1,006 ms
42,588 KB
testcase_24 AC 1,071 ms
42,600 KB
testcase_25 AC 1,405 ms
51,584 KB
testcase_26 AC 1,345 ms
48,472 KB
testcase_27 AC 550 ms
28,320 KB
testcase_28 AC 990 ms
42,584 KB
testcase_29 AC 895 ms
39,972 KB
testcase_30 AC 818 ms
37,124 KB
testcase_31 AC 272 ms
20,224 KB
testcase_32 AC 816 ms
39,484 KB
testcase_33 AC 1,048 ms
46,100 KB
testcase_34 AC 954 ms
42,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

struct I{
    I(){
        ios::sync_with_stdio(false);cin.tie(0);
    }
}cww;

namespace BIT_{
#define CNT 4
#define SZ 700000
    int cnt=0;
    LL mem[CNT][SZ];
}

class BIT{
private: LL *t;int n;
public:
    BIT(int n):t(BIT_::mem[BIT_::cnt++]),n(n){
        fill(t,t+n,0);
    }
    LL sum(int i){
        LL s=0;
        while(i>0){
            s+=t[i];
            i-=i&-i;
        }
        return s;
    }
    LL sum(int lb,int ub){
        return sum(ub)-sum(lb-1);
    }
    void add(int i,LL x){
        while(i<=n){
            t[i]+=x,i+=i&-i;
        }
    }
};


int main(){
    int N,Q;cin>>N;
    map<int,int> z;
    vector<int> a(N);
    for(auto &it:a){
        cin>>it;
        z[it]=0;
    }
    cin>>Q;
    vector<int> L(Q),H(Q);
    for(int i=0;i<Q;i++){
        cin>>L[i]>>H[i];
        z[L[i]]=z[H[i]]=0;
    }
    int Z=1;
    
    int LB=0;
    int UB=1e9+1;
    z[LB]=z[UB]=1;
    for(auto &it:z)it.second=Z++;
    LB=z[LB];
    UB=z[UB];
    for(auto &it:a)it=z[it];
    for(auto &it:L)it=z[it];
    for(auto &it:H)it=z[it];

    BIT res(Z),l(Z),r(Z),same(Z);
    for(auto &it:a)r.add(it,1);
    for(int i=0;i<N;i++){
        int v=a[i];
        
        same.add(v,-l.sum(v,v));
        r.add(v,-1);

        LL ans=0;
        ans+=l.sum(LB,v-1)*r.sum(LB,v-1)-same.sum(LB,v-1);
        ans+=l.sum(v+1,UB)*r.sum(v+1,UB)-same.sum(v+1,UB);
        res.add(v,ans);

        same.add(v,r.sum(v,v));
        l.add(v,1);
    }

    for(int i=0;i<Q;i++){
        cout<<res.sum(L[i],H[i])<<endl;
    }
    return 0;
}
0