#include <bits/stdc++.h>
using namespace std;
struct rezervor
{
int d,c;
};
struct binary_lifting
{
int poz,val;
};
int n,q;
binary_lifting u[100005][25];
rezervor v[100005];
int query(int val, int poz)
{
if(v[poz].c>=val)
{
return poz;
}
val-=v[poz].c;
for(int p=20;p>=0;p--)
{
if(u[poz][p].val<val)
{
if(u[poz][p].poz==0)
{
return 0;
}
val-=u[poz][p].val;
poz = u[poz][p].poz;
}
}
return u[poz][0].poz;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>v[i].d>>v[i].c;
}
stack<int> st;
for(int i=n;i>=1;i--)
{
while(!st.empty() && v[st.top()].d<=v[i].d)
{
st.pop();
}
if(st.empty())
{
u[i][0].poz = 0;
u[i][0].val = 0;
}
else
{
u[i][0].poz = st.top();
u[i][0].val = v[st.top()].c;
}
st.push(i);
}
for(int k=1;k<=20;k++)
{
for(int i=1;i<=n;i++)
{
u[i][k].poz = u[u[i][k-1].poz][k-1].poz;
u[i][k].val = u[i][k-1].val + u[u[i][k-1].poz][k-1].val;
}
}
for(int i=1;i<=q;i++)
{
int val,poz;
cin>>poz>>val;
cout<<query(val,poz)<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
241 ms |
23616 KB |
Output is correct |
2 |
Correct |
241 ms |
22328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
241 ms |
23616 KB |
Output is correct |
9 |
Correct |
241 ms |
22328 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
97 ms |
13924 KB |
Output is correct |
12 |
Correct |
345 ms |
26296 KB |
Output is correct |
13 |
Correct |
216 ms |
25448 KB |
Output is correct |
14 |
Correct |
164 ms |
24868 KB |
Output is correct |
15 |
Correct |
132 ms |
25004 KB |
Output is correct |