結果

問題 No.318 学学学学学
ユーザー ama_nukoama_nuko
提出日時 2018-07-09 13:21:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 3,063 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 112,936 KB
実行使用メモリ 20,528 KB
最終ジャッジ日時 2023-09-04 21:05:47
合計ジャッジ時間 6,654 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
4,620 KB
testcase_01 AC 39 ms
6,488 KB
testcase_02 AC 45 ms
6,820 KB
testcase_03 AC 30 ms
5,488 KB
testcase_04 AC 39 ms
6,540 KB
testcase_05 AC 275 ms
20,528 KB
testcase_06 AC 208 ms
14,120 KB
testcase_07 AC 179 ms
12,240 KB
testcase_08 AC 144 ms
10,448 KB
testcase_09 AC 119 ms
9,056 KB
testcase_10 AC 92 ms
8,124 KB
testcase_11 AC 269 ms
20,456 KB
testcase_12 AC 180 ms
14,388 KB
testcase_13 AC 145 ms
12,028 KB
testcase_14 AC 121 ms
10,504 KB
testcase_15 AC 101 ms
9,060 KB
testcase_16 AC 77 ms
8,128 KB
testcase_17 AC 162 ms
14,204 KB
testcase_18 AC 136 ms
14,092 KB
testcase_19 AC 161 ms
14,076 KB
testcase_20 AC 64 ms
8,048 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

template <typename T>
class lazy_segment_tree{
    private:
        static int calc_size(int n){
            int m = 1;
            while(m < n){
                m *= 2;
            }
            return m;
        }

        void eval(int i, int l, int r){
            if(lazy[i] != -1){
                node[i] = lazy[i];
                if(r - l > 1){
                    lazy[i * 2 + 1] = lazy[i];
                    lazy[i * 2 + 2] = lazy[i];
                }
                lazy[i] = -1;
            }
        }

        void update(int s, int t, int x, int i, int l, int r){
            eval(i, l, r);

            if(t <= l || r <= s){
                return;
            }

            if(s <= l && r <= t){
                lazy[i] = x;
                eval(i, l, r);
                return;
            }

            int m = l + (r - l) / 2;
            update(s, t, x, 2 * i + 1, l, m);
            update(s, t, x, 2 * i + 2, m, r);
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }

        T query(int s, int t, int i, int l, int r){
            eval(i, l, r);

            if(t <= l || r <= s){
                return init;
            }

            if(s <= l && r <= t){
                return node[i];
            }

            int m = l + (r - l) / 2;
            T vl = query(s, t, i * 2 + 1, l, m);
            T vr = query(s, t, i * 2 + 2, m, r);
            return f(vl, vr);
        }

    public:
        int n;
        vector<T> node, lazy;
        T init;
        function<T(T, T)> f;

        lazy_segment_tree(int n, T init, function<T(T, T)> f)
            : n(calc_size(n)), node(calc_size(n) * 2, init) , lazy(calc_size(n) * 2, -1), init(init), f(f) {}

        T query(int s, int t){
            return query(s, t, 0, 0, n);
        }

        void update(int s, int t, int x){
            update(s, t, x, 0, 0, n);
        }
};

signed main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    map<int, int> left, right;
    rep(i, n){
        if(left.find(a[i]) != left.end()){
            continue;
        }
        left.emplace(a[i], i);
    }
    for(int i = n-1; i >= 0; i--){
        if(right.find(a[i]) != right.end()){
            continue;
        }
        right.emplace(a[i], i);
    }

    lazy_segment_tree<int> lst(n, 0, [](int a, int b){return a+b;});
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    rep(i, (int)a.size()){
        lst.update(left[a[i]], right[a[i]]+1, a[i]);
    }

    rep(i, n-1){
        cout << lst.query(i, i+1) << " ";
    }
    cout << lst.query(n-1, n) << endl;

    return 0;
}
0