#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
cin>>n>>q;
vector<pair<int,int>>v;
int sum[n+1];
sum[0]=0;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
sum[i+1]=sum[i]+x;
v.push_back({x,y});
}
while(q--)
{
int x,y;
cin>>x>>y;
if(n<=1005)
{
x--;
int d=v[x].first;///x-1
y-=v[x].second;
if(y>0)
x++;
if(y>0)
while(true)
{
if(x>=n)
{
break;
}
if(d<v[x].first)
{
y-=v[x].second;
d=v[x].first;
}
if(y<=0)break;
x++;
}
x++;
if(x>n)x=0;
cout<<x<<endl;
}
else
{
int l=x;
int r=n;
while(l<r)
{
int mid=l+(r-l)/2;
if(sum[mid]-sum[x-1]<y)
{
l=mid+1;
}
if(sum[mid]-sum[x-1]>y)
{
r=mid;
}
if(sum[mid]-sum[x-1]==y)
{
l=mid;
}
}
cout<<l<<endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
259 ms |
2380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
312 KB |
Output is correct |
8 |
Incorrect |
259 ms |
2380 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |