結果

問題 No.1675 Strange Minimum Query
ユーザー FukucchiFukucchi
提出日時 2021-09-10 21:53:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 16,473 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 138,736 KB
実行使用メモリ 50,564 KB
最終ジャッジ日時 2023-09-02 17:03:02
合計ジャッジ時間 14,227 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
45,648 KB
testcase_01 AC 26 ms
45,724 KB
testcase_02 AC 26 ms
45,668 KB
testcase_03 AC 99 ms
49,620 KB
testcase_04 AC 82 ms
48,848 KB
testcase_05 AC 31 ms
45,984 KB
testcase_06 AC 114 ms
50,156 KB
testcase_07 AC 97 ms
50,424 KB
testcase_08 AC 26 ms
45,680 KB
testcase_09 AC 28 ms
45,708 KB
testcase_10 AC 220 ms
47,376 KB
testcase_11 AC 88 ms
45,816 KB
testcase_12 AC 259 ms
47,560 KB
testcase_13 AC 264 ms
50,432 KB
testcase_14 AC 390 ms
50,432 KB
testcase_15 AC 310 ms
50,424 KB
testcase_16 AC 26 ms
45,672 KB
testcase_17 AC 84 ms
46,144 KB
testcase_18 AC 101 ms
46,656 KB
testcase_19 AC 169 ms
46,508 KB
testcase_20 AC 255 ms
48,552 KB
testcase_21 AC 256 ms
48,024 KB
testcase_22 AC 320 ms
49,092 KB
testcase_23 AC 206 ms
48,836 KB
testcase_24 AC 216 ms
48,524 KB
testcase_25 AC 149 ms
47,888 KB
testcase_26 AC 110 ms
46,468 KB
testcase_27 AC 239 ms
47,764 KB
testcase_28 AC 148 ms
46,444 KB
testcase_29 AC 139 ms
45,996 KB
testcase_30 AC 106 ms
46,496 KB
testcase_31 AC 247 ms
49,896 KB
testcase_32 AC 386 ms
50,464 KB
testcase_33 AC 382 ms
50,436 KB
testcase_34 AC 387 ms
50,420 KB
testcase_35 AC 440 ms
50,352 KB
testcase_36 AC 440 ms
50,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region  header */
#pragma GCC optimize("O3") //コンパイラ最適化用

#ifdef LOCAL
#define _GLIBCXX_DEBUG //配列に[]でアクセス時のエラー表示
#endif
#define _USE_MATH_DEFINES
#include <algorithm> //sort,二分探索,など
#include <bitset>    //固定長bit集合
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <cassert> //assert(p)で,not pのときにエラー
#include <cctype>
#include <chrono> //実行時間計測
#include <climits>
#include <cmath>   //pow,logなど
#include <complex> //複素数
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional> //sortのgreater
#include <iomanip>    //setprecision(浮動小数点の出力の誤差)
#include <ios>        // std::left, std::right
#include <iostream>   //入出力
#include <iterator>   //集合演算(積集合,和集合,差集合など)
#include <map>
#include <numeric> //iota(整数列の生成),gcdとlcm,accumulate
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;

#define ALL(x) x.begin(), x.end()
const long long INF = numeric_limits<long long>::max() / 4;
const int MOD = 1e9 + 7;
// const int MOD=998244353;
//略記
#define FF first
#define SS second
#define int long long
#define stoi stoll
#define LD long double
#define PII pair<int, int>
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(x) (int)((x).size())
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define VI vector<int>
#define VVI vector<vector<int>>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n) - (int)1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(b) - (int)1; i >= (int)a; i--)

const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

int in() {
    int x;
    cin >> x;
    return x;
}
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed#c%E3%81%A7%E3%81%AE%E5%AE%9F%E8%A3%85
void print() { cout << "\n"; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
    for (auto &a : vec) {
        cout << a;
        if (&a != &vec.back())
            cout << " ";
    }
    cout << "\n";
}
template <class T> void print(set<T> &set) {
    for (auto &a : set) {
        cout << a << " ";
    }
    cout << "\n";
}
template <class T> void print(vector<vector<T>> &df) {
    for (auto &vec : df) {
        print(vec);
    }
}

