結果

問題 No.318 学学学学学
ユーザー xuzijian629xuzijian629
提出日時 2018-11-04 20:33:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 3,307 bytes
コンパイル時間 2,952 ms
コンパイル使用メモリ 225,880 KB
実行使用メモリ 18,600 KB
最終ジャッジ日時 2023-09-04 21:16:27
合計ジャッジ時間 8,230 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
4,716 KB
testcase_01 AC 30 ms
6,036 KB
testcase_02 AC 37 ms
6,640 KB
testcase_03 AC 24 ms
5,188 KB
testcase_04 AC 32 ms
6,076 KB
testcase_05 AC 240 ms
18,572 KB
testcase_06 AC 267 ms
13,996 KB
testcase_07 AC 240 ms
11,688 KB
testcase_08 AC 219 ms
10,540 KB
testcase_09 AC 202 ms
9,764 KB
testcase_10 AC 180 ms
9,028 KB
testcase_11 AC 240 ms
18,600 KB
testcase_12 AC 186 ms
14,008 KB
testcase_13 AC 168 ms
11,740 KB
testcase_14 AC 156 ms
10,540 KB
testcase_15 AC 149 ms
9,800 KB
testcase_16 AC 144 ms
9,064 KB
testcase_17 AC 180 ms
14,052 KB
testcase_18 AC 139 ms
14,008 KB
testcase_19 AC 179 ms
13,944 KB
testcase_20 AC 143 ms
9,056 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

template <class T, class T2>
class LazySegTree {
    using F = function<T(T, T)>;
    using G = function<T(T, T2)>;
    using H = function<T2(T2, T2)>;
    using P = function<T2(T2, int)>;
    int n;
    vector<T> data;
    vector<T2> lazy;
    const F f;
    const G g;
    const H h;
    const P p;
    const T e;
    const T2 e2;

public:
    LazySegTree(const vector<T>& as, const F f, const G g, const H h, const P p, const T& e, const T2 e2) : f(f), g(g), h(h), p(p), e(e), e2(e2) {
        n = 1;
        while (n < as.size()) n <<= 1;
        data.assign(2 * n, e);
        lazy.assign(2 * n, e2);
        for (int i = 0; i < as.size(); i++) {
            data[n + i] = as[i];
        }
        for (int i = n - 1; i > 0; i--) {
            data[i] = f(data[2 * i], data[2 * i + 1]);
        }
    }

    void propagate(int k, int len) {
        if (lazy[k] != e2) {
            if (k < n) {
                lazy[2 * k] = h(lazy[2 * k], lazy[k]);
                lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            }
            data[k] = g(data[k], p(lazy[k], len));
            lazy[k] = e2;
        }
    }

    T update(int a, int b, const T2& x, int k, int l, int r) {
        propagate(k, r - l);
        if (r <= a || b <= l) {
            return data[k];
        } else if (a <= l && r <= b) {
            lazy[k] = h(lazy[k], x);
            propagate(k, r - l);
            return data[k];
        } else {
            return data[k] = f(update(a, b, x, 2 * k, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
        }
    }

    T update(int a, int b, const T2& x) {
        return update(a, b, x, 1, 0, n);
    }

    T query(int a, int b, int k, int l, int r) {
        propagate(k, r - l);
        if (r <= a || b <= l) {
            return e;
        } else if (a <= l && r <= b) {
            return data[k];
        } else {
            return f(query(a, b, 2 * k, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r));
        }
    }

    T query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};

void printvec(vi& x, int size = 0) {
    if (x.size() == 0) {
        cout << endl;
        return;
    }
    cout << x.front();
    for (int i = 1; i < (size == 0 ? x.size() : size); i++) {
        cout << " " << x[i];
    }
    cout << endl;
}

int main() {
    int n;
    cin >> n;
    vi as(n);

    unordered_map<int, int> minl, maxr;

    for (int i = 0; i < n; i++) {
        cin >> as[i];
        if (minl.count(as[i])) {
            minl[as[i]] = min(minl[as[i]], i);
            maxr[as[i]] = max(maxr[as[i]], i);
        } else {
            minl[as[i]] = i;
            maxr[as[i]] = i;
        }
    }

    // RUQ, RMQ
    auto f = [](i64 a, i64 b) {return min(a, b);};
    auto g = [](i64 a, i64 b) {return b < 0 ? a : b;};
    auto h = [](i64 a, i64 b) {return b < 0 ? a : b;};
    auto p = [](i64 a, int b) {return a;};
    LazySegTree<i64, i64> tree(as, f, g, h, p, 1e18, -1);
    
    sort(as.begin(), as.end());

    for (int a: as) {
        tree.update(minl[a], maxr[a] + 1, a);
    }

    vi ans;
    for (int i = 0; i < n; i++) {
        ans.push_back(tree.query(i, i + 1));
    }

    printvec(ans);
}
0