답안 #426941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
426941 2021-06-14T11:04:14 Z snasibov05 RMQ (NOI17_rmq) C++14
0 / 100
1 ms 204 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>

using namespace std;

#define oo 1000000000
#define pb push_back
#define pii pair<int, int>
#define f first
#define s second

struct range{
    int l, r;
    int val;
    bool operator<(range r1) const{
        return val < r1.val;
    }
};

struct SegmentTree{
    vector<int> t;
    vector<int> lazy;
    SegmentTree(int n){
        t.resize(n * 4 + 5);
        t.assign(n * 4 + 5, -1);
        lazy.resize(n * 4 + 5);
        lazy.assign(n * 4 + 5, -1);
    }

    void push(int v){
        if (lazy[v] == -1) return;
        int k = lazy[v]; lazy[v] = -1;
        t[v*2] = max(t[v*2], k); lazy[v*2] = k;
        t[v*2+1] = max(t[v*2+1], k); lazy[v*2+1] = k;
    }

    void update(int v, int tl, int tr, int l, int r, int val){
        if (tl == l && tr == r){
            t[v] = max(t[v], val);
            lazy[v] = max(val, lazy[v]);
            return;
        }

        push(v);

        int tm = (tl + tr) / 2;
        if (l <= tm) update(v*2, tl, tm, l, min(r, tm), val);
        if (r > tm) update(v*2+1, tm+1, tr, max(l, tm+1), r, val);
        t[v] = max(t[v*2], t[v*2+1]);
    }

     int getMin(int v, int tl, int tr, int l, int r){
        if (tl == l && tr == r){
            return t[v];
        }

        push(v);

        int tm = (tl + tr) / 2;
        int res = oo;
        if (l <= tm) res = min(res, getMin(v*2, tl, tm, l, min(r, tm)));
        if (r > tm) res = min(res, getMin(v*2+1, tm+1, tr, max(l, tm+1), r));

        return res;
    }
};

int main() {
    int n, q; cin >> n >> q;
    vector<range> v(q);
    for (int i = 0; i < q; ++i) {
        cin >> v[i].l >> v[i].r >> v[i].val;
    }

    sort(v.rbegin(), v.rend());

    vector<int> ans(n, -1);
    vector<int> mn(n, -1);
    vector<pii> arr(n);

    SegmentTree tree(n);

    int k = 0;
    bool flag = true;
    for (int i = n-1; i >= 0; --i) {

        int l = 0, r = n-1;
        int x = n-1, y = 0;

        while (k < q && v[k].val == i){
            l = max(v[k].l, l); x = min(x, v[k].l);
            r = min(v[k].r, r); y = max(y, v[k].r);
            k++;
        }

        if (l > r) flag = false;

        if (x <= y) tree.update(1, 0, n-1, x, y, i);
        arr[i] = {l, r};
    }

    vector<vector<int>> a(n);
    set<int> st;
    for (int i = 0; i < n; ++i) {
        int x = tree.getMin(1, 0, n-1, i, i);

        if (x == -1) st.insert(i);
        else a[x].pb(i);
    }

    for (int i = 0; i < n && flag; ++i) {
        for (auto x : a[i]) st.insert(x);
        auto it = st.lower_bound(arr[i].f);
        if (it == st.end()){
            flag = false;
            break;
        }
        int pos = *it;
        st.erase(pos);
        ans[pos] = i;
    }

    if (flag) for (auto x : ans) cout << x << " ";
    else for (int i = 0; i < n; ++i) cout << "-1 ";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -