Submission #228203

#TimeUsernameProblemLanguageResultExecution timeMemory
228203emil_physmathHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
64 / 100
3087 ms262144 KiB
#include <algorithm> #include <iostream> #include <bitset> #include <vector> using namespace std; int a[1000'000], w[1000'000]; bitset<1000'000> ans; int* t[4000'000]; int tans[4000'000], tmax[4000'000]; void Build(int v, int vl, int vr) { t[v] = new int[vr - vl + 1]; if (vl == vr) { tmax[v] = t[v][0] = a[vr]; return; } int vm = vl + (vr - vl) / 2; Build(v * 2, vl, vm); Build(v * 2 + 1, vm + 1, vr); tmax[v] = max(tmax[v * 2], tmax[v * 2 + 1]); merge(t[v * 2], t[v * 2] + vm - vl + 1, t[v * 2 + 1], t[v * 2 + 1] + vr - vm, t[v]); tans[v] = max(tans[v * 2], tans[v * 2 + 1]); auto smaller = lower_bound(t[v * 2 + 1], t[v * 2 + 1] + vr - vm, tmax[v * 2]); if (smaller != t[v * 2 + 1]) tans[v] = max(tans[v], tmax[v * 2] + *--smaller); delete [] t[v * 2]; delete [] t[v * 2 + 1]; if (v == 1) delete [] t[v]; } struct UPD { int maxAtLeft, q; }; vector<UPD> upd[4000'000]; int Query(int v, int vl, int vr, int l, int r, int maxAtLeft, int q) { if (l > vr || vl > r) return 0; if (l <= vl && vr <= r) { ans[q] = min((bool)ans[q], tans[v] <= w[q]); upd[v].push_back(UPD()); upd[v].back().maxAtLeft = maxAtLeft; upd[v].back().q = q; return tmax[v]; } int vm = vl + (vr - vl) / 2; maxAtLeft = max(Query(v * 2, vl, vm, l, r, maxAtLeft, q), maxAtLeft); maxAtLeft = max(Query(v * 2 + 1, vm + 1, vr, l, r, maxAtLeft, q), maxAtLeft); return maxAtLeft; } void Solve(int v, int vl, int vr) { if (vl == vr) { t[v] = new int[1]; t[v][0] = a[vr]; } else { int vm = vl + (vr - vl) / 2; Solve(v * 2, vl, vm); Solve(v * 2 + 1, vm + 1, vr); t[v] = new int[vr - vl + 1]; merge(t[v * 2], t[v * 2] + vm - vl + 1, t[v * 2 + 1], t[v * 2 + 1] + vr - vm, t[v]); delete [] t[v * 2]; delete [] t[v * 2 + 1]; } int p = vr - vl; sort(upd[v].begin(), upd[v].end(), [](const UPD& l, const UPD& r) { return l.maxAtLeft > r.maxAtLeft; }); for (UPD x: upd[v]) { int maxAtLeft = x.maxAtLeft, q = x.q; while (p >= 0 && t[v][p] >= maxAtLeft) --p; if (p >= 0) ans[q] = min((bool)ans[q], maxAtLeft + t[v][p] <= w[q]); } upd[v].clear(); vector<UPD>().swap(upd[v]); if (v == 1) delete [] t[v]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i]; Build(1, 0, n - 1); const int bl = 600'000; for (int s = 0; s < m; s += bl) { for (int i = s; i < m && i < s + bl; ++i) { ans[i] = 1; int l, r; cin >> l >> r >> w[i]; Query(1, 0, n - 1, --l, --r, 0, i); } Solve(1, 0, n - 1); } for (int i = 0; i < m; ++i) cout << ans[i] << '\n'; }
#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...