結果

問題 No.404 部分門松列
ユーザー kimiyukikimiyuki
提出日時 2016-07-23 00:11:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,281 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 104,492 KB
実行使用メモリ 33,476 KB
最終ジャッジ日時 2024-04-24 03:09:35
合計ジャッジ時間 15,170 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,760 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1,751 ms
6,532 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 TLE -
testcase_18 WA -
testcase_19 WA -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 WA -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
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 <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(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 <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }
template <typename T, typename X> auto vectors(T a, X x) { return vector<T>(x, a); }
template <typename T, typename X, typename Y, typename... Zs> auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector<decltype(cont)>(x, cont); }

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> cnt(n, 0, plus<int>());
    for (auto it : que) {
        int ai; vector<int> is; tie(ai, is) = it;
        ai *= direction;
        acc[ai] = acc[first];
        first = ai;
        for (int i : is) {
            fprintf(stderr, "a[i] = %d, i = %d, [0,%d) = %d, (%d,%d) = %d\n", ai, i, i, cnt.range_concat(0, i), i, n, cnt.range_concat(i+1, n));
            acc[ai] += cnt.range_concat(0, i) * cnt.range_concat(i+1, n); // acc[ai] += cnt[0,i) * cnt(i,n)
        }
        for (int i : is) {
            cnt.point_update(i, cnt.range_concat(i,i+1) + 1); // cnt[i] += 1
        }
    }
    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
    int max_a = *whole(max_element, a);
    map<int,int> acch = count_kadomatsu(a, -1, max_a+1); // a1 < a2, a2 > a3
    map<int,int> accl = count_kadomatsu(a, max_a+1, -1); // a1 > a2, a2 < a3
    for (auto it : acch) {
        fprintf(stderr, "accr %d %d\n", it.first, it.second);
    }
    for (auto it : accl) {
        fprintf(stderr, "accl %d %d\n", it.first, it.second);
    }
    // output
    int q; scanf("%d", &q);
    while (q --) {
        int l, h; scanf("%d%d", &l, &h);
        fprintf(stderr, "query %d %d\n", l, h);
        int hr, hl, lr, ll;
        auto it = acch.upper_bound(h);
        -- it;
        // fprintf(stderr, "hri %d\n", it->first);
        hr = it->second;
        it = acch.upper_bound(l-1);
        -- it;
        // fprintf(stderr, "hli %d\n", it->first);
        hl = it->second;
        it = accl.upper_bound(h+1);
        -- it;
        fprintf(stderr, "lri %d\n", it->first);
        lr = it->second;
        it = accl.upper_bound(l);
        -- it;
        fprintf(stderr, "lli %d\n", it->first);
        ll = it->second;
        fprintf(stderr, "h %d\n", hr - hl);
        fprintf(stderr, "l %d\n", ll - lr);
        printf("%d\n", hr - hl + ll - lr);
    }
    return 0;
}
0