Submission #521497

#TimeUsernameProblemLanguageResultExecution timeMemory
521497MilosMilutinovicOsumnjičeni (COCI21_osumnjiceni)C++14
110 / 110
444 ms40544 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> l(n), r(n); vector<int> cs; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; } multiset<pair<int, int>> segs; auto Check = [&](int L, int R) { auto it = segs.lower_bound({L, -1}); if (it != segs.end()) { if (it->first <= R) { return false; } } if (it != segs.begin()) { it = prev(it); if (it->second >= L) { return false; } } return true; }; const int L = 20; vector<vector<int>> jump(n + 1, vector<int>(L, n)); int ptr = n - 1; for (int i = n - 1; i >= 0; i--) { while (!Check(l[i], r[i])) { segs.erase(segs.find({l[ptr], r[ptr]})); ptr--; } segs.insert({l[i], r[i]}); jump[i][0] = ptr; } for (int j = 1; j < L; j++) { for (int i = 0; i < n; i++) { if (jump[i][j - 1] < n) { jump[i][j] = jump[jump[i][j - 1] + 1][j - 1]; } } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; --l; --r; int ans = 0; for (int j = L - 1; j >= 0; j--) { if (jump[l][j] <= r) { l = jump[l][j] + 1; ans += 1 << j; } if (l >= n) { break; } } cout << ans + (l <= r ? 1 : 0) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...