Submission #499134

# Submission time Handle Problem Language Result Execution time Memory
499134 2021-12-27T09:30:54 Z blue RMQ (NOI17_rmq) C++17
100 / 100
105 ms 26928 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

const int lg = 16;
const int mx = 100'000;

using vi = vector<int>;
using vvi = vector<vi>;

int st[mx][1+lg];
vi maxpow(1+mx);

int rangemin(int l, int r)
{
    return min(st[l][maxpow[r-l+1]], st[r - (1 << maxpow[r-l+1]) + 1][maxpow[r-l+1]]);
}

vi P;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    maxpow[1] = 0;
    for(int i = 2; i <= mx; i++)
    {
        maxpow[i] = maxpow[i-1];
        if((1 << (maxpow[i]+1)) == i)
            maxpow[i]++;
    }

    int N, Q;
    cin >> N >> Q;

    vi le[N], re[N];

    int L[Q], R[Q], A[Q];
    for(int j = 0; j < Q; j++)
    {
        cin >> L[j] >> R[j] >> A[j];
        le[L[j]].push_back(j);
        re[R[j]].push_back(j);
    }

    multiset<int> V{0};

    P = vi(N);

    for(int i = 0; i < N; i++)
    {
        for(int j: le[i])
            V.insert(A[j]);

        P[i] = *V.rbegin();

        for(int j: re[i])
            V.erase(V.find(A[j]));
    }

    vvi P_pos(N);
    for(int i = 0; i < N; i++) P_pos[P[i]].push_back(i);

    vi val_lo(N, -1), val_hi(N, N);
    for(int j = 0; j < Q; j++)
    {
        val_lo[A[j]] = max(val_lo[A[j]], L[j]);
        val_hi[A[j]] = min(val_hi[A[j]], R[j]);
    }


    vi P_final(N, -1);

    set<int> currpos;

    set<int> rv;

        // cerr << "done\n";

    // for(int i = 0; i < N; i++) cerr << P[i] << ' ';
    // cerr << '\n';



    for(int v = 0; v < N; v++)
    {
        // cerr << "current P final = ";
        // for(int i = 0; i < N; i++) cerr << P_final[i] << ' ';
        // cerr << "\n\n";
        // cerr << "v = " << v << '\n';
        for(int p: P_pos[v]) currpos.insert(p);

        // cerr << "currpos = ";
        // for(int c: currpos) cerr << c << ' ';
        // cerr << "\n";

        rv.insert(v);

        if(val_lo[v] == -1) continue;

        rv.erase(v);


        auto q = currpos.lower_bound(val_lo[v]);

        if(q == currpos.end())
        {
            // cerr << "no ans 1 called\n";
            for(int i = 0; i < N; i++) cout << "-1 ";
            cout << '\n';
            return 0;
        }

        if(*q > val_hi[v])
        {
            // cerr << "no ans 2 called\n";
            for(int i = 0; i < N; i++) cout << "-1 ";
            cout << '\n';
            return 0;
        }

        P_final[*q] = v;
        currpos.erase(q);
    }

    vi rem_values;
    for(int t: rv) rem_values.push_back(t);

    // cerr << "rem values: ";
    // for(int t: rem_values) cerr << t << ' ';
    // cerr << '\n';

    vi rem_pos;
    for(int i = 0; i < N; i++)
        if(P_final[i] == -1)
            rem_pos.push_back(i);

    // cerr << "done\n";


    sort(rem_pos.begin(), rem_pos.end(), [] (int u, int v)
    {
        return P[u] < P[v];
    });

    // cerr << "rem pos = ";
    // for(int t: rem_pos) cerr << t << ' ';
    // cerr << '\n';

    int z = int(rem_values.size());

    for(int i = 0; i < z; i++)
    {
        P_final[rem_pos[i]] = rem_values[i];
    }



    for(int i = 0; i < N; i++)
        st[i][0] = P_final[i];

    for(int e = 1; e <= lg; e++)
        for(int i = 0; i+(1<<e)-1 < N; i++)
            st[i][e] = min(st[i][e-1], st[i + (1 << (e-1))][e-1]);

    for(int j = 0; j < Q; j++)
    {
        if(rangemin(L[j], R[j]) != A[j])
        {
            for(int i = 0; i < N; i++) cout << "-1 ";
            cout << '\n';
            return 0;
        }
    }

    for(int i = 0; i < N; i++) cout << P_final[i] << ' ';
    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 2 ms 844 KB Output is correct
13 Correct 2 ms 972 KB Output is correct
14 Correct 1 ms 844 KB Output is correct
15 Correct 2 ms 972 KB Output is correct
16 Correct 1 ms 972 KB Output is correct
17 Correct 2 ms 972 KB Output is correct
18 Correct 1 ms 972 KB Output is correct
19 Correct 1 ms 844 KB Output is correct
20 Correct 1 ms 844 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 2 ms 844 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 2 ms 844 KB Output is correct
13 Correct 2 ms 972 KB Output is correct
14 Correct 1 ms 844 KB Output is correct
15 Correct 2 ms 972 KB Output is correct
16 Correct 1 ms 972 KB Output is correct
17 Correct 2 ms 972 KB Output is correct
18 Correct 1 ms 972 KB Output is correct
19 Correct 1 ms 844 KB Output is correct
20 Correct 1 ms 844 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 2 ms 844 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 102 ms 22820 KB Output is correct
28 Correct 101 ms 25568 KB Output is correct
29 Correct 86 ms 24384 KB Output is correct
30 Correct 105 ms 26928 KB Output is correct
31 Correct 81 ms 20692 KB Output is correct
32 Correct 88 ms 19364 KB Output is correct
33 Correct 48 ms 22132 KB Output is correct
34 Correct 32 ms 15528 KB Output is correct
35 Correct 59 ms 26020 KB Output is correct
36 Correct 51 ms 12848 KB Output is correct
37 Correct 79 ms 13428 KB Output is correct
38 Correct 10 ms 8016 KB Output is correct
39 Correct 64 ms 12332 KB Output is correct
40 Correct 1 ms 716 KB Output is correct
41 Correct 1 ms 716 KB Output is correct