#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 5e5 + 7;
const int INF = (int) 1e9;
int a[N], d[N], when[N], jump[N], p[N];
int n, q;
int calc(int t, int x) {
return -x + (t / when[x]) * when[x];
}
int get(int t, int x) {
int low = 1, high = n, sol = n + 1;
while (low <= high) {
int mid = (low + high) / 2;
if (calc(t, mid) <= x) {
high = mid - 1;
sol = mid;
} else {
low = mid + 1;
}
}
return n - sol + 1;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
for (int i = 1; i <= n; i++) {
a[i] = -i;
}
when[1] = d[1];
jump[1] = d[1];
for (int i = 2; i <= n; i++) {
int from = -i + d[i] + 1 + (i - 1);
assert(jump[i - 1] != 0);
int moment = (from - 1) / jump[i - 1] + 1;
int t = when[i - 1] * moment;
when[i] = t;
jump[i] = a[i - 1] + jump[i - 1] * (when[i] / when[i - 1]) - a[i] - 1;
}
for (int i = 1; i <= n; i++) {
assert(when[i] == jump[i]);
}
for (int i = 1; i < n; i++) {
assert(when[i] <= when[i + 1]);
}
for (int i = 1; i <= q; i++) {
int t, l, r;
cin >> t >> l >> r;
cout << get(t, r) - get(t, l - 1) + (l <= t && t <= r) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
428 ms |
26584 KB |
Output is correct |
2 |
Correct |
427 ms |
26576 KB |
Output is correct |
3 |
Correct |
461 ms |
26624 KB |
Output is correct |
4 |
Correct |
433 ms |
26532 KB |
Output is correct |
5 |
Correct |
424 ms |
26572 KB |
Output is correct |
6 |
Correct |
422 ms |
26552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
428 ms |
26584 KB |
Output is correct |
2 |
Correct |
427 ms |
26576 KB |
Output is correct |
3 |
Correct |
461 ms |
26624 KB |
Output is correct |
4 |
Correct |
433 ms |
26532 KB |
Output is correct |
5 |
Correct |
424 ms |
26572 KB |
Output is correct |
6 |
Correct |
422 ms |
26552 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
246 ms |
25064 KB |
Output is correct |
14 |
Correct |
260 ms |
25732 KB |
Output is correct |
15 |
Correct |
233 ms |
24296 KB |
Output is correct |
16 |
Correct |
246 ms |
24824 KB |
Output is correct |
17 |
Correct |
327 ms |
29224 KB |
Output is correct |
18 |
Correct |
326 ms |
29124 KB |
Output is correct |
19 |
Correct |
318 ms |
29256 KB |
Output is correct |
20 |
Correct |
316 ms |
29132 KB |
Output is correct |
21 |
Correct |
331 ms |
29204 KB |
Output is correct |
22 |
Correct |
319 ms |
29224 KB |
Output is correct |
23 |
Correct |
319 ms |
29104 KB |
Output is correct |
24 |
Correct |
347 ms |
29336 KB |
Output is correct |
25 |
Correct |
426 ms |
26572 KB |
Output is correct |
26 |
Correct |
425 ms |
26592 KB |
Output is correct |
27 |
Correct |
386 ms |
28768 KB |
Output is correct |
28 |
Correct |
351 ms |
29016 KB |
Output is correct |
29 |
Correct |
361 ms |
28540 KB |
Output is correct |
30 |
Correct |
345 ms |
28748 KB |
Output is correct |
31 |
Correct |
382 ms |
28968 KB |
Output is correct |
32 |
Correct |
327 ms |
25204 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |