Submission #562878

#TimeUsernameProblemLanguageResultExecution timeMemory
562878StickfishHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
64 / 100
734 ms262144 KiB
#include <iostream> #include <random> #include <vector> using namespace std; using ll = long long; using ush = unsigned short; const ll INF = 1e9 + 177013; const int MAXN = 1e6 + 123; const int SHORT = 1 << 16; int w[MAXN]; int pv[MAXN]; int get_max(int l, int r) { int mx = -INF; int ans = 0; for (int i = l; i < r; ++i) { if (mx > w[i]) ans = max(ans, mx + w[i]); else mx = w[i]; } return ans; } void merg_add(vector<pair<int, int>>& a, pair<int, int> vl) { while (a.size() && a.back().first == vl.first) { a.pop_back(); } a.push_back(vl); } vector<pair<int, int>> merg(vector<pair<int, int>> a, vector<pair<int, int>> b) { vector<pair<int, int>> ans; int n = a.size(); int m = b.size(); int j = 0; for (int i = 0; i < n; ++i) { while (j < m && b[j] < a[i]) { merg_add(ans, b[j]); ++j; } merg_add(ans, a[i]); } while (j < m) { merg_add(ans, b[j]); ++j; } int s = ans.size(); for (int i = s - 2; i >= 0; --i) { ans[i].second = max(ans[i].second, ans[i + 1].second); } return ans; } struct stree { void resize(int n) { sz = n; t.resize(sz * 2 - 1); go_down.resize(sz * 2 - 1); t.shrink_to_fit(); go_down.shrink_to_fit(); init(0, 0, sz); } int get(int l, int r) { return get(0, 0, sz, l, r, lower_bound(t[0].begin(), t[0].end(), pair<int, int>(l, -1)) - t[0].begin()); } private: int sz; vector<vector<pair<int, int>>> t; vector<vector<pair<ush, ush>>> go_down; void init(int ind, int lnd, int rnd) { if (rnd - lnd == 1) { if (pv[lnd] >= 0) t[ind] = {{pv[lnd], w[lnd] + w[pv[lnd]]}}; } else { int mnd = (lnd + rnd) / 2; int chd = ind + (mnd - lnd) * 2; init(ind + 1, lnd, mnd); init(chd, mnd, rnd); t[ind] = merg(t[ind + 1], t[chd]); int s = t[ind].size(); int n = t[ind + 1].size(); int m = t[chd].size(); go_down[ind].assign(s, pair<ush, ush>(0, 0)); int upi = s - 1; if (t[ind + 1].size()) { while (upi >= 0 && t[ind][upi].first > t[ind + 1].back().first) { go_down[ind][upi].first = n; --upi; } for (int i = n - 1; i >= 0; --i) { while (upi >= 0 && (i == 0 || t[ind][upi].first > t[ind + 1][i - 1].first)) { go_down[ind][upi].first = i; --upi; } } } upi = s - 1; if (t[chd].size()) { while (upi >= 0 && t[ind][upi].first > t[chd].back().first) { go_down[ind][upi].second = m; --upi; } for (int j = m - 1; j >= 0; --j) { while (upi >= 0 && (j == 0 || t[ind][upi].first > t[chd][j - 1].first)) { go_down[ind][upi].second = j; --upi; } } } t[ind].shrink_to_fit(); go_down[ind].shrink_to_fit(); } } int get(int ind, int lnd, int rnd, int l, int r, int i) { //cout << "A" << endl; if (i == t[ind].size()) return 0; int mnd = (lnd + rnd) / 2; int chd = ind + (mnd - lnd) * 2; if (lnd >= l && rnd <= r) { return t[ind][i].second; } if (lnd >= r || rnd <= l) return 0; int li = go_down[ind][i].first; int ri = go_down[ind][i].second; while (li < t[ind + 1].size() && t[ind + 1][li].first != t[ind][i].first) li += SHORT; while (ri < t[chd].size() && t[chd][ri].first != t[ind][i].first) ri += SHORT; return max(get(ind + 1, lnd, mnd, l, r, go_down[ind][i].first), get(chd, mnd, rnd, l, r, go_down[ind][i].second)); } }; mt19937 rng(177013); signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> w[i]; //while (n < 1000000) { //w[n++] = rng() % INF; //} vector<pair<int, int>> previnit = {{INF, -1}}; for (int i = 0; i < n; ++i) { while (previnit.back().first <= w[i]) { previnit.pop_back(); } pv[i] = previnit.back().second; previnit.push_back({w[i], i}); } stree tr; tr.resize(n); for (int i = 0; i < m; ++i) { int l, r, k; cin >> l >> r >> k; --l; if (tr.get(l, r) > k) { cout << 0 << '\n'; } else { cout << 1 << '\n'; } } }

Compilation message (stderr)

sortbooks.cpp: In member function 'int stree::get(int, int, int, int, int, int)':
sortbooks.cpp:122:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |          if (i == t[ind].size())
      |              ~~^~~~~~~~~~~~~~~~
sortbooks.cpp:133:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |          while (li < t[ind + 1].size() && t[ind + 1][li].first != t[ind][i].first)
      |                 ~~~^~~~~~~~~~~~~~~~~~~
sortbooks.cpp:135:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |          while (ri < t[chd].size() && t[chd][ri].first != t[ind][i].first)
      |                 ~~~^~~~~~~~~~~~~~~
#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...