100742024-03-26 12:23:05CWMBarátokcpp17Accepted 100/100134ms24736 KiB
#include <iostream>
#include <optional>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
#define int long long

struct fenwick_tree {
	vector<int> fwt;
	vector<int> base;
	fenwick_tree(vector<int>& input) {
		base = input;
		vector<int> prefixSum(input.size()+1);
		prefixSum[0] = 0;
		for (size_t i = 1; i < input.size()+1; i++)
		{
			prefixSum[i] = prefixSum[i - 1] + input[i-1];
		}
		fwt.push_back(prefixSum[0]);
		for (size_t i = 1; i < input.size()+1; i++)
		{
			int range = i - (i & (i - 1));
			fwt.push_back(prefixSum[i] - prefixSum[i - range]);
		}
	}
	int sumRange(int beg, int end) {
		end++;
		return sumFromBeginning(end) - sumFromBeginning(beg);
	}
	int sumFromBeginning(int end) {
		int sum = 0;
		while (end != 0) {
			sum += fwt[end];
			end = (end & (end - 1));
		}
		return sum;
	}
	void update(int idx, int newVal) {
		int dif = newVal - base[idx];
		base[idx] = newVal;
		idx++;
		while (idx < fwt.size()) {
			fwt[idx] += dif;
			int range = idx - (idx & (idx - 1));
			idx = (idx & (idx - 1)) + 2*range;
		}
	}
};

signed main()
{
	int n;
	cin >> n;
	vector<int> inp(n,1);
	vector<pair<int, int>> sVec(n);
	for (size_t i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		sVec[i] = { a,i };
	}
	sort(sVec.begin(), sVec.end());
	fenwick_tree fw = fenwick_tree(inp);
	int ans = 0;
	for (size_t i = 0; i < n; i++)
	{
		fw.update(sVec[i].second, 0);
		int beg = max(0ll, sVec[i].second - sVec[i].first);
		int end = min(n-1, sVec[i].second + sVec[i].first);
		ans += fw.sumRange(beg, end);
	}
	cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Accepted76ms13776 KiB
subtask211/11
3Accepted3ms2304 KiB
4Accepted4ms3136 KiB
5Accepted4ms3444 KiB
6Accepted4ms3396 KiB
7Accepted4ms3620 KiB
8Accepted4ms3972 KiB
9Accepted4ms3924 KiB
10Accepted4ms3936 KiB
subtask312/12
11Accepted111ms23284 KiB
12Accepted101ms23312 KiB
13Accepted111ms23568 KiB
14Accepted75ms23592 KiB
15Accepted108ms23452 KiB
16Accepted76ms23740 KiB
17Accepted104ms23664 KiB
subtask431/31
18Accepted97ms23692 KiB
19Accepted108ms23664 KiB
20Accepted70ms23952 KiB
21Accepted103ms23860 KiB
22Accepted127ms24156 KiB
23Accepted126ms24140 KiB
24Accepted134ms24076 KiB
25Accepted126ms24248 KiB
26Accepted131ms24184 KiB
subtask546/46
27Accepted111ms24204 KiB
28Accepted119ms24488 KiB
29Accepted120ms24520 KiB
30Accepted79ms24520 KiB
31Accepted126ms24480 KiB
32Accepted72ms24448 KiB
33Accepted128ms24492 KiB
34Accepted128ms24488 KiB
35Accepted128ms24432 KiB
36Accepted71ms24416 KiB
37Accepted129ms24408 KiB
38Accepted123ms24408 KiB
39Accepted123ms24412 KiB
40Accepted128ms24548 KiB
41Accepted126ms24736 KiB