結果

問題 No.1675 Strange Minimum Query
ユーザー milanis48663220milanis48663220
提出日時 2021-09-10 21:43:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 373 ms / 2,000 ms
コード長 2,792 bytes
コンパイル時間 1,255 ms
コンパイル使用メモリ 128,860 KB
実行使用メモリ 33,488 KB
最終ジャッジ日時 2023-09-02 16:35:18
合計ジャッジ時間 11,848 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 178 ms
13,212 KB
testcase_04 AC 178 ms
16,828 KB
testcase_05 AC 20 ms
10,660 KB
testcase_06 AC 225 ms
14,668 KB
testcase_07 AC 283 ms
18,580 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 116 ms
17,228 KB
testcase_11 AC 44 ms
11,664 KB
testcase_12 AC 133 ms
18,780 KB
testcase_13 AC 184 ms
27,732 KB
testcase_14 AC 229 ms
33,488 KB
testcase_15 AC 216 ms
33,208 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 49 ms
8,348 KB
testcase_18 AC 71 ms
9,640 KB
testcase_19 AC 111 ms
16,860 KB
testcase_20 AC 229 ms
20,864 KB
testcase_21 AC 214 ms
21,948 KB
testcase_22 AC 270 ms
25,092 KB
testcase_23 AC 191 ms
14,608 KB
testcase_24 AC 188 ms
17,720 KB
testcase_25 AC 128 ms
11,984 KB
testcase_26 AC 70 ms
11,160 KB
testcase_27 AC 191 ms
21,440 KB
testcase_28 AC 98 ms
15,088 KB
testcase_29 AC 78 ms
15,020 KB
testcase_30 AC 70 ms
10,976 KB
testcase_31 AC 250 ms
18,048 KB
testcase_32 AC 371 ms
28,808 KB
testcase_33 AC 362 ms
28,684 KB
testcase_34 AC 373 ms
28,964 KB
testcase_35 AC 312 ms
24,708 KB
testcase_36 AC 330 ms
24,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

const int INF = 2e9;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    vector<int> l(q), r(q), a(q);
    vector<vector<int>> ins(n), outs(n+1);
    for(int i = 0; i < q; i++){
        cin >> l[i] >> r[i] >> a[i]; l[i]--; r[i]--;
        ins[l[i]].push_back(a[i]);
        outs[r[i]+1].push_back(a[i]);
    }
    multiset<int> st;
    st.insert(1);
    segtree<int> sgt(n, INF, [](int a, int b){ return min(a, b); });
    for(int i = 0; i < n; i++){
        for(int x: ins[i]) st.insert(x);
        for(int x: outs[i]) st.erase(st.find(x));
        auto p = st.end();
        p--;
        sgt.update(i, *p);
    }
    for(int i = 0; i < q; i++){
        int m = sgt.query(l[i], r[i]+1);
        if(m != a[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    for(int i = 0; i < n; i++) {
        cout << sgt.query(i, i+1);
        if(i != n-1) cout << ' ';
    }
    cout << endl;
}
0