Submission #499134

#TimeUsernameProblemLanguageResultExecution timeMemory
499134blueRMQ (NOI17_rmq)C++17
100 / 100
105 ms26928 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...