Submission #519538

#TimeUsernameProblemLanguageResultExecution timeMemory
519538Dasha_GnedkoHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
2411 ms104556 KiB
#include <bits/stdc++.h> //#include <ext/pb_ds/detail/standard_policies.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("Ofast") //#pragma GCC target("avx2") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; //using namespace __gnu_pbds; //template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>; mt19937 gen(time(0)); #define ll long long #define ld long double #define pb push_back #define F first #define S second #define TIME clock() * 1.0 / CLOCKS_PER_SEC #define sz(a) int32_t(a.size()) #define endl '\n' //#define int long long const int N = 1e6 + 100; const int M = 31; const int mod = 998244353; const int inf = 2e9 + 100; int a[N]; struct DO { vector < int > t, add; int pr = 2; void build(int n) { while (pr < n) pr *= 2; t.resize(2 * pr, 0); add.resize(2 * pr, 0); for(int i = pr; i < pr + n; i++) t[i] = a[i - pr]; for(int v = pr - 1; v > 0; v--) t[v] = max(t[v * 2], t[v * 2 + 1]); } void push(int v) { t[v * 2] += add[v]; t[v * 2 + 1] += add[v]; add[v * 2] += add[v]; add[v * 2 + 1] += add[v]; add[v] = 0; } void upd(int l, int r, int v, int cl, int cr, int zn) { if (l > r) return; if (cr < l || r < cl) return; if (cl != cr) push(v); if (l <= cl && cr <= r) { t[v] += zn; add[v] += zn; return; } int mid = (cl + cr) / 2; upd(l, r, v * 2, cl, mid, zn); upd(l, r, v * 2 + 1, mid + 1, cr, zn); t[v] = max(t[v * 2], t[v * 2 + 1]); } int get(int l, int r, int v, int cl, int cr) { if (cr < l || r < cl) return 0; if (cl != cr) push(v); if (l <= cl && cr <= r) return t[v]; int mid = (cl + cr) / 2; return max(get(l, r, v * 2, cl, mid), get(l, r, v * 2 + 1, mid + 1, cr)); } }; DO t; vector < pair < pair < int, int >, int > > ask[N]; int ans[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif // LOCAL int n, q; cin >> n >> q; for(int i = 0; i < n; i++) cin >> a[i]; t.build(n); for(int i = 0; i < q; i++) { int l, r, k; cin >> l >> r >> k; l--; r--; ask[l].pb({{r, k}, i}); } vector < pair < pair < int, int >, int > > b; int ls = n - 1; for(int i = n - 1; i >= 0; i--) { if (i + 1 < n && a[i + 1] < a[i]) ls = i; while (sz(b) && b.back().S < a[i]) { t.upd(b.back().F.F + 1, b.back().F.S, 1, 0, t.pr - 1, -b.back().S); t.upd(b.back().F.F, b.back().F.F, 1, 0, t.pr - 1, b.back().S); b.pop_back(); } if (!sz(b)) { b.pb({{i, n - 1}, a[i]}); t.upd(i, i, 1, 0, t.pr - 1, -a[i]); t.upd(i + 1, n - 1, 1, 0, t.pr - 1, a[i]); } else { t.upd(i + 1, b.back().F.F - 1, 1, 0, t.pr - 1, a[i]); t.upd(i, i, 1, 0, t.pr - 1, -a[i]); b.pb({{i, b.back().F.F - 1}, a[i]}); } for(auto to: ask[i]) ans[to.S] = (t.get(i, to.F.F, 1, 0, t.pr - 1) <= to.F.S); } for(int i = 0; i < q; i++) cout << ans[i] << endl; }

Compilation message (stderr)

sortbooks.cpp: In function 'int32_t main()':
sortbooks.cpp:116:9: warning: variable 'ls' set but not used [-Wunused-but-set-variable]
  116 |     int ls = n - 1;
      |         ^~
#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...