// debug macro
// https://atcoder.jp/contests/abc202/submissions/22815715
namespace debugger {
template <class T> void view(const std::vector<T> &a) {
    std::cerr << "{ ";
    for (const auto &v : a) {
        std::cerr << v << ", ";
    }
    std::cerr << "\b\b }";
}
template <class T> void view(const std::vector<std::vector<T>> &a) {
    std::cerr << "{\n";
    for (const auto &v : a) {
        std::cerr << "\t";
        view(v);
        std::cerr << "\n";
    }
    std::cerr << "}";
}
template <class T, class U> void view(const std::vector<std::pair<T, U>> &a) {
    std::cerr << "{\n";
    for (const auto &p : a)
        std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
    std::cerr << "}";
}
template <class T, class U> void view(const std::map<T, U> &m) {
    std::cerr << "{\n";
    for (const auto &p : m)
        std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
    std::cerr << "}";
}
template <class T, class U> void view(const std::pair<T, U> &p) {
    std::cerr << "(" << p.first << ", " << p.second << ")";
}
template <class T> void view(const std::set<T> &s) {
    std::cerr << "{ ";
    for (auto &v : s) {
        view(v);
        std::cerr << ", ";
    }
    std::cerr << "\b\b }";
}

template <class T> void view(const T &e) { std::cerr << e; }
} // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
    debugger::view(H);
    std::cerr << ", ";
    debug_out(T...);
}
#define debug(...)                                                             \
    do {                                                                       \
        std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : [";              \
        debug_out(__VA_ARGS__);                                                \
        std::cerr << "\b\b]\n";                                                \
    } while (false)
#else
#define debug(...) (void(0))
#endif

long long power(long long x, long long n) {
    // O(logn)
    // https://algo-logic.info/calc-pow/#toc_id_1_2
    long long ret = 1;
    while (n > 0) {
        if (n & 1)
            ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける
        x *= x;
        n >>= 1; // n を1bit 左にずらす
    }
    return ret;
}
// @brief nCr. O(r log n)。あるいは前処理 O(n), 本処理 O(1)で求められる modint
// の bc を検討。
int comb(int n, int r) {
    // https://www.geeksforgeeks.org/program-to-calculate-the-value-of-ncr-efficiently/
    if (n < r)
        return 0;

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            // gcd of p, k
            long long m = __gcd(p, k);

            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
    return p;
}

// MOD
void add(long long &a, long long b) {
    a += b;
    if (a >= MOD)
        a -= MOD;
}
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// 負数も含む丸め
long long ceildiv(long long a, long long b) {
    if (b < 0)
        a = -a, b = -b;
    if (a >= 0)
        return (a + b - 1) / b;
    else
        return a / b;
}
long long floordiv(long long a, long long b) {
    if (b < 0)
        a = -a, b = -b;
    if (a >= 0)
        return a / b;
    else
        return (a - b + 1) / b;
}
long long floorsqrt(long long x) {
    assert(x >= 0);
    long long ok = 0;
    long long ng = 1;
    while (ng * ng <= x)
        ng <<= 1;
    while (ng - ok > 1) {
        long long mid = (ng + ok) >> 1;
        if (mid * mid <= x)
            ok = mid;
        else
            ng = mid;
    }
    return ok;
}

// @brief a^n mod mod
long long modpower(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// @brief s が c を含むか
template <class T> bool contain(const std::string &s, const T &c) {
    return s.find(c) != std::string::npos;
}

__attribute__((constructor)) void faster_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cerr.tie(nullptr);
}
/* #endregion */

template <class T> struct SegmentTreeBeats {
  private:
    size_t n, n_;
    const T MAX = std::numeric_limits<T>::max();
    const T MIN = -MAX;
    std::vector<T> max_v, min_v, smax_v, smin_v, lz_upd, lz_ad, sigm, len;
    std::vector<int> max_c, min_c;

    void init(std::vector<T> &v) {
        n_ = v.size(), n = 1;
        while (n < n_)
            n *= 2;
        max_v.assign(n * 2 - 1, MIN);
        min_v.assign(n * 2 - 1, MAX);
        smax_v.assign(n * 2 - 1, MIN);
        smin_v.assign(n * 2 - 1, MAX);
        lz_upd.assign(n * 2 - 1, MAX);
        lz_ad.assign(n * 2 - 1, 0);
        len.assign(n * 2 - 1, n);
        sigm.assign(n * 2 - 1, 0);
        max_c.assign(n * 2 - 1, 1);
        min_c.assign(n * 2 - 1, 1);

        for (int i = 0; i < n - 1; i++)
            len[i * 2 + 1] = len[i * 2 + 2] = (len[i] >> 1);

        for (int i = 0; i < n_; i++)
            max_v[i + n - 1] = min_v[i + n - 1] = sigm[i + n - 1] = v[i];

        for (int i = n - 2; i >= 0; i--)
            proc(i);
    }

    void proc(int i) {
        sigm[i] = sigm[i * 2 + 1] + sigm[i * 2 + 2];

        max_v[i] = std::max(max_v[i * 2 + 1], max_v[i * 2 + 2]);

        if (max_v[i * 2 + 1] > max_v[i * 2 + 2]) {
            max_c[i] = max_c[i * 2 + 1];
            smax_v[i] = std::max(smax_v[i * 2 + 1], max_v[i * 2 + 2]);
        }

        if (max_v[i * 2 + 1] < max_v[i * 2 + 2]) {
            max_c[i] = max_c[i * 2 + 2];
            smax_v[i] = std::max(max_v[i * 2 + 1], smax_v[i * 2 + 2]);
        }

        if (max_v[i * 2 + 1] == max_v[i * 2 + 2]) {
            max_c[i] = max_c[i * 2 + 1] + max_c[i * 2 + 2];
            smax_v[i] = std::max(smax_v[i * 2 + 1], smax_v[i * 2 + 2]);
        }

        min_v[i] = std::min(min_v[i * 2 + 1], min_v[i * 2 + 2]);

        if (min_v[i * 2 + 1] < min_v[i * 2 + 2]) {
            min_c[i] = min_c[i * 2 + 1];
            smin_v[i] = std::min(smin_v[i * 2 + 1], min_v[i * 2 + 2]);
        }

        if (min_v[i * 2 + 1] > min_v[i * 2 + 2]) {
            min_c[i] = min_c[i * 2 + 2];
            smin_v[i] = std::min(min_v[i * 2 + 1], smin_v[i * 2 + 2]);
        }

        if (min_v[i * 2 + 1] == min_v[i * 2 + 2]) {
            min_c[i] = min_c[i * 2 + 1] + min_c[i * 2 + 2];
            smin_v[i] = std::min(smin_v[i * 2 + 1], smin_v[i * 2 + 2]);
        }
    }

    void eval(int i) {
        if (lz_upd[i] != MAX) {
            in_update(i * 2 + 1, lz_upd[i]);
            in_update(i * 2 + 2, lz_upd[i]);
            lz_upd[i] = MAX;
            return;
        }

        if (lz_ad[i]) {
            in_add(i * 2 + 1, lz_ad[i]);
            in_add(i * 2 + 2, lz_ad[i]);
            lz_ad[i] = 0;
        }

        if (max_v[i] < max_v[i * 2 + 1])
            in_chmin(i * 2 + 1, max_v[i]);
        if (max_v[i] < max_v[i * 2 + 2])
            in_chmin(i * 2 + 2, max_v[i]);

        if (min_v[i] > min_v[i * 2 + 1])
            in_chmax(i * 2 + 1, min_v[i]);
        if (min_v[i] > min_v[i * 2 + 2])
            in_chmax(i * 2 + 2, min_v[i]);
    }

    void in_update(int i, T x) {
        max_v[i] = min_v[i] = x;
        smax_v[i] = MIN, smin_v[i] = MAX;
        max_c[i] = min_c[i] = len[i];
        sigm[i] = len[i] * x;
        lz_upd[i] = x, lz_ad[i] = 0;
    }

    void in_chmin(int i, T x) {
        sigm[i] += (x - max_v[i]) * max_c[i];

        if (max_v[i] == min_v[i])
            max_v[i] = min_v[i] = x;

        else if (max_v[i] == smin_v[i])
            max_v[i] = smin_v[i] = x;

        else
            max_v[i] = x;

        if (lz_upd[i] != MAX and lz_upd[i] > x)
            lz_upd[i] = x;
    }

    void in_chmax(int i, T x) {
        sigm[i] += (x - min_v[i]) * min_c[i];

        if (min_v[i] == max_v[i])
            min_v[i] = max_v[i] = x;

        else if (min_v[i] == smax_v[i])
            min_v[i] = smax_v[i] = x;

        else
            min_v[i] = x;

        if (lz_upd[i] != MAX and lz_upd[i] < x)
            lz_upd[i] = x;
    }

    void in_add(int i, T x) {
        max_v[i] += x, min_v[i] += x;

        if (smin_v[i] != MAX)
            smin_v[i] += x;
        if (smax_v[i] != MIN)
            smax_v[i] += x;

        sigm[i] += x * len[i];

        if (lz_upd[i] != MAX)
            lz_upd[i] += x;
        lz_ad[i] += x;
    }

    void chmin(int i, int a, int b, int l, int r, T x) {
        if (b <= l or r <= a or x >= max_v[i])
            return;

        if (a <= l and r <= b and smax_v[i] < x) {
            in_chmin(i, x);
            return;
        }

        eval(i);
        chmin(i * 2 + 1, a, b, l, (l + r) >> 1, x);
        chmin(i * 2 + 2, a, b, (l + r) >> 1, r, x);
        proc(i);
    }

    void chmax(int i, int a, int b, int l, int r, T x) {
        if (b <= l or r <= a or x <= min_v[i])
            return;

        if (a <= l and r <= b and smin_v[i] > x) {
            in_chmax(i, x);
            return;
        }

        eval(i);
        chmax(i * 2 + 1, a, b, l, (l + r) >> 1, x);
        chmax(i * 2 + 2, a, b, (l + r) >> 1, r, x);
        proc(i);
    }

    void add(int i, int a, int b, int l, int r, T x) {
        if (b <= l or r <= a)
            return;

        if (a <= l and r <= b) {
            in_add(i, x);
            return;
        }

        eval(i);
        add(i * 2 + 1, a, b, l, (l + r) >> 1, x);
        add(i * 2 + 2, a, b, (l + r) >> 1, r, x);
        proc(i);
    }

    void update(int i, int a, int b, int l, int r, T x) {
        if (b <= l or r <= a)
            return;

        if (a <= l and r <= b) {
            in_update(i, x);
            return;
        }

        eval(i);
        update(i * 2 + 1, a, b, l, (l + r) >> 1, x);
        update(i * 2 + 2, a, b, (l + r) >> 1, r, x);
        proc(i);
    }

    T sum(int i, int a, int b, int l, int r) {
        if (r <= a || b <= l)
            return 0;

        if (a <= l && r <= b)
            return sigm[i];

        eval(i);
        T lv = sum(i * 2 + 1, a, b, l, (l + r) / 2);
        T rv = sum(i * 2 + 2, a, b, (l + r) / 2, r);
        return lv + rv;
    }

    T rangemin(int i, int a, int b, int l, int r) {
        if (r <= a or b <= l)
            return MAX;

        if (a <= l and r <= b)
            return min_v[i];

        eval(i);
        T lv = rangemin(i * 2 + 1, a, b, l, (l + r) >> 1);
        T rv = rangemin(i * 2 + 2, a, b, (l + r) >> 1, r);
        return std::min(lv, rv);
    }

    T rangemax(int i, int a, int b, int l, int r) {
        if (r <= a or b <= l)
            return MIN;

        if (a <= l and r <= b)
            return max_v[i];

        eval(i);
        T lv = rangemax(i * 2 + 1, a, b, l, (l + r) >> 1);
        T rv = rangemax(i * 2 + 2, a, b, (l + r) >> 1, r);
        return std::max(lv, rv);
    }

    T rangesum(int i, int a, int b, int l, int r) {
        if (r <= a or b <= l)
            return 0;

        if (a <= l and r <= b)
            return sigm[i];

        eval(i);
        T lv = rangesum(i * 2 + 1, a, b, l, (l + r) >> 1);
        T rv = rangesum(i * 2 + 2, a, b, (l + r) >> 1, r);
        return lv + rv;
    }

  public:
    SegmentTreeBeats() = default;

    SegmentTreeBeats(std::vector<T> &v) { init(v); }

    SegmentTreeBeats(size_t n, T init_data = 0) {
        std::vector<T> v(n, init_data);
        init(v);
    }

    T operator[](int i) { return query_min(i, i + 1); }

    void update_chmin(int a, int b, T x) { chmin(0, a, b, 0, n, x); }

    void update_chmax(int a, int b, T x) { chmax(0, a, b, 0, n, x); }

    void update_add(int a, int b, T x) { add(0, a, b, 0, n, x); }

    void update_upd(int a, int b, T x) { update(0, a, b, 0, n, x); }

    T query_min(int a, int b) { return rangemin(0, a, b, 0, n); }

    T query_max(int a, int b) { return rangemax(0, a, b, 0, n); }

    T query_sum(int a, int b) { return rangesum(0, a, b, 0, n); }
};

// https://atcoder.jp/contests/abc196/submissions/21082182
//
int n, q;
signed main() {
    cin >> n >> q;
    VI L(q), R(q), B(q);
    for (int i = 0; i < q; ++i) {
        cin >> L[i] >> R[i] >> B[i];
        L[i]--, R[i]--;
    }
    int MAX = 2e5 + 200;
    VI X(MAX, -1);
    SegmentTreeBeats<int> Beats(X); // X で初期化
    REP(qi, q) { Beats.update_chmax(L[qi], R[qi] + 1, B[qi]); }
    bool ng = false;
    REP(qi, q) {
        if (Beats.query_min(L[qi], R[qi] + 1) > B[qi]) {
            ng = true;
            break;
        }
    }
    if (ng) {
        print(-1);
    } else {
        REP(ni, n) {
            if (Beats[ni] == -1) {
                cout << 1000000000;
            } else {
                cout << Beats[ni];
            }
            if (ni != n - 1)
                cout << " ";
        }
        cout << "\n";
    }
}
0