Submission #1309659

#TimeUsernameProblemLanguageResultExecution timeMemory
1309659fauntleroyHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
17 / 100
775 ms327680 KiB
#include <iostream> #include <cstdio> #include <vector> #include <array> #include <string> #include <algorithm> #include <numeric> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <cmath> #include <climits> #include <iomanip> #include <limits> #include <tuple> #include <stack> #include <bitset> #include <cstring> #include <sstream> #include <functional> #include <random> #include <memory> using namespace std; using ll = long long; int n, q; vector<ll> a; struct mst { int n; vector<vector<ll>> t; mst(const vector<ll>& a) { n = a.size(); t.assign(4 * n, {}); build(1, 0, n - 1, a); } void build(int u, int l, int r, const vector<ll>& a) { if (l == r) { t[u] = { a[l] }; return; } int tm = (l + r) / 2; build(u * 2, l, tm, a); build(u * 2 + 1, tm + 1, r, a); auto& L = t[u * 2]; auto& R = t[u * 2 + 1]; t[u].resize(L.size() + R.size()); merge(L.begin(), L.end(), R.begin(), R.end(), t[u].begin()); } ll query(int l, int r, ll k) { ll ans = LLONG_MIN; query_impl(1, 0, n - 1, l, r, k, ans); return ans; } void query_impl(int u, int l, int r, int ql, int qr, ll k, ll& ans) { if (ql > r || qr < l) { return; } if (ql <= l && r <= qr) { auto& v = t[u]; auto it = lower_bound(v.begin(), v.end(), k); if (it != v.begin()) { --it; ans = max(ans, *it); } return; } int tm = (l + r) / 2; query_impl(u * 2, l, tm, ql, qr, k, ans); query_impl(u * 2 + 1, tm + 1, r, ql, qr, k, ans); } }; static unique_ptr<mst> MST; struct Node { ll k, mx; int l, r; bool f; }; vector<Node> seg; Node merge(const Node& a, const Node& b) { if (a.f) { return b; } if (b.f) { return a; } Node r; r.f = false; r.l = a.l; r.r = b.r; r.mx = max(a.mx, b.mx); ll c = LLONG_MIN; ll y = MST->query(b.l, b.r, a.mx); if (y != LLONG_MIN) { c = a.mx + y; } r.k = max({ a.k, b.k, c }); return r; } void build(int u, int l, int r) { if (l == r) { seg[u].mx = a[l]; seg[u].k = 0; seg[u].l = seg[u].r = l; seg[u].f = false; return; } int mid = (l + r) / 2; build(2 * u, l, mid); build(2 * u + 1, mid + 1, r); seg[u] = merge(seg[2 * u], seg[2 * u + 1]); } Node query(int u, int l, int r, int ql, int qr) { if (r < ql || l > qr) { return { LLONG_MIN, LLONG_MIN, 0, -1, true }; } if (ql <= l && r <= qr) { return seg[u]; } int mid = (l + r) >> 1; Node L = query(2 * u, l, mid, ql, qr); Node R = query(2 * u + 1, mid + 1, r, ql, qr); return merge(L, R); } void solve() { cin >> n >> q; a.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } MST = make_unique<mst>(a); seg.assign(4 * n, { LLONG_MIN, LLONG_MIN, 0, -1, true }); build(1, 0, n - 1); while (q--) { int l, r; ll k; cin >> l >> r >> k; --l, --r; Node qr = query(1, 0, n - 1, l, r); if (qr.k == LLONG_MIN || k < qr.k) { cout << "0\n"; } else { cout << "1\n"; } } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); 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...
#Verdict Execution timeMemoryGrader output
Fetching results...