This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <stack>
#define nmax 100000
using namespace std;
struct fountain{
int l, d;
} v[nmax + 5];
int ans[nmax+5], smen[nmax + 5], n, q;
void build(){
stack<int> s;
for(int i=1; i<=n; i++){
if(s.size() == 0)
s.push(i);
else{
while(!s.empty() && v[i].l > v[s.top()].l){
ans[s.top()] = i;
s.pop();
}
s.push(i);
}
}
while(!s.empty()){
ans[s.top()] = -1;
s.pop();
}
}
int main() {
cin>>n>>q;
for(int i=1; i<=n; i++){
cin>>v[i].l>>v[i].d;
}
if(n <= 2000){
build();
for(int t=0; t<q; t++){
int l, i, ok = true;
cin>>i>>l;
while(true){
l -= v[i].d;
if(l <= 0)
break;
if(i == -1){
ok = false;
break;
}
i = ans[i];
}
cout<<max(i, ok)<<endl;
}
}
else{
for(int i=1; i<=n; i++){
smen[i] = smen[i-1] + v[i].d;
}
for(int t=0; t<q; t++){
int l, i;
cin>>i>>l;
int st=i, dr=n+1;
while(st<dr){
int mid = (st+dr)/2;
if(smen[mid] - smen[i-1] < l)
st = mid + 1;
else dr = mid;
}
if(dr == n+1)
cout<<0<<'\n';
else cout<<dr<<'\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |