//srand(time(0)) - always changing
//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define fopen(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const int P = 911;
const ll INF = 1e18;
int gcd(int a, int b) {
while (b) {
a %= b;
swap (a, b);
}
return a;
}
ll __sqrt(ll x) {
ll result = 0;
for (ll k = 1ll << 30; k != 0; k >>= 1) {
if ((result + k) * (result + k) <= x) {
result += k;
}
}
return result;
}
int d[N], c[N], r[N], v[N], ps[N];
vector <int> qu[N];
map <pair <int, int>, int> ans;
void solve() {
int n, q;
vector <int> st;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> d[i] >> c[i];
}
for (int i = 1; i <= q; i++) {
cin >> r[i] >> v[i];
qu[r[i]].pb(v[i]);
}
for (int i = n; i > 0; i--) {
while (!st.empty()) {
if (d[i] < d[st.back()]) break;
else st.popb();
}
ps[i] = c[i];
if (!st.empty()) {
ps[i] += ps[st.back()];
}
st.pb(i);
for (int it: qu[i]) {
// cout << "DBG";
int l = 0, r = len(st) - 1;
while (l <= r) {
int m = l + r >> 1;
// cout << l << ' ' << r << '\n';
int act = st[m];
if (it - (ps[i] - ps[act]) > 0) r = m - 1;
else l = m + 1;
}
if (it - ps[i] > 0) ans[{i, it}] = 0;
else ans[{i, it}] = st[r + 1];
}
}
for (int i = 1; i <= q; i++) {
cout << ans[{r[i], v[i]}] << '\n';
}
}
const bool Cases = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int TT = 1;
if (Cases) cin >> TT;
while (TT--) {
solve();
}
}
Compilation message
fountain.cpp: In function 'void solve()':
fountain.cpp:80:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
20216 KB |
Output is correct |
2 |
Correct |
147 ms |
21880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
115 ms |
20216 KB |
Output is correct |
9 |
Correct |
147 ms |
21880 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
73 ms |
15736 KB |
Output is correct |
12 |
Correct |
259 ms |
27648 KB |
Output is correct |
13 |
Correct |
179 ms |
27636 KB |
Output is correct |
14 |
Correct |
160 ms |
27532 KB |
Output is correct |
15 |
Correct |
147 ms |
27532 KB |
Output is correct |