#include <bits/stdc++.h>
using namespace std;
int j,b[100005][20],x,y,c[100005][20],i,n,q,dr[100005],pu[20],l2[100005];
pair<int,int> a[100005];
stack<int> s;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>q;
for(i=1;i<=n;i++)
{
if(i>1)l2[i]=l2[i>>1]+1;
//dr[i]=n+1;
cin>>a[i].first>>a[i].second;
while(!s.empty()&&a[i].first>a[s.top()].first)
{
dr[s.top()]=i;
s.pop();
}
s.push(i);
}
pu[0]=1;
for(i=1;i<=20;i++)
pu[i]=pu[i-1]<<1;
for(i=1;i<=n;i++)
{
b[i][0]=dr[i];
c[i][0]=a[i].second;
}
for(j=1;j<=l2[n];j++)
for(i=1;i<=n;i++)
{
b[i][j]=b[b[i][j-1]][j-1];
c[i][j]=c[i][j-1]+c[b[i][j-1]][j-1];
}
while(q--)
{
cin>>x>>y;
int k=x;
for(i=l2[n-x+1];i>=0;i--)
{
if(!k)break;
if(c[k][i]<y)
{
y-=c[k][i];
k=b[k][i];
}
}
cout<<k<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
17420 KB |
Output is correct |
2 |
Correct |
181 ms |
19268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
500 KB |
Output is correct |
8 |
Correct |
181 ms |
17420 KB |
Output is correct |
9 |
Correct |
181 ms |
19268 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
77 ms |
12116 KB |
Output is correct |
12 |
Correct |
294 ms |
22620 KB |
Output is correct |
13 |
Correct |
177 ms |
22240 KB |
Output is correct |
14 |
Correct |
174 ms |
21608 KB |
Output is correct |
15 |
Correct |
117 ms |
22264 KB |
Output is correct |