답안 #1104612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104612 2024-10-24T07:08:42 Z Shadow1 Mountains (NOI20_mountains) C++17
100 / 100
685 ms 48228 KB
// Programmer: Shadow1

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!

#define i64 int64_t
#define watch(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << " ";}cout << '\n';
#define vt vector
#define st stack
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


// T: O(n log n)
// M : O(n + k log n)

class FenwickTree {
private:
	vector<int64_t> ft;
public:
	FenwickTree(int m) {  // Create empty Fenwick Tree
		ft.assign(m+1,0);
	}

	int LSOne(int x) {    // Return Least significant 1-bit of x
		return ((x) & (-x));
	}
	void build(const vector<int64_t> &f) {  // Build the fenwick tree array from the frequency array
		int m = sz(f) - 1;
		ft.assign(m+1,0);
		for(int i=1; i<=m; ++i) {
			ft[i] += f[i];
			if(i + LSOne(i) <= m)
				ft[i+LSOne(i)] += ft[i];
		}
	}

	FenwickTree(int m, const vector<int64_t> &s) {   // Convert the array to a frequency array with m integer keys
		vector<int64_t> f(m+1,0);						// i.e. the array's maximum value must be m
		for(int i=0; i<sz(s); ++i)
			++f[s[i]];
	}

	int64_t rsq(int i, int j) {  // i <= j   // O(log m)
		int64_t sum = 0;
		while(j > 0) {
			sum += ft[j];
			j -= LSOne(j);
		}
		--i;
		while(i > 0) {
			sum -= ft[i];
			i -= LSOne(i);
		}
		return sum;
	}

	void update(int i, int64_t v) {
		for(; i<sz(ft); i+=LSOne(i))   // O(log m)
			ft[i] += v;
	}

};


void solve() {
	int n;
	cin >> n;
	vector<int64_t> h(n), cnt(n+1), p(n+1); // cnt[i] is the number of ith smallest elements.
	set<int64_t> s;
	map<int64_t, int> rank;

	for(int i=0; i<n; ++i) {  // O(n log n)
		cin >> h[i];
		s.insert(h[i]);
	}
	int k = 1;
	for(auto &S : s) {  // Worse case O(n log n)
		rank[S] = k;     // S is the kth smallest element
		++k;
	}
	--k;
	assert(k <= n);
	for(auto &H : h) 
		cnt[rank[H]]++;   
	
	p[1] = cnt[1];
	for(int i=2; i<=k; ++i)
		p[i] = p[i-1] + cnt[i];

	// Algorithm starts: 
	FenwickTree ft(k);  // ft[i] will contain the number of ith smallest elements
	int64_t ans = 0;
	
	ft.update(rank[h[0]],1);
	for(int i=1; i<n-1; ++i) {
		int64_t x = ft.rsq(1,rank[h[i]]-1);    // O(log n)	
		int64_t y = p[rank[h[i]]-1];
		ans += (x * (y - x));
		ft.update(rank[h[i]],1);  // update
	}
	cout << ans << '\n';
}

int main() {
	// freopen("output.txt", "w", stdout);
	// freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(NULL); 
	
	solve();
	return 0;
}

/* CHECK :
1. COMPARATOR FUNCTION MUST RETURN FALSE WHEN ARGUMENTS ARE EQUAL!!!
2. Overflow! Typecase int64_t on operations if varaibles are int
3. Check array bounds!!!
4. Check array indexing!!!
5. Edge cases. (N==1)!!!
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 260 ms 48200 KB Output is correct
3 Correct 263 ms 48200 KB Output is correct
4 Correct 264 ms 48200 KB Output is correct
5 Correct 269 ms 48228 KB Output is correct
6 Correct 262 ms 48200 KB Output is correct
7 Correct 269 ms 48212 KB Output is correct
8 Correct 281 ms 48200 KB Output is correct
9 Correct 203 ms 31304 KB Output is correct
10 Correct 212 ms 31304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8016 KB Output is correct
2 Correct 19 ms 8016 KB Output is correct
3 Correct 20 ms 8016 KB Output is correct
4 Correct 19 ms 7944 KB Output is correct
5 Correct 20 ms 8016 KB Output is correct
6 Correct 19 ms 8016 KB Output is correct
7 Correct 18 ms 8016 KB Output is correct
8 Correct 16 ms 8016 KB Output is correct
9 Correct 17 ms 8016 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8016 KB Output is correct
2 Correct 19 ms 8016 KB Output is correct
3 Correct 20 ms 8016 KB Output is correct
4 Correct 19 ms 7944 KB Output is correct
5 Correct 20 ms 8016 KB Output is correct
6 Correct 19 ms 8016 KB Output is correct
7 Correct 18 ms 8016 KB Output is correct
8 Correct 16 ms 8016 KB Output is correct
9 Correct 17 ms 8016 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 45 ms 8272 KB Output is correct
12 Correct 45 ms 8144 KB Output is correct
13 Correct 47 ms 8272 KB Output is correct
14 Correct 45 ms 8440 KB Output is correct
15 Correct 45 ms 8272 KB Output is correct
16 Correct 45 ms 8272 KB Output is correct
17 Correct 45 ms 8272 KB Output is correct
18 Correct 23 ms 8272 KB Output is correct
19 Correct 23 ms 8272 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 10 ms 1872 KB Output is correct
12 Correct 11 ms 1872 KB Output is correct
13 Correct 11 ms 2060 KB Output is correct
14 Correct 10 ms 1872 KB Output is correct
15 Correct 11 ms 1872 KB Output is correct
16 Correct 10 ms 2060 KB Output is correct
17 Correct 11 ms 1872 KB Output is correct
18 Correct 8 ms 1360 KB Output is correct
19 Correct 3 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8016 KB Output is correct
2 Correct 19 ms 8016 KB Output is correct
3 Correct 20 ms 8016 KB Output is correct
4 Correct 19 ms 7944 KB Output is correct
5 Correct 20 ms 8016 KB Output is correct
6 Correct 19 ms 8016 KB Output is correct
7 Correct 18 ms 8016 KB Output is correct
8 Correct 16 ms 8016 KB Output is correct
9 Correct 17 ms 8016 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 45 ms 8272 KB Output is correct
12 Correct 45 ms 8144 KB Output is correct
13 Correct 47 ms 8272 KB Output is correct
14 Correct 45 ms 8440 KB Output is correct
15 Correct 45 ms 8272 KB Output is correct
16 Correct 45 ms 8272 KB Output is correct
17 Correct 45 ms 8272 KB Output is correct
18 Correct 23 ms 8272 KB Output is correct
19 Correct 23 ms 8272 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 374 ms 20300 KB Output is correct
22 Correct 333 ms 20296 KB Output is correct
23 Correct 362 ms 20372 KB Output is correct
24 Correct 347 ms 20296 KB Output is correct
25 Correct 326 ms 20312 KB Output is correct
26 Correct 306 ms 20296 KB Output is correct
27 Correct 334 ms 20160 KB Output is correct
28 Correct 132 ms 20296 KB Output is correct
29 Correct 135 ms 20300 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 260 ms 48200 KB Output is correct
3 Correct 263 ms 48200 KB Output is correct
4 Correct 264 ms 48200 KB Output is correct
5 Correct 269 ms 48228 KB Output is correct
6 Correct 262 ms 48200 KB Output is correct
7 Correct 269 ms 48212 KB Output is correct
8 Correct 281 ms 48200 KB Output is correct
9 Correct 203 ms 31304 KB Output is correct
10 Correct 212 ms 31304 KB Output is correct
11 Correct 19 ms 8016 KB Output is correct
12 Correct 19 ms 8016 KB Output is correct
13 Correct 20 ms 8016 KB Output is correct
14 Correct 19 ms 7944 KB Output is correct
15 Correct 20 ms 8016 KB Output is correct
16 Correct 19 ms 8016 KB Output is correct
17 Correct 18 ms 8016 KB Output is correct
18 Correct 16 ms 8016 KB Output is correct
19 Correct 17 ms 8016 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 45 ms 8272 KB Output is correct
22 Correct 45 ms 8144 KB Output is correct
23 Correct 47 ms 8272 KB Output is correct
24 Correct 45 ms 8440 KB Output is correct
25 Correct 45 ms 8272 KB Output is correct
26 Correct 45 ms 8272 KB Output is correct
27 Correct 45 ms 8272 KB Output is correct
28 Correct 23 ms 8272 KB Output is correct
29 Correct 23 ms 8272 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 504 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 10 ms 1872 KB Output is correct
42 Correct 11 ms 1872 KB Output is correct
43 Correct 11 ms 2060 KB Output is correct
44 Correct 10 ms 1872 KB Output is correct
45 Correct 11 ms 1872 KB Output is correct
46 Correct 10 ms 2060 KB Output is correct
47 Correct 11 ms 1872 KB Output is correct
48 Correct 8 ms 1360 KB Output is correct
49 Correct 3 ms 592 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 374 ms 20300 KB Output is correct
52 Correct 333 ms 20296 KB Output is correct
53 Correct 362 ms 20372 KB Output is correct
54 Correct 347 ms 20296 KB Output is correct
55 Correct 326 ms 20312 KB Output is correct
56 Correct 306 ms 20296 KB Output is correct
57 Correct 334 ms 20160 KB Output is correct
58 Correct 132 ms 20296 KB Output is correct
59 Correct 135 ms 20300 KB Output is correct
60 Correct 1 ms 336 KB Output is correct
61 Correct 603 ms 48200 KB Output is correct
62 Correct 566 ms 48200 KB Output is correct
63 Correct 522 ms 48088 KB Output is correct
64 Correct 669 ms 48204 KB Output is correct
65 Correct 685 ms 48076 KB Output is correct
66 Correct 606 ms 48128 KB Output is correct
67 Correct 679 ms 48044 KB Output is correct
68 Correct 445 ms 31304 KB Output is correct
69 Correct 424 ms 24392 KB Output is correct
70 Correct 1 ms 336 KB Output is correct