/*#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include<bits/stdc++.h>
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define nl "\n"
using namespace std;
using pii = pair<int, int>;
const int N = (int)1e5 + 7; // make sure this is right
const int inf = (int)1e9 + 7;
const ll INF = (ll)1e18 + 7;
const ll MOD = (ll)998244353; // make sure this is right
pii dir[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, q, d[N], c[N], r[N], up[N][21];
ll sum[N][21];
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) cin >> d[i] >> c[i];
d[n + 1] = inf;
stack<int> st;
st.push(n + 1);
for(int i = n; i >= 1; --i) {
while(!st.empty()) {
int x = st.top();
if(d[x] > d[i]) {
r[i] = x;
break;
}
st.pop();
}
st.push(i);
}
for(int i = n; i >= 1; --i) {
up[i][0] = r[i];
sum[i][0] = c[r[i]];
for(int j = 1; j <= 17; ++j) {
sum[i][j] = sum[i][j - 1] + sum[up[i][j - 1]][j - 1];
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
while(q--) {
int i, e;
cin >> i >> e;
if(c[i] >= e) {
cout << i << nl;
continue;
}
int l = 1, r = n - i + 1, res = 0;
while(l <= r) {
int mid = (l + r) >> 1;
ll cur = c[i];
int v = i;
for(int j = 0; j < 18; ++j) {
if(mid >> j & 1) {
cur += sum[v][j];
v = up[v][j];
}
}
if(cur >= e) {
res = v;
r = mid - 1;
} else
l = mid + 1;
}
cout << res << nl;
}
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
while(test--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
28892 KB |
Output is correct |
2 |
Correct |
524 ms |
27124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
398 ms |
28892 KB |
Output is correct |
9 |
Correct |
524 ms |
27124 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
185 ms |
16892 KB |
Output is correct |
12 |
Correct |
661 ms |
31692 KB |
Output is correct |
13 |
Correct |
554 ms |
30924 KB |
Output is correct |
14 |
Correct |
288 ms |
30272 KB |
Output is correct |
15 |
Correct |
143 ms |
30540 KB |
Output is correct |