#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#ifndef LOCAL
#define cerr if(0)cerr
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 5e5+1, inf = 1e9+1;
int n, q;
int d[N];
int ans[N];
struct Q {
int t, l, r, id;
bool operator<(const Q& o) const {
return t < o.t;
}
};
vector<Q> queries;
pii jump[N]; // st - dist, nd - t
ll get_pos(int x, int t) {
return -x + jump[x].st * (t/jump[x].nd);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>q;
for(int i=1; i<=n; ++i) {
cin>>d[i];
}
jump[0] = mp(1, 1);
for(int i=1; i<=n; ++i) {
jump[i] = mp(0, inf);
ll lo = 1, hi = inf, mid;
while(lo<=hi) {
mid = (lo+hi)/2;
if(get_pos(i-1, mid) - (-i) > d[i]) {
jump[i] = mp(get_pos(i-1, mid)-1 - (-i), mid);
hi = mid-1;
}
else {
lo = mid+1;
}
}
}
queries.resize(q);
for(int i=0; i<q; ++i) {
cin>>queries[i].t>>queries[i].l>>queries[i].r;
queries[i].id = i;
}
sort(queries.begin(), queries.end());
for(auto qry: queries) {
auto bs = [](bool find_leftmost, Q& qq)->int {
int lo = 0, hi = n, mid;
int res = -1;
while(lo<=hi) {
mid = (lo+hi)/2;
if(get_pos(mid, qq.t) < qq.l) {
hi = mid-1;
}
else if(get_pos(mid, qq.t) > qq.r) {
lo = mid+1;
}
if(get_pos(mid, qq.t) >= qq.l && get_pos(mid, qq.t) <= qq.r) {
res = mid;
if(find_leftmost) hi = mid-1;
else lo = mid+1;
}
}
return res;
};
int leftmost = bs(1, qry);
int rightmost = bs(0, qry);
if(leftmost == -1) ans[qry.id] = 0;
else {
ans[qry.id] = rightmost - leftmost + 1;
}
}
for(int i=0; i<q; ++i) {
cout<<ans[i]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
778 ms |
34576 KB |
Output is correct |
2 |
Correct |
807 ms |
34448 KB |
Output is correct |
3 |
Correct |
776 ms |
34600 KB |
Output is correct |
4 |
Correct |
772 ms |
34412 KB |
Output is correct |
5 |
Correct |
772 ms |
34540 KB |
Output is correct |
6 |
Correct |
789 ms |
34632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
778 ms |
34576 KB |
Output is correct |
2 |
Correct |
807 ms |
34448 KB |
Output is correct |
3 |
Correct |
776 ms |
34600 KB |
Output is correct |
4 |
Correct |
772 ms |
34412 KB |
Output is correct |
5 |
Correct |
772 ms |
34540 KB |
Output is correct |
6 |
Correct |
789 ms |
34632 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
504 ms |
33048 KB |
Output is correct |
14 |
Correct |
512 ms |
33644 KB |
Output is correct |
15 |
Correct |
486 ms |
32252 KB |
Output is correct |
16 |
Correct |
518 ms |
32748 KB |
Output is correct |
17 |
Correct |
583 ms |
37228 KB |
Output is correct |
18 |
Correct |
591 ms |
37064 KB |
Output is correct |
19 |
Correct |
590 ms |
37356 KB |
Output is correct |
20 |
Correct |
587 ms |
36972 KB |
Output is correct |
21 |
Correct |
586 ms |
37228 KB |
Output is correct |
22 |
Correct |
607 ms |
37228 KB |
Output is correct |
23 |
Correct |
584 ms |
36972 KB |
Output is correct |
24 |
Correct |
591 ms |
37120 KB |
Output is correct |
25 |
Correct |
769 ms |
34540 KB |
Output is correct |
26 |
Correct |
769 ms |
34668 KB |
Output is correct |
27 |
Correct |
638 ms |
36588 KB |
Output is correct |
28 |
Correct |
625 ms |
36844 KB |
Output is correct |
29 |
Correct |
629 ms |
36484 KB |
Output is correct |
30 |
Correct |
659 ms |
36716 KB |
Output is correct |
31 |
Correct |
637 ms |
36968 KB |
Output is correct |
32 |
Correct |
629 ms |
33132 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |