結果

問題 No.404 部分門松列
ユーザー parukiparuki
提出日時 2016-07-26 22:59:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,323 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 175,888 KB
実行使用メモリ 10,572 KB
最終ジャッジ日時 2023-09-11 02:32:01
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 113 ms
5,844 KB
testcase_14 AC 83 ms
4,384 KB
testcase_15 AC 94 ms
4,376 KB
testcase_16 AC 115 ms
7,412 KB
testcase_17 AC 176 ms
8,152 KB
testcase_18 AC 86 ms
4,376 KB
testcase_19 AC 58 ms
5,160 KB
testcase_20 AC 98 ms
4,696 KB
testcase_21 AC 154 ms
7,428 KB
testcase_22 AC 106 ms
4,376 KB
testcase_23 AC 114 ms
4,776 KB
testcase_24 AC 173 ms
4,776 KB
testcase_25 AC 262 ms
10,572 KB
testcase_26 AC 234 ms
8,120 KB
testcase_27 AC 48 ms
4,376 KB
testcase_28 AC 143 ms
5,512 KB
testcase_29 AC 178 ms
7,632 KB
testcase_30 AC 119 ms
4,380 KB
testcase_31 AC 28 ms
4,376 KB
testcase_32 AC 152 ms
8,516 KB
testcase_33 AC 165 ms
6,868 KB
testcase_34 AC 143 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

struct BIT{
    int n;
    vector<long long> t;
    BIT(){}
    BIT(int _n):n(_n+1), t(_n+1){}
    void add(int k, long long val){
        k++;
        while(k < n){
            t[k] += val;
            k += (k&-k);
        }
    }
    long long sum(int k){
        ll r = 0;
        while(k > 0){
            r += t[k];
            k -= (k&-k);
        }
        return r;
    }
    long long sum(int l, int r){
        return sum(r) - sum(l);
    }
};

template<class Val>
vector<Val> compress(vector<Val> &v){
    vector<Val> a = v;
    sort(all(a));
    a.erase(unique(all(a)), a.end());
    each(b, v)b = (int)(lower_bound(all(a), b) - a.begin());
    return a;
}

int N, Q;
vi a;

int main(){
    cin >> N;
    a.resize(N);
    rep(i, N)scanf("%d", &a[i]);

    auto b = compress(a);
    const int M = sz(b);
    vll cnl(M), cnr(M);
    BIT bitl(M), bitr(M), ans(M);
    
    for(int i = 1; i <= N - 1; ++i){
        cnr[a[i]]++;
        bitr.add(a[i], 1);
    }

    ll bad = 0;
    for(int i = 1; i <= N - 2; ++i){
        bad -= cnl[a[i - 1]] * cnr[a[i - 1]];
        if(a[i - 1] != a[i])bad -= cnl[a[i]] * cnr[a[i]];

        cnl[a[i - 1]]++;
        cnr[a[i]]--;
        bitl.add(a[i - 1], 1);
        bitr.add(a[i], -1);

        bad += cnl[a[i - 1]] * cnr[a[i - 1]];
        if(a[i - 1] != a[i])bad += cnl[a[i]] * cnr[a[i]];

        ll sum = bitl.sum(0, a[i])*bitr.sum(0, a[i]) + bitl.sum(a[i] + 1, M)*bitr.sum(a[i] + 1, M);
        sum = sum - bad + cnl[a[i]] * cnr[a[i]];

        ans.add(a[i], sum);
    }

    auto f = [&](int x){
        return (int)(lower_bound(all(b), x) - b.begin());
    };

    cin >> Q;
    while(Q--){
        int L, R;
        scanf("%d%d", &L, &R);
        R++;
        L = f(L);
        R = f(R);
        ll res = ans.sum(L, R);
        printf("%lld\n", res);
    }
}
0