Submission #501930

#TimeUsernameProblemLanguageResultExecution timeMemory
501930nightyHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
77 / 100
3035 ms38632 KiB
#include <bits/stdc++.h> using namespace std; int calcMaxInv(vector<int>& a, int l, int r) { int maxVal = 0, res = 0; for (int i = l; i <= r; ++i) { if (maxVal > a[i]) { res = max(res, maxVal + a[i]); } maxVal = max(maxVal, a[i]); } return res; } const int K = 470; const int N = 1e6; vector<int> s[N / K + 10]; int maxInverse[N / K + 10]; int calcMaxInv2(vector<int>& a, int l, int r) { int res = 0, maxVal = 0; if (l % K != 0) { res = calcMaxInv(a, l, min(r, l - l % K + K - 1)); maxVal = *max_element(a.begin() + l, a.begin() + l - l % K + K - 1 + 1); l = l - l % K + K; } while (l + K - 1 <= r) { res = max(res, maxInverse[l / K]); auto it = lower_bound(s[l / K].begin(), s[l / K].end(), maxVal); if (it != s[l / K].begin()) { --it; res = max(res, *it + maxVal); } maxVal = max(maxVal, *s[l / K].rbegin()); l += K; } if (l <= r) { for (int i = l; i <= r; ++i) { if (a[i] < maxVal) { res = max(res, a[i] + maxVal); } } res = max(res, calcMaxInv(a, l, r)); } return res; } const int INF = 2e9; struct segtree { int n; struct node { int min, max; bool isSorted; friend node combine(node a, node b) { if (!a.isSorted || !b.isSorted) { return {std::min(a.min, b.min), std::max(a.max, b.max), false}; } if (a.max <= b.min) { return {std::min(a.min, b.min), std::max(a.max, b.max), true}; } return {std::min(a.min, b.min), std::max(a.max, b.max), false}; } }; vector<node> tree; void build(vector<int>& a) { n = 1; while (n < a.size()) { n *= 2; } tree.assign(n * 2, {INF, -INF, true}); for (int i = 0; i < a.size(); ++i) { tree[n - 1 + i] = {a[i], a[i], true}; } for (int i = n - 2; i >= 0; --i) { tree[i] = combine(tree[2 * i + 1], tree[2 * i + 2]); } } node get(int l, int r, int x, int lx, int rx) { if (rx < l || r < lx) { return {INF, -INF, true}; } if (l <= lx && rx <= r) { return tree[x]; } node a = get(l, r, x * 2 + 1, lx, (lx + rx) / 2); node b = get(l, r, x * 2 + 2, (lx + rx) / 2 + 1, rx); return combine(a, b); } node get(int l, int r) { return get(l, r, 0, 0, n - 1); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n); for (auto& i : a) { cin >> i; } for (int i = 0; i < n; ++i) { s[i / K].push_back(a[i]); } for (auto& i : s) { sort(i.begin(), i.end()); } for (int i = 0; (i + 1) * K - 1 < n; ++i) { maxInverse[i] = calcMaxInv(a, i * K, (i + 1) * K - 1); } int minVal = *min_element(a.begin(), a.end()); segtree st; st.build(a); while (m--) { int l, r, k; cin >> l >> r >> k; --l, --r; if (k < minVal) { cout << st.get(l, r).isSorted << '\n'; // cout << is_sorted(a.begin() + l, a.begin() + r + 1) << '\n'; } else { cout << (calcMaxInv2(a, l, r) <= k) << '\n'; } } return 0; }

Compilation message (stderr)

sortbooks.cpp: In member function 'void segtree::build(std::vector<int>&)':
sortbooks.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         while (n < a.size()) {
      |                ~~^~~~~~~~~~
sortbooks.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int i = 0; i < a.size(); ++i) {
      |                         ~~^~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...