This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |