Submission #798568

#TimeUsernameProblemLanguageResultExecution timeMemory
798568HunterXDRMQ (NOI17_rmq)C++17
100 / 100
103 ms12676 KiB
#include <bits/stdc++.h> using namespace std; typedef int ll; typedef vector<int> vl; typedef vector<vl> vvl; #define all(x) x.begin(), x.end() #define pb push_back #define f first #define s second const char nd = '\n'; const int infi = 1e9; struct queries { ll l, r, mini; }; struct nodo { ll l, r; ll mini; nodo *left, *right; }; nodo *st; void start(nodo *yo, vl &a) { if (yo->l == yo->r) { yo->mini = a[yo->l]; return; } ll mid = (yo->l + yo->r) / 2; yo->left = new nodo({yo->l, mid, 0, NULL, NULL}); yo->right = new nodo({mid + 1, yo->r, 0, NULL, NULL}); start(yo->left, a); start(yo->right, a); yo->mini = min(yo->left->mini, yo->right->mini); } ll query(nodo *c, ll l, ll r) { if (r < c->l || c->r < l) return infi; if (l <= c->l && c->r <= r) return c->mini; return min(query(c->left, l, r), query(c->right, l, r)); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; vector<queries> que; vector<pair<int, int> > might(n, make_pair(-1, -1)), need(n, make_pair(-1, -1)); vl res(n, -1); ll l, r, mini; while (q--) { cin >> l >> r >> mini; que.pb({l, r, mini}); if (need[mini].f == -1) { need[mini] = make_pair(l, r); } else { need[mini].f = max(need[mini].f, l); need[mini].s = min(need[mini].s, r); } if (might[mini].f == -1) { might[mini] = make_pair(l, r); } else { might[mini].f = min(might[mini].f, l); might[mini].s = max(might[mini].s, r); } } set<ll> have, marked; for (ll i = 0; i < n; i++) have.insert(i), marked.insert(i); set<ll>::iterator it, it2; for (ll i = n - 1; i >= 0; i--) { if (need[i].f == -1) continue; it = marked.lower_bound(need[i].f); if (need[i].f > need[i].s || it == marked.end() || *it > need[i].s) { for (auto v : res) cout << -1 << " "; cout << nd; return 0; } res[*it] = i; marked.erase(it); have.erase(i); while (true) { it = marked.lower_bound(might[i].f); if (it == marked.end() || *it > might[i].s) break; it2 = have.lower_bound(i); if (it2 == have.end()) { for (auto v : res) cout << -1 << " "; cout << nd; return 0; } res[*it] = *it2; marked.erase(it); have.erase(it2); } } for (auto &v : res) { if (v == -1) { v = *(have.begin()); have.erase(have.begin()); } } // for (auto v : res) cout << v << " "; // cout << nd; nodo *st = new nodo({0, n - 1, 0, NULL, NULL}); start(st, res); for (auto v : que) { if (query(st, v.l, v.r) != v.mini) { for (auto v : res) cout << -1 << " "; cout << nd; return 0; } } for (auto v : res) cout << v << " "; cout << nd; }

Compilation message (stderr)

rmq.cpp: In function 'int main()':
rmq.cpp:93:17: warning: unused variable 'v' [-Wunused-variable]
   93 |       for (auto v : res) cout << -1 << " ";
      |                 ^
rmq.cpp:108:19: warning: unused variable 'v' [-Wunused-variable]
  108 |         for (auto v : res) cout << -1 << " ";
      |                   ^
rmq.cpp:134:17: warning: unused variable 'v' [-Wunused-variable]
  134 |       for (auto v : res) cout << -1 << " ";
      |                 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...