結果

問題 No.318 学学学学学
ユーザー kimiyukikimiyuki
提出日時 2016-09-01 21:52:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 2,862 bytes
コンパイル時間 1,563 ms
コンパイル使用メモリ 111,856 KB
実行使用メモリ 12,140 KB
最終ジャッジ日時 2023-09-04 20:32:39
合計ジャッジ時間 5,704 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
4,380 KB
testcase_01 AC 28 ms
5,052 KB
testcase_02 AC 34 ms
5,308 KB
testcase_03 AC 22 ms
4,748 KB
testcase_04 AC 30 ms
5,280 KB
testcase_05 AC 186 ms
12,140 KB
testcase_06 AC 157 ms
9,004 KB
testcase_07 AC 136 ms
7,948 KB
testcase_08 AC 118 ms
7,076 KB
testcase_09 AC 104 ms
6,600 KB
testcase_10 AC 86 ms
6,048 KB
testcase_11 AC 185 ms
12,120 KB
testcase_12 AC 134 ms
8,948 KB
testcase_13 AC 117 ms
7,948 KB
testcase_14 AC 102 ms
7,128 KB
testcase_15 AC 87 ms
6,540 KB
testcase_16 AC 70 ms
6,128 KB
testcase_17 AC 133 ms
9,004 KB
testcase_18 AC 113 ms
8,928 KB
testcase_19 AC 132 ms
8,952 KB
testcase_20 AC 54 ms
6,068 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,384 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <functional>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
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; }

// TODO: generalize
template <typename T, typename U=T>
struct starry_sky_tree {
    int n;
    vector<T> a; // elements and the cache
    vector<U> b; // lazied queries
    function<T (T, T)> append; // associative
    function<U (U, U)> foo;
    // function<T (T, U, int)> bar;
    function<T (T, U)> bar;
    T unit;
    template <typename F, typename G, typename H>
    explicit starry_sky_tree(int a_n, T a_unit, F a_append, G a_foo, H a_bar) {
        n = pow(2,ceil(log2(a_n)));
        a.resize(2*n-1, a_unit);
        b.resize(2*n-1, a_unit);
        unit = a_unit;
        append = a_append;
        foo = a_foo;
        bar = a_bar;
    }
    void range_update(int l, int r, U z) {
        range_update(0, 0, n, l, r, z);
    }
    void range_update(int i, int il, int ir, int l, int r, U z) {
        if (l <= il and ir <= r) {
            b[i] = foo(b[i], z);
            a[i] = bar(a[i], z);
        } else if (ir <= l or r <= il) {
            // nop
        } else {
            range_update(2*i+1, il, (il+ir)/2, l, r, z);
            range_update(2*i+2, (il+ir)/2, ir, l, r, z);
            a[i] = bar(
                    append(a[2*i+1], a[2*i+2]),
                    b[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 bar(
                    append(
                        range_concat(2*i+1, il, (il+ir)/2, l, r),
                        range_concat(2*i+2, (il+ir)/2, ir, l, r)),
                    b[i]);
        }
    }
};

int main() {
    // input
    int n; cin >> n;
    vector<int> as(n); repeat (i,n) cin >> as[i];
    // compute
    map<int,pair<int,int> > q;
    repeat (i,n) {
        if (not q.count(as[i])) q[as[i]] = { i, i };
        setmin(q[as[i]].first,  i);
        setmax(q[as[i]].second, i);
    }
    auto f = [](int a, int b) { return max(a, b); };
    starry_sky_tree<int> t(n, 0, f, f, f);
    for (auto it : q) {
        int a = it.first;
        int l, r; tie(l, r) = it.second;
        t.range_update(l, r+1, a);
    }
    // output
    repeat (i,n) {
        if (i) cout << ' ';
        cout << t.range_concat(i, i+1);
    }
    cout << endl;
    return 0;
}
0