#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sz(v) (int)v.size()
#define sum_of(v) accumulate(all(v), 0ll)
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vl = vector<ll>;
using vd = vector<db>;
using vpi = vector<pi>;
using vpl = vector<pl>;
void setIO(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
}
const int inf = 1e9 + 10;
int ceil_div(int a, int b){
return (a + b - 1) / b;
}
int main(){
setIO();
int N, Q;
cin >> N >> Q;
vi d(N+1);
FOR(i, 1, N+1) cin >> d[i];
vl step(N+1);
step[1] = d[1];
FOR(i, 2, N+1){
if(step[i-1] == inf){
step[i] = inf;
continue;
}
step[i] = min(1LL * inf, step[i-1] * ceil_div(d[i], step[i-1]));
}
vi value = {step[1]};
vector<vi> position = {{-1}};
FOR(i, 2, N+1){
if(step[i] == inf) break;
if(step[i] != step[i-1]) value.pb(step[i]), position.pb({});
position.back().pb(-i);
}
FOR(i, 0, sz(position)){
reverse(all(position[i]));
}
//Person i : when T = k * step[i] => position = k * step[i] - i for 1 <= i <= N
//number of distinct values of step[] <= log2(1e9)
while(Q--){
int T, L, R;
cin >> T >> L >> R;
int ans = L <= T && T <= R;
FOR(i, 0, sz(value)){
int t = T / value[i];
t *= value[i];
int l = lower_bound(all(position[i]), L - t) - position[i].begin();
int r = upper_bound(all(position[i]), R - t) - position[i].begin();
ans += r - l;
}
cout << ans << '\n';
}
return 0;
}
/*
First one :
- For T = kD[1] : X[1] = kD[1]-1
Second one :
k * D[1] - 1 - (-2) >= D[2] + 1
<=> k >= ceil(D[2] / D[1])
<=> k = ceil(D[2] / D[1]) (for the first time)
<=> X[2] = ceil(D[2] / D[1]) * D[1] - 2
the second time person 2nd jumps
k' * D[1] - 1 - (ceil(D[2] / D[1]) * D[1] - 2) >= D[2] + 1
k' * D[1] >= (ceil(D[2] / D[1]) * D[1] + D[2])
=> k' >= 2* ceil(D[2] / D[1])
... (same for the k-th times)
generally : t = k * ceil(D[2] / D[1]) * D[1], position = k * ceil(D[2] / D[1]) * D[1] - 2
step[2] = ceil(D[2] / D[1]) * D[1]
Third one :
- For T = k * step[2] : second person : k * step[2] - 2
k * step[2] - 2 - (-3) >= D[3] + 1
<=> k * step[2] >= D[3]
<=> k >= ceil(D[3] / step[2])
first jump : k = ceil(D[3] / step[2]) * step[2]
*/
Compilation message (stderr)
worst_reporter3.cpp: In function 'int main()':
worst_reporter3.cpp:86:26: warning: narrowing conversion of 'step.std::vector<long long int>::operator[](1)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
86 | vi value = {step[1]};
| ^
worst_reporter3.cpp:86:26: warning: narrowing conversion of 'step.std::vector<long long int>::operator[](1)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |