Submission #1163017

#TimeUsernameProblemLanguageResultExecution timeMemory
1163017SmuggingSpunHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
100 / 100
1722 ms245300 KiB
#include<bits/stdc++.h> #define taskname "B" using namespace std; template<class T>void maximize(T& a, T b){ if(a < b){ a = b; } } int n, m; namespace sub12{ void solve(){ vector<int>w(n + 1); for(int i = 1; i <= n; i++){ cin >> w[i]; } vector<vector<int>>dp(n + 1, vector<int>(n + 1)); for(int i = 1; i <= n; i++){ dp[i][i] = 0; int prev_max = w[i]; for(int j = i + 1; j <= n; j++){ if(w[j] >= prev_max){ dp[i][j] = dp[i][j - 1]; prev_max = w[j]; } else{ dp[i][j] = max(dp[i][j - 1], w[j] + prev_max); } } } for(int _ = 0; _ < m; _++){ int l, r, k; cin >> l >> r >> k; cout << int(k >= dp[l][r]) << "\n"; } } } namespace sub3456{ const int lim = 1e6 + 5; int log_v[lim], spt[lim][20]; pair<int, int>up[lim][20]; int get(int l, int r){ int k = log_v[r - l + 1]; return max(spt[l][k], spt[r - (1 << k) + 1][k]); } void solve(){ log_v[0] = -1; for(int i = 1; i <= n; i++){ cin >> spt[i][0]; log_v[i] = log_v[i >> 1] + 1; } for(int j = 1; j < 20; j++){ for(int i = 1; i + (1 << j) - 1 <= n; i++){ spt[i][j] = max(spt[i][j - 1], spt[i + (1 << (j - 1))][j - 1]); } } stack<int>st; for(int i = 0; i < 20; i++){ up[n + 1][i] = make_pair(n + 1, 0); } for(int i = n; i > 0; st.push(i--)){ while(!st.empty() && spt[i][0] > spt[st.top()][0]){ st.pop(); } int r = (st.empty() ? n + 1 : st.top()); up[i][0] = make_pair(r, i + 1 == r ? 0 : get(i + 1, r - 1) + spt[i][0]); for(int j = 1; j < 20; j++){ up[i][j] = make_pair(up[up[i][j - 1].first][j - 1].first, max(up[i][j - 1].second, up[up[i][j - 1].first][j - 1].second)); } } for(int _ = 0; _ < m; _++){ int l, r, k, opt = 0; cin >> l >> r >> k; for(int i = 19; i > -1; i--){ if(up[l][i].first <= r){ maximize(opt, up[l][i].second); l = up[l][i].first; } } cout << int(k >= max(opt, l == r ? 0 : get(l + 1, r) + spt[l][0])) << "\n"; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n >> m; if(n <= 5000){ sub12::solve(); } else{ sub3456::solve(); } }

Compilation message (stderr)

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...