結果

問題 No.404 部分門松列
ユーザー latte0119latte0119
提出日時 2016-07-22 23:10:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,648 ms / 2,000 ms
コード長 2,794 bytes
コンパイル時間 2,843 ms
コンパイル使用メモリ 158,652 KB
実行使用メモリ 143,224 KB
最終ジャッジ日時 2023-09-11 02:27:33
合計ジャッジ時間 25,660 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
87,012 KB
testcase_01 AC 35 ms
87,264 KB
testcase_02 AC 34 ms
87,036 KB
testcase_03 AC 34 ms
87,040 KB
testcase_04 AC 36 ms
87,116 KB
testcase_05 AC 36 ms
87,100 KB
testcase_06 AC 35 ms
87,100 KB
testcase_07 AC 35 ms
87,052 KB
testcase_08 AC 35 ms
87,040 KB
testcase_09 AC 35 ms
87,324 KB
testcase_10 AC 36 ms
87,172 KB
testcase_11 AC 35 ms
86,980 KB
testcase_12 AC 35 ms
87,264 KB
testcase_13 AC 662 ms
109,240 KB
testcase_14 AC 599 ms
114,272 KB
testcase_15 AC 694 ms
109,972 KB
testcase_16 AC 1,188 ms
123,208 KB
testcase_17 AC 1,114 ms
124,828 KB
testcase_18 AC 317 ms
99,932 KB
testcase_19 AC 424 ms
102,420 KB
testcase_20 AC 827 ms
132,128 KB
testcase_21 AC 1,131 ms
125,228 KB
testcase_22 AC 367 ms
102,048 KB
testcase_23 AC 907 ms
135,624 KB
testcase_24 AC 1,615 ms
141,476 KB
testcase_25 AC 1,646 ms
143,224 KB
testcase_26 AC 1,648 ms
142,124 KB
testcase_27 AC 256 ms
95,672 KB
testcase_28 AC 638 ms
110,160 KB
testcase_29 AC 1,322 ms
133,636 KB
testcase_30 AC 836 ms
114,208 KB
testcase_31 AC 141 ms
89,020 KB
testcase_32 AC 1,092 ms
125,612 KB
testcase_33 AC 866 ms
117,628 KB
testcase_34 AC 1,005 ms
123,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

struct segtree{
    static const int SEG=1<<18;
    vector<vint>dat;
    segtree():dat(SEG*2){}
    void init(vint v){
        for(int i=0;i<v.size();i++)dat[i+SEG-1].pb(v[i]);
        for(int i=SEG-2;i>=0;i--){
            if(dat[i*2+1].size()+dat[i*2+2].size()==0)continue;
            dat[i].resize(dat[i*2+1].size()+dat[i*2+2].size());
            merge(all(dat[i*2+1]),all(dat[i*2+2]),dat[i].begin());
        }
    }
    int query(int a,int b,int ll,int hh,int k=0,int l=0,int r=SEG){
        if(r<=a||b<=l)return 0;
        if(a<=l&&r<=b)return lower_bound(all(dat[k]),hh)-lower_bound(all(dat[k]),ll);
        return query(a,b,ll,hh,k*2+1,l,(l+r)/2)+query(a,b,ll,hh,k*2+2,(l+r)/2,r);
    }
};

struct BIT{
    int N;
    vint dat;
    void init(int n){
        N=n;
        dat.resize(n+1);
    }
    void add(int k,int x){
        for(k++;k<=N;k+=k&-k)dat[k]+=x;
    }
    int sum(int k){
        int ret=0;
        for(k++;k;k-=k&-k)ret+=dat[k];
        return ret;
    }
};

int N;
vint A;
int Q;
int L[222222],H[222222];

int acc[777777];
int cnt[777777],cnt2[7777777];
signed main(){
    cin>>N;
    A.resize(N);
    rep(i,N)cin>>A[i];
    cin>>Q;
    rep(i,Q)cin>>L[i]>>H[i];

    vint as;
    rep(i,N)as.pb(A[i]);
    rep(i,Q){
        L[i]--;
        as.pb(L[i]);
        as.pb(H[i]);
    }

    sort(all(as));as.erase(unique(all(as)),as.end());
    rep(i,N)A[i]=lower_bound(all(as),A[i])-as.begin();
    rep(i,Q){
        L[i]=lower_bound(all(as),L[i])-as.begin();
        H[i]=lower_bound(all(as),H[i])-as.begin();
    }

    segtree seg;seg.init(A);
    rep(i,N)cnt[A[i]]++;
    BIT bit;bit.init(7777777);
    rep(i,N){

        int l=seg.query(0,i,0,A[i]);
        int r=seg.query(i+1,N,0,A[i]);
        acc[A[i]]+=l*r;
        l=seg.query(0,i,A[i]+1,1001001001);
        r=seg.query(i+1,N,A[i]+1,1001001001);
        acc[A[i]]+=l*r;

        acc[A[i]]-=bit.sum(A[i]-1);
        acc[A[i]]-=bit.sum(7777777)-bit.sum(A[i]);

        int tmp=bit.sum(A[i])-bit.sum(A[i]-1);
        cnt2[A[i]]++;
        bit.add(A[i],-tmp+cnt2[A[i]]*(cnt[A[i]]-cnt2[A[i]]));
    }

    for(int i=0;i<777777-1;i++)acc[i+1]+=acc[i];
    for(int i=0;i<Q;i++){
        printf("%lld\n",acc[H[i]]-acc[L[i]]);
    }
    return 0;
}
0