100732024-03-26 12:17:29CWMBarátokcpp17Wrong answer 23/100120ms43272 KiB
#include <iostream>
#include <optional>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

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(0, 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
2Wrong answer70ms8716 KiB
subtask211/11
3Accepted3ms2884 KiB
4Accepted4ms3472 KiB
5Accepted4ms3724 KiB
6Accepted4ms4052 KiB
7Accepted4ms4296 KiB
8Accepted4ms4560 KiB
9Accepted4ms4552 KiB
10Accepted4ms4584 KiB
subtask312/12
11Accepted101ms14956 KiB
12Accepted96ms15576 KiB
13Accepted101ms16452 KiB
14Accepted68ms16892 KiB
15Accepted101ms17812 KiB
16Accepted68ms18216 KiB
17Accepted97ms18996 KiB
subtask40/31
18Accepted92ms19520 KiB
19Accepted101ms20376 KiB
20Accepted65ms20772 KiB
21Accepted94ms21664 KiB
22Wrong answer118ms22828 KiB
23Wrong answer115ms23856 KiB
24Wrong answer120ms25264 KiB
25Accepted115ms26664 KiB
26Wrong answer118ms28152 KiB
subtask50/46
27Accepted101ms28852 KiB
28Accepted111ms29708 KiB
29Accepted114ms30868 KiB
30Accepted72ms31368 KiB
31Wrong answer116ms32584 KiB
32Accepted65ms33020 KiB
33Wrong answer116ms34360 KiB
34Wrong answer116ms35592 KiB
35Wrong answer115ms36780 KiB
36Accepted64ms37272 KiB
37Wrong answer116ms38544 KiB
38Accepted114ms39732 KiB
39Wrong answer115ms40740 KiB
40Wrong answer119ms42184 KiB
41Wrong answer114ms43272 KiB