結果

問題 No.404 部分門松列
ユーザー kimiyukikimiyuki
提出日時 2016-07-23 04:07:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,415 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 99,128 KB
実行使用メモリ 54,708 KB
最終ジャッジ日時 2024-04-24 07:09:51
合計ジャッジ時間 20,309 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 674 ms
22,356 KB
testcase_14 AC 165 ms
6,144 KB
testcase_15 AC 274 ms
5,736 KB
testcase_16 AC 988 ms
31,628 KB
testcase_17 AC 1,309 ms
40,192 KB
testcase_18 AC 313 ms
9,844 KB
testcase_19 AC 382 ms
18,520 KB
testcase_20 AC 253 ms
8,340 KB
testcase_21 AC 1,136 ms
30,208 KB
testcase_22 AC 386 ms
9,888 KB
testcase_23 AC 261 ms
8,416 KB
testcase_24 AC 577 ms
7,860 KB
testcase_25 TLE -
testcase_26 AC 1,558 ms
34,048 KB
testcase_27 AC 61 ms
5,376 KB
testcase_28 AC 755 ms
21,568 KB
testcase_29 AC 1,268 ms
32,560 KB
testcase_30 AC 325 ms
5,740 KB
testcase_31 AC 29 ms
5,376 KB
testcase_32 AC 1,200 ms
41,608 KB
testcase_33 AC 1,077 ms
31,156 KB
testcase_34 AC 402 ms
6,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <tuple>
#include <functional>
#include <cmath>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) y) { return (f)(begin(y), end(y), ## __VA_ARGS__); })(x)
typedef long long ll;
using namespace std;

template <typename T>
struct segment_tree { // on monoid
    int n;
    vector<T> a;
    function<T (T,T)> append; // associative
    T unit;
    template <typename F>
    segment_tree(int a_n, T a_unit, F a_append) {
        n = pow(2,ceil(log2(a_n)));
        a.resize(2*n-1, a_unit);
        unit = a_unit;
        append = a_append;
    }
    void point_update(int i, T z) {
        a[i+n-1] = z;
        for (i = (i+n)/2; i > 0; i /= 2) {
            a[i-1] = append(a[2*i-1], a[2*i]);
        }
    }
    T range_concat(int l, int r) {
        return range_concat(0, 0, n, l, r);
    }
    T range_concat(int i, int il, int ir, int l, int r) {
        if (l <= il and ir <= r) {
            return a[i];
        } else if (ir <= l or r <= il) {
            return unit;
        } else {
            return append(
                    range_concat(2*i+1, il, (il+ir)/2, l, r),
                    range_concat(2*i+2, (il+ir)/2, ir, l, r));
        }
    }
};

map<int,ll> count_kadomatsu(vector<int> const & as, int first, int last) {
    int n = as.size();
    int direction = first < last ? 1 : -1;
    map<int,ll> acc;
    acc[first] = 0;
    map<int,vector<int> > que;
    repeat (i,n) que[direction * as[i]].push_back(i);
    segment_tree<int> cnt(n, 0, plus<int>());
    for (auto & it : que) {
        int a; vector<int> is; tie(a, is) = it;
        a *= direction;
        acc[a] = acc[first];
        first = a;
        for (int i : is) {
            int l = cnt.range_concat(0, i);
            int r = cnt.range_concat(i+1, n);
            acc[a] += l *(ll) r;
        }
        for (int i : is) {
            cnt.point_update(i, 1);
        }
    }
    return acc;
}

map<int,ll> count_same(vector<int> const & as) {
    int n = as.size();
    map<int,ll> dp;
    map<int,int> total; repeat (i,n) total[as[i]] += 1;
    map<int,int> used;
    ll cur = 0;
    for (int a : as) {
        dp[a] += cur - used[a] *(ll) (total[a] - used[a]);
        cur -= used[a];
        used[a] += 1;
        cur += total[a] - used[a];
    }
    map<int,ll> acc;
    int first = -1;
    acc[first] = 0;
    for (auto it : dp) {
        acc[it.first] = acc[first] + it.second;
        first = it.first;
    }
    acc[1e9+7] = acc[first];
    return acc;
}

int main() {
    // input
    int n; scanf("%d", &n);
    vector<int> a(n); repeat (i,n) scanf("%d", &a[i]);
    // compute
    map<int,ll> acch = count_kadomatsu(a, -1, 1e9+7); // a1 < a2, a2 > a3
    map<int,ll> accl = count_kadomatsu(a, 1e9+7, -1); // a1 > a2, a2 < a3
    map<int,ll> accs = count_same(a);
    // output
    int q; scanf("%d", &q);
    while (q --) {
        int l, h; scanf("%d%d", &l, &h);
        ll ahr = (-- acch.upper_bound(h  ))->second;
        ll ahl = (-- acch.upper_bound(l-1))->second;
        ll alr = (   accl.lower_bound(h+1))->second;
        ll all = (   accl.lower_bound(l  ))->second;
        ll asr = (-- accs.upper_bound(h  ))->second;
        ll asl = (-- accs.upper_bound(l-1))->second;
        printf("%lld\n", (ahr - ahl) + (all - alr) - (asr - asl));
    }
    return 0;
}
0