Submission #468317

#TimeUsernameProblemLanguageResultExecution timeMemory
468317mariowongHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
0 / 100
124 ms5036 KiB
#include <bits/stdc++.h> using namespace std; struct query{ int l,r,id,val; }; int n,m,a[100005],ans[100005],cnt,pt,seg[400005]; stack <int> s; pair <int,int> inv[100005]; query q[100005]; bool cmp(query a1,query a2){ if (a1.r == a2.r) return a1.id < a2.id; return a1.l > a2.l; } bool invcmp(pair<int,int> a1,pair<int,int> a2){ if (a1.first == a2.first) return a1.second > a2.second; return a1.first > a2.first; } void update(int id,int l,int r,int pos,int v){ if (l == r) seg[id]=max(seg[id],v); else { int mid=(l+r)/2; if (pos <= mid)update(id*2,l,mid,pos,v); else update(id*2+1,mid+1,r,pos,v); seg[id]=max(seg[id*2],seg[id*2+1]); } } int getans(int id,int l,int r,int x,int y){ if (x <= l && r <= y) return seg[id]; if (x > r || y < l) return -1; int mid=(l+r)/2; return max(getans(id*2,l,mid,x,y),getans(id*2+1,mid+1,r,x,y)); } int main(){ ios::sync_with_stdio(false); cin >> n >> m; for (int i=1;i<=n;i++){ cin >> a[i]; while (!s.empty() && a[i] >= s.top()) s.pop(); if (!s.empty()) inv[++cnt]=make_pair(s.top(),i); s.push(i); } for (int i=1;i<=m;i++){ cin >> q[i].l >> q[i].r >> q[i].val; q[i].id=i; } sort(inv+1,inv+1+cnt); reverse(inv+1,inv+1+cnt); sort(q+1,q+1+m,cmp); for (int i=1;i<=m;i++){ while (pt < cnt && inv[pt+1].first >= q[i].l) { pt++; update(1,1,n,inv[pt].second,a[inv[pt].first]+a[inv[pt].second]); } if (getans(1,1,n,q[i].l,q[i].r) <= q[i].val) ans[q[i].id]=1; } for (int i=1;i<=m;i++){ cout << ans[i] << "\n"; } return 0; }
#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...