#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, q;
cin>>n>>q;
int L=log2(n)+1;
int d[n+1], c[n+1];
c[0]=d[0]=1e9+1;
pair<int, long long>up[n+1][L+1];
for(int i=1; i<=n; i++)
{
cin>>d[i]>>c[i];
}
stack<pair<long long, long long>>s;
s.push({c[0], 0});
for(int i=n; i; i--)
{
while(d[i]>=s.top().first)
{
s.pop();
}
up[i][0].first=s.top().second;
up[i][0].second=c[s.top().second];
for(int j=1; j<=L; j++)
{
up[i][j].first=up[up[i][j-1].first][j-1].first;
up[i][j].second=up[i][j-1].second+up[up[i][j-1].first][j-1].second;
}
s.push({d[i], i});
}
while(q--)
{
int idx;
long long val;
cin>>idx>>val;
val-=c[idx];
if(val<=0)
{
cout<<idx<<"\n";
continue;
}
for(int i=L; i>=0; i--)
{
if(up[idx][i].second<val)
{
val-=up[idx][i].second;
idx=up[idx][i].first;
}
}
cout<<up[idx][0].first<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
812 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
33104 KB |
Output is correct |
2 |
Correct |
426 ms |
31056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
812 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
373 ms |
33104 KB |
Output is correct |
9 |
Correct |
426 ms |
31056 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
223 ms |
17828 KB |
Output is correct |
12 |
Correct |
585 ms |
36128 KB |
Output is correct |
13 |
Correct |
477 ms |
34444 KB |
Output is correct |
14 |
Correct |
395 ms |
33360 KB |
Output is correct |
15 |
Correct |
405 ms |
33540 KB |
Output is correct |