Submission #1028362

#TimeUsernameProblemLanguageResultExecution timeMemory
1028362FatonimHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1270 ms180032 KiB
// "We create our own demons" #include <bits/stdc++.h> using namespace std; #ifdef ONPC #include "debug.h" #else #define dbg(...) #endif #define int long long #define ll long long #define ld long double #define pi pair<int, int> // vector #define sz(a) (int)((a).size()) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define maxel(x) (*max_element(all(x))) #define minel(x) (*min_element(all(x))) #define maxpos(x) (max_element(all(x)) - x.begin()) #define minpos(x) (min_element(all(x)) - x.begin()) #define rev(x) (reverse(all(x))) // bits #define popcount(n) __builtin_popcountll(n) #define clz(n) __builtin_clzll(n) // math #define sqr(n) ((n) * (n)) #define divup(a, b) (((a) + (b)-1) / (b)) // ostream #define Fixed(a) cout << fixed << setprecision(12) << a; template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; } template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>; template <class T> using V = vector<T>; using vi = V<int>; using vd = V<ld>; using vb = V<bool>; using vpi = V<pi>; using vvi = V<vi>; using vvb = V<vb>; const int mod = 1e9 + 7; // 998244353 1e9 + 7 const ll inf = (int)(1e18) + 7; const int inf_s = 1e9 + 7; const ld eps = 1e-9; const int B = 32; const int N = 1000 + 3; const int logn = 21; const int maxn = 1e6 + 7; /////////////////////////solve///////////////////////// struct segtree { public: struct node { int mx = 0; void apply(int tl, int tr, int x) { mx = x; } }; private: int n; V<node> tree; node unite(const node &a, const node &b) { node res; res.mx = max(a.mx, b.mx); return res; } void push(int v, int tl, int tr) { } template <class T> void build(const V<T>& a, int v, int tl, int tr) { if (tr - tl == 1) { tree[v].apply(tl, tr, a[tl]); return; } int tm = (tl + tr) >> 1; build(a, 2 * v, tl, tm); build(a, 2 * v + 1, tm, tr); tree[v] = unite(tree[2 * v], tree[2 * v + 1]); } void build(int v, int tl, int tr) { if (tr - tl == 1) { return; } int tm = (tl + tr) >> 1; build(2 * v, tl, tm); build(2 * v + 1, tm, tr); tree[v] = unite(tree[2 * v], tree[2 * v + 1]); } template <class T> void update(int l, int r, const T &x, int v, int tl, int tr) { if (tl >= r || tr <= l) return; if (tl >= l && tr <= r) { tree[v].apply(tl, tr, x); return; } int tm = (tl + tr) >> 1; push(v, tl, tr); update(l, r, x, 2 * v, tl, tm); update(l, r, x, 2 * v + 1, tm, tr); tree[v] = unite(tree[2 * v], tree[2 * v + 1]); } node get(int l, int r, int v, int tl, int tr) { if (tl >= l && tr <= r) return tree[v]; int tm = (tl + tr) >> 1; push(v, tl, tr); if (tm <= l) return get(l, r, 2 * v + 1, tm, tr); if (tm >= r) return get(l, r, 2 * v, tl, tm); return unite(get(l, r, 2 * v, tl, tm), get(l, r, 2 * v + 1, tm, tr)); } public: segtree(int n) : n(n) { tree.resize(4 * n); build(1, 0, n); } template <class T> segtree(const V<T>& a) { n = sz(a); tree.resize(4 * n); build(a, 1, 0, n); } template <class T> void update(int l, int r, const T& x) { update(l, r + 1, x, 1, 0, n); } template <class T> void update(int i, const T& x) { update(i, i + 1, x, 1, 0, n); } node get(int l, int r) { if (l > r) return {-inf}; return get(l, r + 1, 1, 0, n); } node get(int i) { return get(i, i + 1, 1, 0, n); } }; struct query { int r, k, qi; }; V<query> L[maxn]; int ans[maxn]; void solve() { int n, m; cin >> n >> m; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int qi = 0; qi < m; ++qi) { int l, r, k; cin >> l >> r >> k; --l; --r; L[l].push_back({r, k, qi}); } segtree st1(a), st2(n); vi st; st.push_back(n); for (int i = n - 1; i >= 0; --i) { while (sz(st) > 1 && a[st.back()] < a[i]) { int j = st.back(); st.pop_back(); st2.update(j, 0); } int mx = st1.get(i + 1, st.back() - 1).mx; dbg(i, a[i], mx, st.back()); st2.update(i, a[i] + mx); st.push_back(i); for (auto& [r, k, qi] : L[i]) { dbg(st); auto it = upper_bound(rall(st), r); dbg(*it, r); int res = 0; --it; dbg(i, *it); chmax(res, st2.get(i, *it - 1).mx); dbg(res); if (*it != n) { chmax(res, a[*it] + st1.get(*it + 1, r).mx); } dbg(res); ans[qi] = (res <= k); } } for (int qi = 0; qi < m; ++qi) { cout << ans[qi] << "\n"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef ONPC freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif int t = 1; //cin >> t; for (int i = 1; i <= t; ++i) { #ifdef ONPC cerr << "===========" << i << "===========" << '\n'; #endif solve(); } #ifdef ONPC cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; #endif }
#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...