Submission #342955

#TimeUsernameProblemLanguageResultExecution timeMemory
342955TosicHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
0 / 100
254 ms44920 KiB
#include <bits/stdc++.h> #define maxn 800010 using namespace std; int n, m, a[maxn], allR[maxn], ans; vector<int> sorted[maxn]; void init(int idx, int l, int r){ if(l == r){ sorted[idx]={a[l]}; return; } int mid = (r+l)/2; init(idx*2+1, l, mid); init(idx*2+2, mid+1, r); sorted[idx].resize(sorted[idx*2+1].size()+sorted[idx*2+2].size()); int tmp = lower_bound(sorted[idx*2+2].begin(), sorted[idx*2+2].end(), sorted[idx*2+1].back()) - sorted[idx*2+2].begin(); --tmp; allR[idx] = max(allR[idx*2+1], allR[idx*2+2]); if(tmp >= 0){ allR[idx] = max(allR[idx], sorted[idx*2+2][tmp] + sorted[idx*2+1].back() ); } merge(sorted[idx*2+1].begin(), sorted[idx*2+1].end(), sorted[idx*2+2].begin(), sorted[idx*2+2].end(), sorted[idx].begin()); } vector<int> allIx; int query(int idx, int l, int r, int ql, int qr){ if(l >= ql and r <= qr){ allIx.push_back(idx); return allR[idx]; } if(l > qr or r<ql){ return 0; } int mid = (l+r)/2; return max(query(idx*2+1, l, mid, ql, qr),query(idx*2+2, mid+1, r, ql, qr)); } int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n >> m; for(int i = 0; i < n; ++i){ cin >> a[i]; } init(0, 0, n-1); for(int i = 0; i < m; ++i){ int l,r,k; cin>>l>>r>>k; --l, --r; if(l == r){ cout << "1\n"; continue; } allIx.clear(); int tmp = query(0,0, n-1,l,r), pr = -1; for(auto j:allIx){ int tmpIdx=lower_bound(sorted[j].begin(), sorted[j].end(),pr)-sorted[j].begin(); --tmpIdx; if(tmpIdx >= 0){ tmp = max(sorted[j][tmpIdx] + pr, tmp); } pr = max(pr, sorted[j].back()); } cout << (tmp<=k) << '\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...