#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>
#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
const ll N = 1e5+5, MOD = 1e9+7, M = 2e2+5, LOG = 30;
ll tc = 1, n, m, dr[N], vr[N];
ll x, y, k, pr[N];
string s, s1, s2, ye = "YES", no = "NO";
vector<ll> tt, v;
pairll nx[N][31];
void input(){
cin >> n >> m;
repp(i,1,n) cin >> dr[i] >> vr[i];
}
void build(){
repp(i,1,n){
while(v.size() && dr[v.back()] < dr[i]){
nx[v.back()][0] = mp(i,vr[v.back()]);
//cout << v.back() << " " << i << endl;
v.pop_back();
}
v.pb(i);
}
nx[0][0] = mp(0,0);
while(v.size()){
nx[v.back()][0] = mp(0,vr[v.back()]);
v.pop_back();
}
// repp(i,1,n){
// cout << i << " " << "0" << " " << nx[i][0].fr << " " << nx[i][0].sc << endl;
// }
repp(bt,1,LOG){
repp(i,1,n){
//nx[i][bt] = nx[nx[i][bt]][bt];
ll loc = nx[nx[i][bt-1].fr][bt-1].fr;
ll cost = nx[i][bt-1].sc+nx[nx[i][bt-1].fr][bt-1].sc;
nx[i][bt] = mp(loc,cost);
//cout << i << " " << bt << " " << loc << " " << cost << endl;
}
}
}
void solve(){
build();
while(m--){
cin >> x >> y;
ll op = x;
repm(bt,LOG,0){
ll idx = nx[x][bt].fr, cost = nx[x][bt].sc;
if (bt <= 30){
//cout << bt << " " << x << " " << y << " " << idx << " " << cost << endl;
}
if (cost < y){
x = idx;
y -= cost;
op = idx;
}
}
cout << op << endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//cin >> tc;
while(tc--){
input();
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
48692 KB |
Output is correct |
2 |
Correct |
479 ms |
44952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
363 ms |
48692 KB |
Output is correct |
9 |
Correct |
479 ms |
44952 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
198 ms |
30380 KB |
Output is correct |
12 |
Correct |
571 ms |
55548 KB |
Output is correct |
13 |
Correct |
492 ms |
55340 KB |
Output is correct |
14 |
Correct |
397 ms |
54536 KB |
Output is correct |
15 |
Correct |
372 ms |
55744 KB |
Output is correct |