69122023-12-19 17:15:42111Jó intervallumokcpp17Accepted 100/100140ms21740 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

struct Fenwick {
	vector<int> v;
 
	Fenwick(int n) : v(n) {
	}
 
	void add(int i, int x) {
		for (int j = i; j < v.size(); j += j & -j) {
			v[j] += x;
		}
	}
 
	int sum(int i) {
		int x = 0;
		for (int j = i; j > 0; j -= j & -j) {
			x += v[j];
		}
		return x;
	}
};


signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	while (T--) {
		int N;
		cin >> N;
		vector<int> v(N + 1);
		for (int i = 1; i <= N; i++) {
			cin >> v[i];
		}
		vector<int> d(N + 1, 1);
		vector<int> e(N + 1);
		for (int i = 1; i <= N; i++) {
			while (i + d[i] <= N && v[i + d[i]] % (d[i] + 1) == 0) {
				d[i]++;
			}
			e[i + d[i] - 1]++;
		}
		Fenwick f(N + 1);
		for (int i = 1, s = 0; i <= N; i++) {
			s++;
			f.add(i, s);
			s -= e[i];
		}
		int Q;
		cin >> Q;
		vector<vector<pii>> w(N + 1);
		for (int i = 0; i < Q; i++) {
			int L, R;
			cin >> L >> R;
			w[L].push_back({R, i});
		}
		vector<int> ans(Q);
		for (int i = 1, s = 0, c = 0; i <= N; i++) {
			for (auto [j, q] : w[i]) {
				ans[q] = f.sum(j) - f.sum(i - 1);
			}
			for (int j = i; j < i + d[i]; j++) {
				f.add(j, -1);
			}
		}
		for (int i = 0; i < Q; i++) {
			cout << ans[i] << '\n';
		}
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
subtask210/10
2Accepted3ms2024 KiB
3Accepted3ms2236 KiB
4Accepted3ms2452 KiB
5Accepted3ms2548 KiB
6Accepted3ms2648 KiB
subtask320/20
7Accepted32ms3592 KiB
8Accepted32ms4096 KiB
9Accepted34ms5112 KiB
10Accepted34ms6268 KiB
11Accepted37ms9604 KiB
subtask430/30
12Accepted63ms5248 KiB
13Accepted83ms7052 KiB
14Accepted67ms9396 KiB
15Accepted90ms12584 KiB
16Accepted96ms21512 KiB
subtask540/40
17Accepted68ms5624 KiB
18Accepted83ms7432 KiB
19Accepted71ms9832 KiB
20Accepted90ms12996 KiB
21Accepted140ms21740 KiB