#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define piii pair<int, pii>
#define pllll pair<ll, ll>
#define plli pair<ll, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define size(v) v.size()
#define INF 2e9
#define f first
#define s second
#define ln "\n"
using namespace std;
const int up = 1e5 + 3;
const int LOG = 18;
int d[up], c[up];
int parent[LOG][up], volume[LOG][up];
void build(int n){
stack<int>st;
d[0] = INF;
st.push(0);
for(int v = n; v >= 1; --v){
while(d[st.top()] <= d[v]){
st.pop();
}
parent[0][v] = st.top();
st.push(v);
}
for(int v = 0; v <= n; ++v){
volume[0][v] = c[v];
}
for(int i = 1; i < LOG; ++i){
for(int v = 1; v <= n; ++v){
parent[i][v] = parent[i - 1][parent[i - 1][v]];
volume[i][v] = volume[i - 1][v] + volume[i - 1][parent[i - 1][v]];
}
}
}
int LCA(int R, int V){
for(int i = LOG - 1; i >= 0 && R; --i){
if(volume[i][R] < V){
R = parent[i][R];
V -= volume[i][R];
}
}
return R;
}
void solve(){
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> d[i] >> c[i];
}
build(n);
while(q--){
int R, V;
cin >> R >> V;
cout << LCA(R, V) << "\n";
}
}
int main(){
int t;
t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
441 ms |
18268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |