#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ > using AR = array<T, SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = ((b)-1); i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const ll MOD = 1e9 + 7;
const ll MX = 1e9;
const ll INF = 1e18;
const db PI = acos((db)-1);
const int ddef[4]{ 1,0,-1,0 }, dataq[4]{ 0,1,0,-1 };
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
void setIO(string name = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void _print(char i) { cerr << i; }
void _print(string i) { cerr << i; }
void _print(float i) { cerr << i; }
void _print(int i) { cerr << i; }
void _print(double i) { cerr << i; }
void _print() { cerr << "\n"; };
template<typename x, typename y> void _print(pair<x, y>& t) { cerr << "{";_print(t.first);cerr << ", ";_print(t.second);cerr << "},"; }
template<typename x> void _print(x& t) { cerr << "{"; for (int i = 0;i < (int)t.size();i++) { _print(t[i]); if (i < (int)t.size() - 1) cerr << ", "; } cerr << "}"; }
template<typename x, typename... y> void _print(x a, y... b) { _print(a);if (sizeof...(b)) cerr << ", ";_print(b...); }
#define dbg(x...) cerr<<"["<<#x<<"] = [";_print(x);cerr<<"]\n";
int test;
void solve() {
ll n; cin >> n;
vl a(n);
each(x, a) cin >> x;
vl b = a;
sor(b);
if(a == b){
cout << "0\n";
return;
}
if(n <= 10000){
ll ans = 0;
FOR(i, 1, n - 1){
ll cnt1 = 0, cnt2 = 0;
for(ll j = 0; j < i; j++){
if(a[i] > a[j]) cnt1++;
}
for(ll j = i + 1; j < n; j++){
if(a[i] > a[j]) cnt2++;
}
ans += cnt1 * cnt2;
}
cout << ans << "\n";
return;
}
map<ll, ll> m;
ll cnt = 0;
F0R(i, n){
if(!m[b[i]]){
m[b[i]] = cnt++;
}
}
ll pref[n + 1][cnt];
F0R(i, n){
a[i] = m[a[i]];
}
F0R(i, n + 1){
F0R(j, cnt){
pref[i][j] = 0;
}
}
for(ll i = 0; i < n; i++){
for(ll j = 0; j < cnt; j++){
pref[i + 1][j] = pref[i][j] + (j > a[i]);
}
}
ll ans = 0;
FOR(i, 1, n - 1){
// cout << pref[i][a[i]] - pref[i - 1][a[i]] << " " << pref[n][a[i]] - pref[i][a[i]] << "\n";
ans += (pref[i][a[i]]) * (pref[n][a[i]] - pref[i + 1][a[i]]);
}
for(ll j = 0; j < cnt; j++){
for(ll i = 0; i < n + 1; i++){
}
}
cout << ans << "\n";
}
int main() {
setIO("");
int T = 1;
// cin >> T;
for (test = 1; test <= T; test++) solve();
return 0;
}
Compilation message
Mountains.cpp: In function 'void setIO(std::string)':
Mountains.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mountains.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
55 ms |
4976 KB |
Output is correct |
3 |
Correct |
57 ms |
4976 KB |
Output is correct |
4 |
Correct |
57 ms |
4976 KB |
Output is correct |
5 |
Correct |
55 ms |
4972 KB |
Output is correct |
6 |
Correct |
56 ms |
4940 KB |
Output is correct |
7 |
Correct |
49 ms |
4984 KB |
Output is correct |
8 |
Correct |
52 ms |
5016 KB |
Output is correct |
9 |
Correct |
59 ms |
4976 KB |
Output is correct |
10 |
Correct |
52 ms |
4980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11980 KB |
Output is correct |
2 |
Correct |
31 ms |
11936 KB |
Output is correct |
3 |
Correct |
33 ms |
11960 KB |
Output is correct |
4 |
Correct |
34 ms |
12016 KB |
Output is correct |
5 |
Correct |
39 ms |
12020 KB |
Output is correct |
6 |
Correct |
35 ms |
12028 KB |
Output is correct |
7 |
Correct |
33 ms |
12048 KB |
Output is correct |
8 |
Correct |
19 ms |
4988 KB |
Output is correct |
9 |
Correct |
20 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11980 KB |
Output is correct |
2 |
Correct |
31 ms |
11936 KB |
Output is correct |
3 |
Correct |
33 ms |
11960 KB |
Output is correct |
4 |
Correct |
34 ms |
12016 KB |
Output is correct |
5 |
Correct |
39 ms |
12020 KB |
Output is correct |
6 |
Correct |
35 ms |
12028 KB |
Output is correct |
7 |
Correct |
33 ms |
12048 KB |
Output is correct |
8 |
Correct |
19 ms |
4988 KB |
Output is correct |
9 |
Correct |
20 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
214 ms |
242212 KB |
Output is correct |
12 |
Correct |
223 ms |
242204 KB |
Output is correct |
13 |
Correct |
210 ms |
242284 KB |
Output is correct |
14 |
Correct |
215 ms |
242212 KB |
Output is correct |
15 |
Correct |
228 ms |
242204 KB |
Output is correct |
16 |
Correct |
203 ms |
242192 KB |
Output is correct |
17 |
Correct |
220 ms |
242112 KB |
Output is correct |
18 |
Correct |
21 ms |
4992 KB |
Output is correct |
19 |
Correct |
21 ms |
4984 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
79 ms |
468 KB |
Output is correct |
12 |
Correct |
73 ms |
468 KB |
Output is correct |
13 |
Correct |
76 ms |
468 KB |
Output is correct |
14 |
Correct |
75 ms |
456 KB |
Output is correct |
15 |
Correct |
79 ms |
468 KB |
Output is correct |
16 |
Correct |
79 ms |
468 KB |
Output is correct |
17 |
Correct |
75 ms |
468 KB |
Output is correct |
18 |
Correct |
76 ms |
468 KB |
Output is correct |
19 |
Correct |
88 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11980 KB |
Output is correct |
2 |
Correct |
31 ms |
11936 KB |
Output is correct |
3 |
Correct |
33 ms |
11960 KB |
Output is correct |
4 |
Correct |
34 ms |
12016 KB |
Output is correct |
5 |
Correct |
39 ms |
12020 KB |
Output is correct |
6 |
Correct |
35 ms |
12028 KB |
Output is correct |
7 |
Correct |
33 ms |
12048 KB |
Output is correct |
8 |
Correct |
19 ms |
4988 KB |
Output is correct |
9 |
Correct |
20 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
214 ms |
242212 KB |
Output is correct |
12 |
Correct |
223 ms |
242204 KB |
Output is correct |
13 |
Correct |
210 ms |
242284 KB |
Output is correct |
14 |
Correct |
215 ms |
242212 KB |
Output is correct |
15 |
Correct |
228 ms |
242204 KB |
Output is correct |
16 |
Correct |
203 ms |
242192 KB |
Output is correct |
17 |
Correct |
220 ms |
242112 KB |
Output is correct |
18 |
Correct |
21 ms |
4992 KB |
Output is correct |
19 |
Correct |
21 ms |
4984 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
55 ms |
4976 KB |
Output is correct |
3 |
Correct |
57 ms |
4976 KB |
Output is correct |
4 |
Correct |
57 ms |
4976 KB |
Output is correct |
5 |
Correct |
55 ms |
4972 KB |
Output is correct |
6 |
Correct |
56 ms |
4940 KB |
Output is correct |
7 |
Correct |
49 ms |
4984 KB |
Output is correct |
8 |
Correct |
52 ms |
5016 KB |
Output is correct |
9 |
Correct |
59 ms |
4976 KB |
Output is correct |
10 |
Correct |
52 ms |
4980 KB |
Output is correct |
11 |
Correct |
31 ms |
11980 KB |
Output is correct |
12 |
Correct |
31 ms |
11936 KB |
Output is correct |
13 |
Correct |
33 ms |
11960 KB |
Output is correct |
14 |
Correct |
34 ms |
12016 KB |
Output is correct |
15 |
Correct |
39 ms |
12020 KB |
Output is correct |
16 |
Correct |
35 ms |
12028 KB |
Output is correct |
17 |
Correct |
33 ms |
12048 KB |
Output is correct |
18 |
Correct |
19 ms |
4988 KB |
Output is correct |
19 |
Correct |
20 ms |
4948 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
214 ms |
242212 KB |
Output is correct |
22 |
Correct |
223 ms |
242204 KB |
Output is correct |
23 |
Correct |
210 ms |
242284 KB |
Output is correct |
24 |
Correct |
215 ms |
242212 KB |
Output is correct |
25 |
Correct |
228 ms |
242204 KB |
Output is correct |
26 |
Correct |
203 ms |
242192 KB |
Output is correct |
27 |
Correct |
220 ms |
242112 KB |
Output is correct |
28 |
Correct |
21 ms |
4992 KB |
Output is correct |
29 |
Correct |
21 ms |
4984 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
79 ms |
468 KB |
Output is correct |
42 |
Correct |
73 ms |
468 KB |
Output is correct |
43 |
Correct |
76 ms |
468 KB |
Output is correct |
44 |
Correct |
75 ms |
456 KB |
Output is correct |
45 |
Correct |
79 ms |
468 KB |
Output is correct |
46 |
Correct |
79 ms |
468 KB |
Output is correct |
47 |
Correct |
75 ms |
468 KB |
Output is correct |
48 |
Correct |
76 ms |
468 KB |
Output is correct |
49 |
Correct |
88 ms |
468 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
52 |
Halted |
0 ms |
0 KB |
- |