結果

問題 No.404 部分門松列
ユーザー btk
提出日時 2016-07-27 17:11:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,545 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 179,252 KB
実行使用メモリ 48,256 KB
最終ジャッジ日時 2024-06-28 17:03:26
合計ジャッジ時間 24,711 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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