답안 #905175

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
905175 2024-01-12T17:08:39 Z LOLOLO RMQ (NOI17_rmq) C++17
100 / 100
243 ms 12248 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 1e5 + 100;
vector <pair <int, int> > save[N];
int ans[N], seg[N * 4];

void upd(int id, int l, int r, int u, int v, int s) {
    if (l > v || r < u)
        return;

    if (l >= u && r <= v) {
        seg[id] = max(seg[id], s);
        return;
    }

    int m = (l + r) / 2;
    upd(id * 2, l, m, u, v, s);
    upd(id * 2 + 1, m + 1, r, u, v, s);
}

int get(int id, int l, int r, int p) {
    if (l > p || r < p)
        return 0;

    int ans = seg[id];
    if (l == r)
        return ans;

    int m = (l + r) / 2;
    return max({ans, get(id * 2, l, m, p), get(id * 2 + 1, m + 1, r, p)});
}

int n, q;
bool cmp(int a, int b) {
    return get(1, 0, n - 1, a) < get(1, 0, n - 1, b);
}

void out() {
    for (int i = 0; i < n; i++)
        cout << -1 << " ";

    exit(0);
}

int main() {
    mem(ans, -1);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> q;

    for (int i = 1; i <= q; i++) {
        int l, r, v;
        cin >> l >> r >> v;
        upd(1, 0, n - 1, l, r, v);
        save[v].pb(make_pair(l, r));
    }

    set <int> on;
    for (int i = 0; i < n; i++) {
        on.insert(i);
    }

    vector <int> need, val;

    for (int i = n - 1; i >= 0; i--) {
        if (save[i].empty()) {
            val.pb(i);
            continue;
        }

        int l = 0, r = n - 1;
        for (auto x : save[i]) {
            l = max(l, x.f);
            r = min(r, x.s);
        }

        auto it = on.lower_bound(l);
        if (it != on.end() && *it <= r) {
            ans[*it] = i;
            on.erase(*it);
        } else {
            out();
        }

        for (auto x : save[i]) {
            int l = x.f, r = x.s;
            auto it = on.lower_bound(l);
            vector <int> add;
            while (it != on.end() && *it <= r) {
                add.pb(*it);
                it = next(it);
            }

            for (auto e : add) {
                on.erase(e);
                need.pb(e);
            }
        }
    }

    while (sz(on)) {
        need.pb(*(on.begin()));
        on.erase(*(on.begin()));
    }

    sort(all(need), cmp);
    sort(all(val));

    if (sz(val) != sz(need))
        out();

    for (int i = 0; i < sz(val); i++) {
        ans[need[i]] = val[i];
    }

    for (int i = 0; i < n; i++) {
        if (ans[i] < get(1, 0, n - 1, i)) {
            out();
        }
    }

    for (int i = 0; i < n; i++)
        cout << ans[i] << " ";

    cout << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4440 KB Output is correct
13 Correct 3 ms 4444 KB Output is correct
14 Correct 3 ms 4444 KB Output is correct
15 Correct 3 ms 4444 KB Output is correct
16 Correct 3 ms 4696 KB Output is correct
17 Correct 3 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4440 KB Output is correct
13 Correct 3 ms 4444 KB Output is correct
14 Correct 3 ms 4444 KB Output is correct
15 Correct 3 ms 4444 KB Output is correct
16 Correct 3 ms 4696 KB Output is correct
17 Correct 3 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 200 ms 11124 KB Output is correct
28 Correct 231 ms 11340 KB Output is correct
29 Correct 212 ms 10792 KB Output is correct
30 Correct 239 ms 12116 KB Output is correct
31 Correct 171 ms 10260 KB Output is correct
32 Correct 166 ms 10220 KB Output is correct
33 Correct 201 ms 9172 KB Output is correct
34 Correct 125 ms 7972 KB Output is correct
35 Correct 243 ms 10812 KB Output is correct
36 Correct 55 ms 11676 KB Output is correct
37 Correct 65 ms 12248 KB Output is correct
38 Correct 19 ms 8028 KB Output is correct
39 Correct 55 ms 11064 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct