#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define nL '\n'
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
vvl up;
vvl sum;
// void readInput(ll n, ll i) {
// ll d;
// ll c;
// cin >> d >> c;
// if (i < n)
// readInput(n, i+1);
// if (j > n)
// j = 0;
// up[i][0] = j;
// sum[i][0] = c;
// }
int main() {
#ifdef LOCAL
freopen("io/input.txt", "r", stdin);
freopen("io/output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
ll n, q;
cin >> n >> q;
vpll input(n+1);
for (int i = 1; i <= n; i++)
cin >> input[i].first >> input[i].second;
ll LOG = log2(n);
up = vvl(n+1, vl(LOG));
sum = vvl(n+1, vl(LOG));
stack<ll> stk;
for (int i = n; i > 0; i--) {
while (!stk.empty() && input[stk.top()].first <= input[i].first)
stk.pop();
if (!stk.empty())
up[i][0] = stk.top();
else
up[i][0] = 0;
sum[i][0] = input[i].second;
stk.push(i);
}
for (ll j = 1; j < LOG; j++) {
for (ll v = 1; v <= n; v++) {
up[v][j] = up[up[v][j-1]][j-1];
sum[v][j] = sum[v][j-1] + sum[up[v][j-1]][j-1];
}
}
// for (ll v = n; v > 0; v--) {
// for (ll j = 1; j < LOG; j++) {
// up[v][j] = up[up[v][j-1]][j-1];
// sum[v][j] = sum[v][j-1] + sum[up[v][j-1]][j-1];
// }
// }
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j < LOG; j++)
// cout << "(" << up[i][j] << ", " << sum[i][j] << ") ";
// cout << nL;
// }
while (q--) {
ll r;
ll v;
cin >> r >> v;
for (ll i = LOG-1; i >= 0; i--) {
if (sum[r][i] < v) {
v -= sum[r][i];
r = up[r][i];
}
}
cout << r << nL;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 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 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
352 ms |
34608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 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 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |