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;
#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;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |