結果

問題 No.318 学学学学学
ユーザー playrollerplayroller
提出日時 2019-05-28 09:58:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,847 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 186,156 KB
実行使用メモリ 14,820 KB
最終ジャッジ日時 2023-10-17 18:22:07
合計ジャッジ時間 6,412 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
4,424 KB
testcase_01 AC 31 ms
5,320 KB
testcase_02 AC 37 ms
5,644 KB
testcase_03 AC 24 ms
4,944 KB
testcase_04 AC 33 ms
5,532 KB
testcase_05 AC 243 ms
14,820 KB
testcase_06 AC 200 ms
10,204 KB
testcase_07 AC 158 ms
8,644 KB
testcase_08 AC 123 ms
7,116 KB
testcase_09 AC 97 ms
6,372 KB
testcase_10 AC 68 ms
5,612 KB
testcase_11 AC 245 ms
14,820 KB
testcase_12 AC 146 ms
10,204 KB
testcase_13 AC 114 ms
8,644 KB
testcase_14 AC 90 ms
7,116 KB
testcase_15 AC 72 ms
6,372 KB
testcase_16 AC 54 ms
5,612 KB
testcase_17 AC 138 ms
10,204 KB
testcase_18 AC 108 ms
10,204 KB
testcase_19 AC 137 ms
10,204 KB
testcase_20 AC 51 ms
5,640 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,j,n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define LINF 1e18
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class Monoid> struct LazySegmentTree {
  using F = std::function<Monoid(Monoid, Monoid)>;

  int n;
  std::vector<Monoid> node, lazy;
  std::vector<bool> lazyFlag;

  const F f;
  const Monoid M1;

  LazySegmentTree(int sz, const F f, const Monoid M1) : f(f), M1(M1) {
    n = 1;
    while(n < sz) n *= 2;
    node.resize(2 * n - 1, M1);
    lazy.resize(2 * n - 1, 0);
    lazyFlag.resize(2 * n - 1, false);
  }

  LazySegmentTree(std::vector<Monoid> v, const F f, const Monoid M1) : f(f), M1(M1) {
    int sz = v.size();
    n = 1;
    while(n < sz) n *= 2;
    node.resize(2 * n - 1, M1);
    lazy.resize(2 * n - 1, 0);
    lazyFlag.resize(2 * n - 1, false);

    for(int i = 0; i < sz; ++i) node[i + n - 1] = v[i];
    for(int i = n - 2; i >= 0; --i) node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);
  }

  void lazyEvaluate(int k, int l, int r) {
    if(!lazyFlag[k]) return;

    node[k] = lazy[k];
    if(r - l > 1) {
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
    }
    lazyFlag[k] = false;
  }

  void update(int a, int b, Monoid x, int k = 0, int l = 0, int r = -1) {
    if(r < 0) r = n;

    lazyEvaluate(k, l, r);

    if(b <= l || r <= a) return;

    if(a <= l && r <= b) {
      lazy[k] = x;
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    }
    else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }

  Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if(r < 0) r = n;

    lazyEvaluate(k, l, r);

    if(b <= l || r <= a) return M1;
    if(a <= l && r <= b) return node[k];
    Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  map<int, int> left, right;
  rep(i, 0, n) {
    int a;
    cin >> a;
    if(!left.count(a)) {
      left[a] = i;
    }
    right[a] = i;
  }

  LazySegmentTree<int> seg(n, [](int a, int b){ return max(a, b); }, 0);
  for(auto &it : left) {
    int l = it.second, r = right[it.first];
    seg.update(l, r + 1, it.first);
  }

  rep(i, 0, n) cout << seg.query(i, i + 1) << " \n"[i == n - 1];
}
0