#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ui unsigned int
#define pii pair<int, int>
#define pis pair<int, string>
#define piii pair<int, pii>
#define pb push_back
#define f first
#define s second
#define oo 2000000002
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int up = 1e5 + 5;
const int lg = 18;
int D[up], C[up];
int sum[up][lg], par[up][lg];
void buildST(int n) {
stack<int>st;
D[0] = oo;
st.push(0);
for(int i = n; i >= 1; --i) {
while(D[st.top()] <= D[i]) {
st.pop();
}
par[i][0] = st.top();
st.push(i);
}
for(int i = 0; i <= n; ++i) {
sum[i][0] = C[i];
}
for(int j = 1; j < lg; ++j) {
for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
par[i][j] = par[par[i][j - 1]][j - 1];
sum[i][j] = sum[i][j - 1] + sum[par[i][j - 1]][j - 1];
}
}
}
int query(int id, int vol) {
for(int i = lg - 1; i >= 0 && id; --i) {
if(sum[id][i] && sum[id][i] < vol) {
vol -= sum[id][i];
id = par[id][i];
}
}
return id;
}
void solve() {
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> D[i] >> C[i];
}
buildST(n);
int id, vol;
while(q--) {
cin >> id >> vol;
cout << query(id, vol) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
18200 KB |
Output is correct |
2 |
Correct |
434 ms |
17296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |