105772024-04-05 21:00:37CWMBarátokcpp17Time limit exceeded 11/100699ms69364 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;
		}
	}
};

struct tNode {
	int val;
	int end;
	int beg;
	int par;
	tNode(int Val, int Beg, int End) {
		val = Val;
		beg = Beg;
		end = End;
		par = -1;
	}
	tNode() {};
};
struct segment_tree {
	vector<tNode> smt;
	int fullSize = 1;
	segment_tree(const vector<int>& inp) {
		for (size_t i = 0; i < 32; i++)
		{
			if (inp.size() > fullSize) fullSize *= 2;
			else break;
		}
		smt.resize(2 * fullSize - 1);
		for (size_t i = 0; i < inp.size(); i++)
		{
			smt[i] = tNode(inp[i],i,i);
		}
		int cur = 0;
		for (size_t i = fullSize; i < smt.size(); i++)
		{
			smt[i] = tNode(smt[cur].val + smt[cur + 1].val, smt[cur].beg, smt[cur + 1].end);
			smt[cur].par = i;
			smt[cur+1].par = i;
			cur += 2;
		}
	}
	int sumRange(int beg, int end) {
		int ans = 0;
		int cur = beg;
		while (true)
		{
			if (smt[cur].end==end) {
				ans += smt[cur].val;
				break;
			}
			tNode par = smt[smt[cur].par];
			if (par.beg >= cur && par.end <= end) {
				cur = smt[cur].par;
			}
			else {
				ans += smt[cur].val;
				cur = smt[cur].end + 1;
			}
		}
		return ans;
	}
	void update(int index, int value) {
		int cur = index;
		int dif = value - smt[cur].val;
		while (cur!=-1)
		{
			smt[cur].val += dif;
			cur = smt[cur].par;
		}
	}
};

signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	/*int n, q;
	cin >> n >> q;
	vector<int> inp(n);
	for (size_t i = 0; i < n; i++)
	{
		cin >> inp[i];
	}
	segment_tree sm = segment_tree(inp);
	for (size_t i = 0; i < q; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		if (a == 1) {
			sm.update(b, c);
		}
		else {
			c--;
			cout << sm.sumRange(b, c) << "\n";
		}
	}*/
	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());
	segment_tree st = segment_tree(inp);
	int ans = 0;
	for (size_t i = 0; i < n; i++)
	{
		st.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 += st.sumRange(beg, end);
	}
	cout << ans << "\n";
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Time limit exceeded699ms13172 KiB
subtask211/11
3Accepted3ms3024 KiB
4Accepted79ms4276 KiB
5Accepted61ms4416 KiB
6Accepted98ms4672 KiB
7Accepted21ms4652 KiB
8Accepted116ms5004 KiB
9Accepted6ms4912 KiB
10Accepted118ms4924 KiB
subtask30/12
11Time limit exceeded658ms46528 KiB
12Accepted296ms47164 KiB
13Time limit exceeded662ms26264 KiB
14Accepted94ms48292 KiB
15Time limit exceeded662ms27652 KiB
16Accepted68ms49956 KiB
17Time limit exceeded666ms29188 KiB
subtask40/31
18Accepted162ms51420 KiB
19Accepted407ms52320 KiB
20Accepted68ms52724 KiB
21Accepted402ms53508 KiB
22Time limit exceeded666ms33180 KiB
23Time limit exceeded686ms34220 KiB
24Time limit exceeded657ms35556 KiB
25Time limit exceeded657ms36580 KiB
26Time limit exceeded671ms38036 KiB
subtask50/46
27Time limit exceeded681ms38812 KiB
28Time limit exceeded662ms39616 KiB
29Time limit exceeded666ms40800 KiB
30Time limit exceeded671ms41152 KiB
31Time limit exceeded667ms42712 KiB
32Accepted398ms64792 KiB
33Time limit exceeded677ms44672 KiB
34Time limit exceeded662ms45832 KiB
35Time limit exceeded675ms47208 KiB
36Time limit exceeded643ms69364 KiB
37Time limit exceeded660ms49020 KiB
38Time limit exceeded651ms50060 KiB
39Time limit exceeded671ms51500 KiB
40Time limit exceeded643ms52840 KiB
41Time limit exceeded680ms54124 KiB