#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
const int N = 5e5+7;
int mx[N << 2];
void build(int v, int tl, int tr, vector <int> &a) {
if (tl == tr) {
mx[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v * 2 + 1, tl, tm, a);
build(v * 2 + 2, tm + 1, tr, a);
mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}
int getl(int v, int tl, int tr, int l, int r, int x) {
if (tr < l || r < tl || mx[v] <= x)
return -1;
if (tl == tr)
return tl;
int tm = (tl + tr) >> 1;
int t = getl(v * 2 + 1, tl, tm, l, r, x);
if (t != -1)
return t;
else
return getl(v * 2 + 2, tm + 1, tr, l, r, x);
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n, q;
cin >> n >> q;
vector <int> d(n);
for (int i = 0; i < n; ++i)
cin >> d[i];
build(0, 0, n - 1, d);
while (q--) {
int t, l, r;
cin >> t >> l >> r;
int ans = 0;
ans += l <= t && t <= r;
int step = 1, cnt = t, ptr = 0;
while (ptr < n) {
int i = getl(0, 0, n - 1, ptr, n - 1, step);
if (i == -1)
i = n;
/*
for (int j = ptr; j < i; ++j) {
int to = -j-1+cnt*step;
ans += l <= to && to <= r;
}
*/
//l <= cnt*step-j-1
//j <= cnt*step-l-1
//cnt*step-j-1 <= r
//cnt*step-r-1 <= j
int l1 = max(cnt*step-r-1, ptr);
int r1 = min(cnt*step-l-1, i-1);
if (l1 <= r1)
ans += r1 - l1 + 1;
if (i == n)
break;
int x = (d[i]+step-1)/step;
int step1 = step * x;
int cnt1 = cnt/x;
int to = -i-1 + cnt1 * step1;
ans += l <= to && to <= r;
tie(step, cnt) = mp(step1, cnt1);
ptr = i+1;
}
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
29816 KB |
Output is correct |
2 |
Correct |
288 ms |
31096 KB |
Output is correct |
3 |
Correct |
299 ms |
31072 KB |
Output is correct |
4 |
Correct |
288 ms |
30840 KB |
Output is correct |
5 |
Correct |
295 ms |
30968 KB |
Output is correct |
6 |
Correct |
307 ms |
31060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
29816 KB |
Output is correct |
2 |
Correct |
288 ms |
31096 KB |
Output is correct |
3 |
Correct |
299 ms |
31072 KB |
Output is correct |
4 |
Correct |
288 ms |
30840 KB |
Output is correct |
5 |
Correct |
295 ms |
30968 KB |
Output is correct |
6 |
Correct |
307 ms |
31060 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
471 ms |
29432 KB |
Output is correct |
14 |
Correct |
474 ms |
30328 KB |
Output is correct |
15 |
Correct |
418 ms |
28792 KB |
Output is correct |
16 |
Correct |
479 ms |
29304 KB |
Output is correct |
17 |
Correct |
1518 ms |
19164 KB |
Output is correct |
18 |
Correct |
1512 ms |
19636 KB |
Output is correct |
19 |
Correct |
1654 ms |
17272 KB |
Output is correct |
20 |
Correct |
1601 ms |
20048 KB |
Output is correct |
21 |
Correct |
1519 ms |
19448 KB |
Output is correct |
22 |
Correct |
1526 ms |
17912 KB |
Output is correct |
23 |
Correct |
1509 ms |
21752 KB |
Output is correct |
24 |
Correct |
1502 ms |
17780 KB |
Output is correct |
25 |
Correct |
363 ms |
30968 KB |
Output is correct |
26 |
Correct |
357 ms |
30968 KB |
Output is correct |
27 |
Correct |
1464 ms |
33272 KB |
Output is correct |
28 |
Correct |
1771 ms |
32188 KB |
Output is correct |
29 |
Correct |
1754 ms |
33144 KB |
Output is correct |
30 |
Correct |
1790 ms |
33144 KB |
Output is correct |
31 |
Correct |
1856 ms |
33428 KB |
Output is correct |
32 |
Correct |
353 ms |
29564 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |