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 int
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define pb push_back
void solve() {
    int n, q; cin >> n >> q;
    int a[n+1], b[n+1];
    b[0] = 1;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = (a[i] + b[i - 1] - 1) / b[i - 1] * b[i - 1];
    }
    while (q--) {
        int t, l, r; cin >> t >> l >> r;
        int resr = n + 1, shift = 0;
        int T=t;
        while (t > 0) {
            int i = upper_bound(b,b+n+1,t)-b-1;
            int dist = t - (t % b[i]);
            t -= dist, shift += dist;
            resr = min(resr, n + 1 - i + max(r - (shift - i), -1LL));
        }
        int resl = n + 1;
        shift = 0;
        t=T;
        while (t > 0) {
            int i = upper_bound(b,b+n+1,t)-b-1;
            int dist = t - (t % b[i]);
            t -= dist, shift += dist;
            resl = min(resl, n + 1 - i + max(l-1 - (shift - i), -1LL));
        }
        cout << resr - resl << '\n';
    }
    return;
}
 
int32_t main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int tt=1; //cin>>tt;
	while(tt--) {
	    solve();
	}
	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... |