#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ofind find_by_order
#define okey order_of_key
#define int long long
#define double long double
#define pque priority_queue
#define dque deque
#define que queue
#define umap unordered_map
#define uset unordered_set
#define pipii pair<int, pair<int,int>>
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define iter iterator
#define endl '\n'
#define MOD 1000000007
#define INF 1e18
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, Q;
cin >> N >> Q;
int D[N + 1], C[N + 1];
for (int i = 1; i <= N; i++)
cin >> D[i] >> C[i];
int next[N + 1];
stack<int> st;
for (int i = N; i >= 1; i--){
while (!st.empty() && D[st.top()] <= D[i])
st.pop();
if (!st.empty())
next[i] = st.top();
else
next[i] = 0;
st.push(i);
}
bool visited[N + 1];
vector<pii> v[N + 1];
int root[N + 1], prev[N + 1];
memset(visited, 0, sizeof visited);
for (int i = 1; i <= N; i++){
if (visited[i]) continue;
int ptr = i;
while (ptr != 0){
visited[ptr] = 1;
root[ptr] = i;
if (v[i].empty()){
prev[ptr] = 0;
v[i].pb(mp(C[ptr], ptr));
}
else{
prev[ptr] = v[i].back().first;
v[i].pb(mp(prev[ptr] + C[ptr], ptr));
}
ptr = next[ptr];
}
}
while (Q--){
int a, x;
cin >> a >> x;
x = x + prev[a];
a = root[a];
int low = 0, high = v[a].size() - 1;
while (low <= high){
int mid = (low + high) / 2;
if (v[a][mid].first < x)
low = mid + 1;
else
high = mid - 1;
}
if (high + 1 == (int) v[a].size())
cout << 0 << endl;
else
cout << v[a][high + 1].second << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
9488 KB |
Output is correct |
2 |
Correct |
65 ms |
8948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
59 ms |
9488 KB |
Output is correct |
9 |
Correct |
65 ms |
8948 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Runtime error |
523 ms |
524288 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |