#include <iostream>
#include <stack>
using namespace std;
using PII = pair<int, int>;
static constexpr int NMAX = (int)(1e5 + 1), LOGMAX = 17;
int Log2[NMAX];
int N, Q;
int D[NMAX], C[NMAX];
PII Next[LOGMAX][NMAX];
static inline void Read()
{
ios_base ::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> Q;
for (int i = 1; i <= N; ++i)
{
cin >> D[i] >> C[i], Next[0][i] = {0, 0};
if (i >= 2)
Log2[i] = 1 + Log2[(i >> 1)];
}
return;
}
static inline void Get_level_0()
{
stack<int> S;
for (int i = 1; i <= N; ++i)
{
while (!S.empty() && D[i] > D[S.top()])
Next[0][S.top()].first = i, Next[0][S.top()].second += C[i], S.pop();
S.push(i);
}
return;
}
static inline void Precompute()
{
for (int i = 1; i <= Log2[N]; ++i)
for (int j = 1; j <= N; ++j)
if (Next[i - 1][j].first)
if (Next[i - 1][Next[i - 1][j].first].first)
Next[i][j] = {Next[i - 1][Next[i - 1][j].first].first, Next[i - 1][j].second + Next[i - 1][Next[i - 1][j].first].second};
return;
}
static inline void Test_Case()
{
int node = 0, target = 0;
cin >> node >> target;
if (C[node] >= target)
{
cout << node << '\n';
return;
}
if (!Next[0][node].first)
{
cout << 0 << '\n';
return;
}
target -= C[node];
for (int i = Log2[N - node + 1]; i >= 0; --i)
if (Next[i][node].first)
if (target - Next[i][node].second >= 0)
target -= Next[i][node].second, node = Next[i][node].first;
if (target == 0)
cout << node;
else
cout << Next[0][node].first;
cout << '\n';
return;
}
static inline void Solve()
{
for (int q = 1; q <= Q; ++q)
Test_Case();
return;
}
int main()
{
Read();
Get_level_0();
Precompute();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
13860 KB |
Output is correct |
2 |
Correct |
101 ms |
12796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
102 ms |
13860 KB |
Output is correct |
9 |
Correct |
101 ms |
12796 KB |
Output is correct |
10 |
Correct |
1 ms |
416 KB |
Output is correct |
11 |
Correct |
46 ms |
6988 KB |
Output is correct |
12 |
Correct |
227 ms |
14912 KB |
Output is correct |
13 |
Correct |
126 ms |
13104 KB |
Output is correct |
14 |
Correct |
77 ms |
6752 KB |
Output is correct |
15 |
Correct |
58 ms |
4872 KB |
Output is correct |