結果

問題 No.404 部分門松列
ユーザー kimiyukikimiyuki
提出日時 2016-07-23 03:17:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,331 bytes
コンパイル時間 1,333 ms
コンパイル使用メモリ 95,344 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-04-24 07:06:28
合計ジャッジ時間 6,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 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 6 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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)
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,int> count_kadomatsu(vector<int> const & a, int first, int last) {
    int n = a.size();
    int direction = first < last ? 1 : -1;
    map<int,int> acc;
    acc[first] = 0;
    map<int,vector<int> > que;
    repeat (i,n) que[direction * a[i]].push_back(i);
    segment_tree<int> segtree(n, 0, plus<int>());
    map<int,int> lset, rset; int split = 0; // [0,split), [split,n)
    for (auto & it : que) {
        int ai; vector<int> is; tie(ai, is) = it;
        ai *= direction;
        int cnt = acc[first];
        first = ai;
        for (int i : is) {
            int l = segtree.range_concat(0, i);
            int r = segtree.range_concat(i+1, n);
            cnt += l * r;
            for (; split < i; ++ split) {
                if (rset[a[split]]) {
                    rset[a[split]] -= 1;
                    lset[a[split]] += 1;
                }
            }
            for (; i < split; -- split) {
                if (lset[a[split-1]]) {
                    lset[a[split-1]] -= 1;
                    rset[a[split-1]] += 1;
                }
            }
            for (auto it : lset) {
                cnt -= it.second * rset[it.first];
            }
        }
        for (int i : is) {
            segtree.point_update(i, 1);
            (i < split ? lset : rset)[ai] += 1;
        }
        acc[ai] = cnt;
    }
    acc[last] = 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,int> acch = count_kadomatsu(a, -1, 1e9+7); // a1 < a2, a2 > a3
    map<int,int> accl = count_kadomatsu(a, 1e9+7, -1); // a1 > a2, a2 < a3
    // output
    int q; scanf("%d", &q);
    while (q --) {
        int l, h; scanf("%d%d", &l, &h);
        int hr = (-- acch.upper_bound(h  ))->second;
        int hl = (-- acch.upper_bound(l-1))->second;
        int lr = (   accl.lower_bound(h+1))->second;
        int ll = (   accl.lower_bound(l  ))->second;
        printf("%d\n", hr - hl + ll - lr);
    }
    return 0;
}
0