Submission #209415

# Submission time Handle Problem Language Result Execution time Memory
209415 2020-03-14T07:19:05 Z model_code Mountains (NOI20_mountains) C++17
100 / 100
599 ms 40312 KB
#include<bits/stdc++.h>
using namespace std;
struct node{
	int s, e, m, cnt;
	node *l, *r;
	node(int _s, int _e): s(_s), e(_e), m((_s+_e)/2), cnt(0){
		if(s == e) return;
		l = new node(s, m);
		r = new node(m+1, e);
	}
	void update(int x, int v){
		cnt += v;
		if(s == e) return;
		if(x <= m) l->update(x, v);
		else r->update(x, v);
	}
	int query(int x, int y){
		if(x <= s && y >= e) return cnt;
		int ret = 0;
		if(x <= m) ret += l->query(x, y);
		if(y >= m+1) ret += r->query(x, y);
		return ret;
	}
	void clear(){
		cnt = 0;
		if(s == e) return;
		l->clear();
		r->clear();
	}
};

long long invLeft[300001], invRight[300001];
pair<long long, int> disArr[300001];
int cnt = 1;
long long solve(int n, long long H[]){
	for(int i = 1; i <= n; i++) disArr[i] = {H[i], i};
	sort(disArr + 1, disArr + n + 1);

	for(int i = 1; i <= n; i++){
		if (i > 1 && disArr[i].first != disArr[i-1].first) cnt++;
		H[disArr[i].second] = cnt;
	}
	
	node *root = new node(0, n);
	for(int i = 1; i <= n; i++){
		invLeft[i] = root->query(0, H[i] - 1);
		root->update(H[i], 1);
	}
	
	root->clear();
	for(int i = n; i > 0; i--){
		invRight[i] = root->query(0, H[i] - 1);
		root->update(H[i], 1);
	}
	
	long long total = 0;
	for(int i = 1; i <= n; i++){
		total += invLeft[i] * invRight[i];
	}
	
	return total;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	long long H[300001];
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> H[i];
	long long total = solve(n, H);
	cout << total;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 211 ms 40184 KB Output is correct
3 Correct 217 ms 40184 KB Output is correct
4 Correct 214 ms 40288 KB Output is correct
5 Correct 225 ms 40184 KB Output is correct
6 Correct 218 ms 40192 KB Output is correct
7 Correct 215 ms 40312 KB Output is correct
8 Correct 229 ms 40184 KB Output is correct
9 Correct 230 ms 40184 KB Output is correct
10 Correct 212 ms 40312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 40232 KB Output is correct
2 Correct 179 ms 40188 KB Output is correct
3 Correct 172 ms 40184 KB Output is correct
4 Correct 164 ms 40184 KB Output is correct
5 Correct 173 ms 40204 KB Output is correct
6 Correct 188 ms 40312 KB Output is correct
7 Correct 172 ms 40184 KB Output is correct
8 Correct 151 ms 40304 KB Output is correct
9 Correct 147 ms 40184 KB Output is correct
10 Correct 5 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 40232 KB Output is correct
2 Correct 179 ms 40188 KB Output is correct
3 Correct 172 ms 40184 KB Output is correct
4 Correct 164 ms 40184 KB Output is correct
5 Correct 173 ms 40204 KB Output is correct
6 Correct 188 ms 40312 KB Output is correct
7 Correct 172 ms 40184 KB Output is correct
8 Correct 151 ms 40304 KB Output is correct
9 Correct 147 ms 40184 KB Output is correct
10 Correct 5 ms 380 KB Output is correct
11 Correct 207 ms 40288 KB Output is correct
12 Correct 213 ms 40292 KB Output is correct
13 Correct 200 ms 40184 KB Output is correct
14 Correct 200 ms 40208 KB Output is correct
15 Correct 205 ms 40240 KB Output is correct
16 Correct 205 ms 40304 KB Output is correct
17 Correct 198 ms 40188 KB Output is correct
18 Correct 143 ms 40184 KB Output is correct
19 Correct 155 ms 40288 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 7 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 7 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 13 ms 1656 KB Output is correct
12 Correct 14 ms 1624 KB Output is correct
13 Correct 13 ms 1656 KB Output is correct
14 Correct 13 ms 1656 KB Output is correct
15 Correct 16 ms 1656 KB Output is correct
16 Correct 13 ms 1656 KB Output is correct
17 Correct 13 ms 1656 KB Output is correct
18 Correct 13 ms 1656 KB Output is correct
19 Correct 11 ms 1656 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 40232 KB Output is correct
2 Correct 179 ms 40188 KB Output is correct
3 Correct 172 ms 40184 KB Output is correct
4 Correct 164 ms 40184 KB Output is correct
5 Correct 173 ms 40204 KB Output is correct
6 Correct 188 ms 40312 KB Output is correct
7 Correct 172 ms 40184 KB Output is correct
8 Correct 151 ms 40304 KB Output is correct
9 Correct 147 ms 40184 KB Output is correct
10 Correct 5 ms 380 KB Output is correct
11 Correct 207 ms 40288 KB Output is correct
12 Correct 213 ms 40292 KB Output is correct
13 Correct 200 ms 40184 KB Output is correct
14 Correct 200 ms 40208 KB Output is correct
15 Correct 205 ms 40240 KB Output is correct
16 Correct 205 ms 40304 KB Output is correct
17 Correct 198 ms 40188 KB Output is correct
18 Correct 143 ms 40184 KB Output is correct
19 Correct 155 ms 40288 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 396 ms 40184 KB Output is correct
22 Correct 414 ms 40184 KB Output is correct
23 Correct 398 ms 40312 KB Output is correct
24 Correct 388 ms 40184 KB Output is correct
25 Correct 390 ms 40312 KB Output is correct
26 Correct 396 ms 40184 KB Output is correct
27 Correct 421 ms 40288 KB Output is correct
28 Correct 181 ms 40184 KB Output is correct
29 Correct 201 ms 40184 KB Output is correct
30 Correct 5 ms 404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 211 ms 40184 KB Output is correct
3 Correct 217 ms 40184 KB Output is correct
4 Correct 214 ms 40288 KB Output is correct
5 Correct 225 ms 40184 KB Output is correct
6 Correct 218 ms 40192 KB Output is correct
7 Correct 215 ms 40312 KB Output is correct
8 Correct 229 ms 40184 KB Output is correct
9 Correct 230 ms 40184 KB Output is correct
10 Correct 212 ms 40312 KB Output is correct
11 Correct 175 ms 40232 KB Output is correct
12 Correct 179 ms 40188 KB Output is correct
13 Correct 172 ms 40184 KB Output is correct
14 Correct 164 ms 40184 KB Output is correct
15 Correct 173 ms 40204 KB Output is correct
16 Correct 188 ms 40312 KB Output is correct
17 Correct 172 ms 40184 KB Output is correct
18 Correct 151 ms 40304 KB Output is correct
19 Correct 147 ms 40184 KB Output is correct
20 Correct 5 ms 380 KB Output is correct
21 Correct 207 ms 40288 KB Output is correct
22 Correct 213 ms 40292 KB Output is correct
23 Correct 200 ms 40184 KB Output is correct
24 Correct 200 ms 40208 KB Output is correct
25 Correct 205 ms 40240 KB Output is correct
26 Correct 205 ms 40304 KB Output is correct
27 Correct 198 ms 40188 KB Output is correct
28 Correct 143 ms 40184 KB Output is correct
29 Correct 155 ms 40288 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 376 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 7 ms 376 KB Output is correct
35 Correct 5 ms 376 KB Output is correct
36 Correct 5 ms 376 KB Output is correct
37 Correct 5 ms 504 KB Output is correct
38 Correct 5 ms 376 KB Output is correct
39 Correct 5 ms 376 KB Output is correct
40 Correct 5 ms 376 KB Output is correct
41 Correct 13 ms 1656 KB Output is correct
42 Correct 14 ms 1624 KB Output is correct
43 Correct 13 ms 1656 KB Output is correct
44 Correct 13 ms 1656 KB Output is correct
45 Correct 16 ms 1656 KB Output is correct
46 Correct 13 ms 1656 KB Output is correct
47 Correct 13 ms 1656 KB Output is correct
48 Correct 13 ms 1656 KB Output is correct
49 Correct 11 ms 1656 KB Output is correct
50 Correct 5 ms 376 KB Output is correct
51 Correct 396 ms 40184 KB Output is correct
52 Correct 414 ms 40184 KB Output is correct
53 Correct 398 ms 40312 KB Output is correct
54 Correct 388 ms 40184 KB Output is correct
55 Correct 390 ms 40312 KB Output is correct
56 Correct 396 ms 40184 KB Output is correct
57 Correct 421 ms 40288 KB Output is correct
58 Correct 181 ms 40184 KB Output is correct
59 Correct 201 ms 40184 KB Output is correct
60 Correct 5 ms 404 KB Output is correct
61 Correct 588 ms 40284 KB Output is correct
62 Correct 599 ms 40184 KB Output is correct
63 Correct 565 ms 40184 KB Output is correct
64 Correct 586 ms 40208 KB Output is correct
65 Correct 555 ms 40184 KB Output is correct
66 Correct 560 ms 40288 KB Output is correct
67 Correct 564 ms 40296 KB Output is correct
68 Correct 488 ms 40288 KB Output is correct
69 Correct 445 ms 40244 KB Output is correct
70 Correct 5 ms 376 KB Output is correct