結果

問題 No.1675 Strange Minimum Query
ユーザー 👑 emthrmemthrm
提出日時 2021-09-10 21:28:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 2,561 bytes
コンパイル時間 2,589 ms
コンパイル使用メモリ 210,640 KB
最終ジャッジ日時 2025-01-24 09:40:53
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Band>
struct SparseTable {
  using Fn = std::function<Band(Band, Band)>;

  SparseTable() {}

  SparseTable(const std::vector<Band> &a, Fn fn) { init(a, fn); }

  void init(const std::vector<Band> &a, Fn fn_) {
    is_built = true;
    fn = fn_;
    int n = a.size(), table_h = 0;
    lg.assign(n + 1, 0);
    for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
    while ((1 << table_h) <= n) ++table_h;
    dat.assign(table_h, std::vector<Band>(n));
    for (int j = 0; j < n; ++j) dat[0][j] = a[j];
    for (int i = 1; i < table_h; ++i) for (int j = 0; j + (1 << i) <= n; ++j) {
      dat[i][j] = fn(dat[i - 1][j], dat[i - 1][j + (1 << (i - 1))]);
    }
  }

  Band query(int left, int right) const {
    assert(is_built && left < right);
    int h = lg[right - left];
    return fn(dat[h][left], dat[h][right - (1 << h)]);
  }

private:
  bool is_built = false;
  Fn fn;
  std::vector<int> lg;
  std::vector<std::vector<Band>> dat;
};

int main() {
  int n, q; cin >> n >> q;
  vector<int> l(q), r(q), b(q); REP(i, q) cin >> l[i] >> r[i] >> b[i], --l[i], --r[i];
  vector<int> ord(q);
  iota(ALL(ord), 0);
  sort(ALL(ord), [&](int l, int r) -> bool { return b[l] > b[r]; });
  vector<int> a(n, 1);
  set<int> s;
  REP(i, n) s.emplace(i);
  for (int i : ord) {
    for (auto it = s.lower_bound(l[i]); it != s.end() && *it <= r[i]; it = s.erase(it)) {
      a[*it] = b[i];
    }
  }
  SparseTable<int> sparse_table(a, [](int a, int b) -> int { return min(a, b); });
  REP(i, q) {
    if (sparse_table.query(l[i], r[i] + 1) != b[i]) {
      cout << "-1\n";
      return 0;
    }
  }
  REP(i, n) cout << a[i] << " \n"[i + 1 == n];
  return 0;
}
0