답안 #499127

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499127 2021-12-27T09:17:25 Z blue RMQ (NOI17_rmq) C++17
0 / 100
1 ms 716 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>;

#define noans {for(int i = 0; i < N; i++) cout << "-1 "; cout << "\n"; return 0;}

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, N), val_hi(N, -1);
    for(int j = 0; j < Q; j++)
    {
        val_lo[A[j]] = min(val_lo[A[j]], L[j]);
        val_hi[A[j]] = max(val_hi[A[j]], R[j]);
    }


    vi P_final(N, -1);

    set<int> currpos;

    set<int> rv;

        // cerr << "done\n";



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

        rv.insert(v);

        if(val_lo[v] == N) continue;

        rv.erase(v);


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

        if(q == currpos.end())
        {
            // cerr << "no ans 1 called\n";
            noans
        }

        if(*q > val_hi[v])
        {
            // cerr << "no ans 2 called\n";
            noans
        }

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

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

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

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

    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])
        {
            noans
        }
    }

    for(int i = 0; i < N; i++) cout << P_final[i] << ' ';
    cout << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 716 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 716 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 716 KB Output isn't correct
2 Halted 0 ms 0 KB -