A - Triangular Number Editorial by newsname


This problem can be solved with a Fenwick Tree.

code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t[105];
int n;
void ad(int i, ll v) {
	for (; i <= n; i += i & -i) t[i] += v;
}
ll sm(int i) {
	ll r = 0;
	for (; i; i -= i & -i) r += t[i];
	return r;
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)  ad(i, i);
	cout << sm(n);
	return 0;
}

posted:
last update: