#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,q;
cin>>n>>q;
pair<long long,long long> niza[n];
for(long long i=0; i<n; i++)cin>>niza[i].first>>niza[i].second;
if(n<=2000 && q<=2000)
{
for(long long i=0; i<q; i++)
{
long long r,v;
cin>>r>>v;
r--;
v-=niza[r].second;
if(v<=0)
{
cout<<r+1<<endl;
continue;
}
long long last=niza[r].first;
r++;
while(v>0)
{
if(niza[r].first>last)
{
v-=niza[r].second;
last=niza[r].first;
if(v<=0)break;
}
r++;
if(r==n)break;
}
if(r>=n)
{
cout<<0<<endl;
}
else
{
cout<<r+1<<endl;
}
}
}
else
{
long long prefsum[n];
prefsum[0]=niza[0].second;
for(long long i=1; i<n; i++)
{
prefsum[i]=prefsum[i-1]+niza[i].second;
}
for(long long i=0; i<q; i++)
{
long long r,v;
cin>>r>>v;
r--;
long long b=r,e=n-1;
long long res=INT_MAX;
while(b<=e)
{
long long mid=(b+e)/2;
long long curr=prefsum[mid];
if(r)curr-=prefsum[r-1];
if(curr>=v)
{
res=min(res,mid);
e=mid-1;
}
else b=mid+1;
}
if(res==INT_MAX)res=-1;
cout<<res+1<<endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
484 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
6244 KB |
Output is correct |
2 |
Correct |
260 ms |
6564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
484 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
229 ms |
6244 KB |
Output is correct |
9 |
Correct |
260 ms |
6564 KB |
Output is correct |
10 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |