Submission #224031

# Submission time Handle Problem Language Result Execution time Memory
224031 2020-04-17T05:28:02 Z crimsonred Mountains (NOI20_mountains) C++17
100 / 100
1751 ms 55520 KB
#include <bits/stdc++.h>
using namespace std;

#define mod 1'000'000'007
typedef long long ll;

template<typename T>
void deb(initializer_list<T> l)
{
	for (auto &e : l)
		cout << e << ' ';
	cout << endl;
}

struct SegmentTree
{
	vector<ll> st;

	void build(vector<ll>& a, ll n)
	{
		st.resize(2 * n, 0);
		for (ll i = n; i < 2 * n; ++i)
			st[i] = a[i - n];
		for (ll i = n - 1; i > 0; --i)
			st[i] = st[2 * i] + st[2 * i + 1];
	}

	void update(ll idx, ll val, ll n)
	{
		idx += n;
		st[idx] += val;
		for (idx /= 2; idx; idx /= 2)
			st[idx] = st[2 * idx] + st[2 * idx + 1];
	}

	ll query(ll left, ll right, ll n)
	{
		left += n;
		right += n;
		ll sum = 0;
		while (left < right)
		{
			if (left % 2 == 1)
			{
				sum += st[left];
				++left;
			}
			if (right % 2 == 1)
			{
				--right;
				sum += st[right];
			}
			left /= 2;
			right /= 2;
		}
		return sum;
	}
};

void solve()
{
	ll n;
	cin >> n;
	vector<ll> a(n);
	set<ll> s;
	for (auto &e : a)
	{
		cin >> e;
		s.insert(e);
	}

	map<ll, ll> m;
	ll num = 0;
	for (auto &e : s)
		m[e] = num++;

	vector<ll> left(n, 0), right(n, 0);
	++left[m[a[0]]];
	for (ll i = 1; i < n; ++i)
		++right[m[a[i]]];

	SegmentTree l, r;
	l.build(left, n);
	r.build(right, n);

	ll ans = 0;
	for (ll i = 1; i < n - 1; ++i)
	{
		r.update(m[a[i]], -1, n);
		ans += l.query(0, m[a[i]], n) * r.query(0, m[a[i]], n);
		l.update(m[a[i]], 1, n);
	}
	cout << ans << '\n';
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	// cin >> t;
	while (t--)
		solve();

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 463 ms 55224 KB Output is correct
3 Correct 460 ms 55292 KB Output is correct
4 Correct 456 ms 55288 KB Output is correct
5 Correct 473 ms 55160 KB Output is correct
6 Correct 476 ms 55160 KB Output is correct
7 Correct 489 ms 55288 KB Output is correct
8 Correct 483 ms 55160 KB Output is correct
9 Correct 394 ms 39416 KB Output is correct
10 Correct 391 ms 39416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 17400 KB Output is correct
2 Correct 67 ms 17408 KB Output is correct
3 Correct 64 ms 17408 KB Output is correct
4 Correct 65 ms 17400 KB Output is correct
5 Correct 72 ms 17400 KB Output is correct
6 Correct 61 ms 17400 KB Output is correct
7 Correct 67 ms 17408 KB Output is correct
8 Correct 59 ms 17428 KB Output is correct
9 Correct 62 ms 17408 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 17400 KB Output is correct
2 Correct 67 ms 17408 KB Output is correct
3 Correct 64 ms 17408 KB Output is correct
4 Correct 65 ms 17400 KB Output is correct
5 Correct 72 ms 17400 KB Output is correct
6 Correct 61 ms 17400 KB Output is correct
7 Correct 67 ms 17408 KB Output is correct
8 Correct 59 ms 17428 KB Output is correct
9 Correct 62 ms 17408 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 125 ms 17656 KB Output is correct
12 Correct 127 ms 17656 KB Output is correct
13 Correct 127 ms 17656 KB Output is correct
14 Correct 129 ms 17656 KB Output is correct
15 Correct 126 ms 17656 KB Output is correct
16 Correct 126 ms 17656 KB Output is correct
17 Correct 128 ms 17656 KB Output is correct
18 Correct 86 ms 17656 KB Output is correct
19 Correct 81 ms 17660 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 22 ms 2176 KB Output is correct
12 Correct 24 ms 2176 KB Output is correct
13 Correct 20 ms 2176 KB Output is correct
14 Correct 20 ms 2176 KB Output is correct
15 Correct 20 ms 2176 KB Output is correct
16 Correct 22 ms 2176 KB Output is correct
17 Correct 20 ms 2176 KB Output is correct
18 Correct 17 ms 1536 KB Output is correct
19 Correct 13 ms 1024 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 17400 KB Output is correct
2 Correct 67 ms 17408 KB Output is correct
3 Correct 64 ms 17408 KB Output is correct
4 Correct 65 ms 17400 KB Output is correct
5 Correct 72 ms 17400 KB Output is correct
6 Correct 61 ms 17400 KB Output is correct
7 Correct 67 ms 17408 KB Output is correct
8 Correct 59 ms 17428 KB Output is correct
9 Correct 62 ms 17408 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 125 ms 17656 KB Output is correct
12 Correct 127 ms 17656 KB Output is correct
13 Correct 127 ms 17656 KB Output is correct
14 Correct 129 ms 17656 KB Output is correct
15 Correct 126 ms 17656 KB Output is correct
16 Correct 126 ms 17656 KB Output is correct
17 Correct 128 ms 17656 KB Output is correct
18 Correct 86 ms 17656 KB Output is correct
19 Correct 81 ms 17660 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 746 ms 29048 KB Output is correct
22 Correct 947 ms 28792 KB Output is correct
23 Correct 940 ms 28984 KB Output is correct
24 Correct 774 ms 28968 KB Output is correct
25 Correct 714 ms 29048 KB Output is correct
26 Correct 822 ms 29024 KB Output is correct
27 Correct 749 ms 28980 KB Output is correct
28 Correct 309 ms 28988 KB Output is correct
29 Correct 311 ms 28920 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 463 ms 55224 KB Output is correct
3 Correct 460 ms 55292 KB Output is correct
4 Correct 456 ms 55288 KB Output is correct
5 Correct 473 ms 55160 KB Output is correct
6 Correct 476 ms 55160 KB Output is correct
7 Correct 489 ms 55288 KB Output is correct
8 Correct 483 ms 55160 KB Output is correct
9 Correct 394 ms 39416 KB Output is correct
10 Correct 391 ms 39416 KB Output is correct
11 Correct 72 ms 17400 KB Output is correct
12 Correct 67 ms 17408 KB Output is correct
13 Correct 64 ms 17408 KB Output is correct
14 Correct 65 ms 17400 KB Output is correct
15 Correct 72 ms 17400 KB Output is correct
16 Correct 61 ms 17400 KB Output is correct
17 Correct 67 ms 17408 KB Output is correct
18 Correct 59 ms 17428 KB Output is correct
19 Correct 62 ms 17408 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 125 ms 17656 KB Output is correct
22 Correct 127 ms 17656 KB Output is correct
23 Correct 127 ms 17656 KB Output is correct
24 Correct 129 ms 17656 KB Output is correct
25 Correct 126 ms 17656 KB Output is correct
26 Correct 126 ms 17656 KB Output is correct
27 Correct 128 ms 17656 KB Output is correct
28 Correct 86 ms 17656 KB Output is correct
29 Correct 81 ms 17660 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 4 ms 384 KB Output is correct
41 Correct 22 ms 2176 KB Output is correct
42 Correct 24 ms 2176 KB Output is correct
43 Correct 20 ms 2176 KB Output is correct
44 Correct 20 ms 2176 KB Output is correct
45 Correct 20 ms 2176 KB Output is correct
46 Correct 22 ms 2176 KB Output is correct
47 Correct 20 ms 2176 KB Output is correct
48 Correct 17 ms 1536 KB Output is correct
49 Correct 13 ms 1024 KB Output is correct
50 Correct 4 ms 384 KB Output is correct
51 Correct 746 ms 29048 KB Output is correct
52 Correct 947 ms 28792 KB Output is correct
53 Correct 940 ms 28984 KB Output is correct
54 Correct 774 ms 28968 KB Output is correct
55 Correct 714 ms 29048 KB Output is correct
56 Correct 822 ms 29024 KB Output is correct
57 Correct 749 ms 28980 KB Output is correct
58 Correct 309 ms 28988 KB Output is correct
59 Correct 311 ms 28920 KB Output is correct
60 Correct 5 ms 384 KB Output is correct
61 Correct 1336 ms 55332 KB Output is correct
62 Correct 1328 ms 55264 KB Output is correct
63 Correct 1567 ms 55416 KB Output is correct
64 Correct 1362 ms 55520 KB Output is correct
65 Correct 1751 ms 55200 KB Output is correct
66 Correct 1358 ms 55260 KB Output is correct
67 Correct 1495 ms 55340 KB Output is correct
68 Correct 1202 ms 39416 KB Output is correct
69 Correct 1100 ms 32892 KB Output is correct
70 Correct 4 ms 384 KB Output is correct