#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int B = 560;
int calc(ve<int> a) {
int ans = 0;
fr (i, 0, sz(a) - 1) {
set<int> s;
fr (j, i, sz(a) - 1) {
s.insert(a[j]);
ans += sz(s);
}
}
return ans;
}
ll calc2(ve<int> a) {
int total = 0;
fe (x, a) total += x;
ll ans = 0;
int toLeft = 0;
ans += 1LL * sz(a) * total * (total + 1) / 2;
fr (i, 0, sz(a) - 1) {
int toRight = total - toLeft - a[i];
ans -= 1LL * toLeft * (toLeft + 1) / 2;
ans -= 1LL * toRight * (toRight + 1) / 2;
toLeft += a[i];
}
return ans;
}
ll calc3(ve<pii> a) {
assert(sz(a) <= B);
int total = 0;
int gr = 0;
fe (x, a) {
total += x.fi * x.se;
gr += x.se;
}
ll ans = 0;
ans += 1LL * gr * total * (total + 1) / 2;
int toLeft = 0;
fe (cur, a) {
fr (j, 1, cur.se) {
ans -= 1LL * toLeft * (toLeft + 1) / 2;
toLeft += cur.fi;
}
}
reverse(all(a));
toLeft = 0;
fe (cur, a) {
fr (j, 1, cur.se) {
ans -= 1LL * toLeft * (toLeft + 1) / 2;
toLeft += cur.fi;
}
}
return ans;
}
const int N = 3e5 + 5;
int n, q;
int a[N];
int cnt[N];
int rnd(int l, int r) {
return rng() % (r - l + 1) + l;
}
ll solve(ve<int> a) {
fe (x, a) cnt[x] = 0;
fe (x, a) cnt[x]++;
ve<pii> order;
fe (x, a) {
if (cnt[x]) {
order.pb({cnt[x], x});
cnt[x] = 0;
}
}
sort(all(order));
reverse(all(order));
{
ve<pii> st;
fe (cur, order) {
if (!sz(st) || cur.fi != st.back().fi) {
st.pb({cur.fi, 1});
} else {
st.back().se++;
}
}
deque<pii> d;
bool who = 0;
fe (x, st) {
if (x.se > 1) {
d.push_front({x.fi, x.se / 2});
d.pb({x.fi, x.se / 2});
x.se %= 2;
}
if (x.se) {
if (who == 0) {
if (sz(d) && d.front().fi == x.fi) d.front().se++;
else d.push_front({x.fi, 1});
} else {
if (sz(d) && d.back().fi == x.fi) d.back().se++;
else d.pb({x.fi, 1});
}
who ^= 1;
}
}
ve<pii> v;
fe (x, d) v.pb(x);
return calc3(v);
}
assert(0);
//
// deque<int> d;
// bool who = 0;
// fe (cur, order) {
// if (who == 0) d.push_front(cur.fi);
// else d.pb(cur.fi);
// who ^= 1;
// }
// ve<int> v;
// int now = 1;
// fe (x, d) {
// v.pb(x);
// }
// return calc2(v);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> q;
ve<int> v;
fr (i, 1, n) {
cin >> a[i];
v.pb(a[i]);
}
sort(all(v));
cout << solve(v) << "\n";
// int mn = 1e9;
// do {
// umn(mn, calc(v));
// } while (next_permutation(all(v)));
//
// cout << solve(v) << " " << mn << "\n";
//
// do {
// if (calc(v) == mn) {
// cout << "v: ";
// fe (x, v) cout << x << " ";
// cout << "\n";
// }
// } while (next_permutation(all(v)));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
1620 KB |
Output is correct |
5 |
Correct |
17 ms |
2824 KB |
Output is correct |
6 |
Correct |
26 ms |
3876 KB |
Output is correct |
7 |
Correct |
24 ms |
3988 KB |
Output is correct |
8 |
Correct |
26 ms |
4032 KB |
Output is correct |
9 |
Correct |
32 ms |
3968 KB |
Output is correct |
10 |
Correct |
25 ms |
3916 KB |
Output is correct |
11 |
Correct |
25 ms |
3984 KB |
Output is correct |
12 |
Correct |
27 ms |
3972 KB |
Output is correct |
13 |
Correct |
26 ms |
3984 KB |
Output is correct |
14 |
Correct |
25 ms |
3936 KB |
Output is correct |
15 |
Correct |
25 ms |
4040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
1620 KB |
Output is correct |
5 |
Correct |
17 ms |
2824 KB |
Output is correct |
6 |
Correct |
26 ms |
3876 KB |
Output is correct |
7 |
Correct |
24 ms |
3988 KB |
Output is correct |
8 |
Correct |
26 ms |
4032 KB |
Output is correct |
9 |
Correct |
32 ms |
3968 KB |
Output is correct |
10 |
Correct |
25 ms |
3916 KB |
Output is correct |
11 |
Correct |
25 ms |
3984 KB |
Output is correct |
12 |
Correct |
27 ms |
3972 KB |
Output is correct |
13 |
Correct |
26 ms |
3984 KB |
Output is correct |
14 |
Correct |
25 ms |
3936 KB |
Output is correct |
15 |
Correct |
25 ms |
4040 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
9 ms |
1620 KB |
Output is correct |
20 |
Correct |
18 ms |
2768 KB |
Output is correct |
21 |
Correct |
28 ms |
3984 KB |
Output is correct |
22 |
Correct |
27 ms |
3908 KB |
Output is correct |
23 |
Correct |
27 ms |
3896 KB |
Output is correct |
24 |
Correct |
28 ms |
3904 KB |
Output is correct |
25 |
Correct |
27 ms |
3904 KB |
Output is correct |
26 |
Correct |
28 ms |
3872 KB |
Output is correct |
27 |
Correct |
28 ms |
3988 KB |
Output is correct |
28 |
Correct |
27 ms |
3984 KB |
Output is correct |
29 |
Correct |
29 ms |
3960 KB |
Output is correct |
30 |
Correct |
32 ms |
4036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
1620 KB |
Output is correct |
5 |
Correct |
17 ms |
2824 KB |
Output is correct |
6 |
Correct |
26 ms |
3876 KB |
Output is correct |
7 |
Correct |
24 ms |
3988 KB |
Output is correct |
8 |
Correct |
26 ms |
4032 KB |
Output is correct |
9 |
Correct |
32 ms |
3968 KB |
Output is correct |
10 |
Correct |
25 ms |
3916 KB |
Output is correct |
11 |
Correct |
25 ms |
3984 KB |
Output is correct |
12 |
Correct |
27 ms |
3972 KB |
Output is correct |
13 |
Correct |
26 ms |
3984 KB |
Output is correct |
14 |
Correct |
25 ms |
3936 KB |
Output is correct |
15 |
Correct |
25 ms |
4040 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
9 ms |
1620 KB |
Output is correct |
20 |
Correct |
18 ms |
2768 KB |
Output is correct |
21 |
Correct |
28 ms |
3984 KB |
Output is correct |
22 |
Correct |
27 ms |
3908 KB |
Output is correct |
23 |
Correct |
27 ms |
3896 KB |
Output is correct |
24 |
Correct |
28 ms |
3904 KB |
Output is correct |
25 |
Correct |
27 ms |
3904 KB |
Output is correct |
26 |
Correct |
28 ms |
3872 KB |
Output is correct |
27 |
Correct |
28 ms |
3988 KB |
Output is correct |
28 |
Correct |
27 ms |
3984 KB |
Output is correct |
29 |
Correct |
29 ms |
3960 KB |
Output is correct |
30 |
Correct |
32 ms |
4036 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
6 ms |
984 KB |
Output is correct |
38 |
Correct |
6 ms |
1036 KB |
Output is correct |
39 |
Correct |
11 ms |
1652 KB |
Output is correct |
40 |
Runtime error |
25 ms |
5500 KB |
Execution killed with signal 6 |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
288 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
9 ms |
1620 KB |
Output is correct |
15 |
Correct |
17 ms |
2824 KB |
Output is correct |
16 |
Correct |
26 ms |
3876 KB |
Output is correct |
17 |
Correct |
24 ms |
3988 KB |
Output is correct |
18 |
Correct |
26 ms |
4032 KB |
Output is correct |
19 |
Correct |
32 ms |
3968 KB |
Output is correct |
20 |
Correct |
25 ms |
3916 KB |
Output is correct |
21 |
Correct |
25 ms |
3984 KB |
Output is correct |
22 |
Correct |
27 ms |
3972 KB |
Output is correct |
23 |
Correct |
26 ms |
3984 KB |
Output is correct |
24 |
Correct |
25 ms |
3936 KB |
Output is correct |
25 |
Correct |
25 ms |
4040 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
9 ms |
1620 KB |
Output is correct |
30 |
Correct |
18 ms |
2768 KB |
Output is correct |
31 |
Correct |
28 ms |
3984 KB |
Output is correct |
32 |
Correct |
27 ms |
3908 KB |
Output is correct |
33 |
Correct |
27 ms |
3896 KB |
Output is correct |
34 |
Correct |
28 ms |
3904 KB |
Output is correct |
35 |
Correct |
27 ms |
3904 KB |
Output is correct |
36 |
Correct |
28 ms |
3872 KB |
Output is correct |
37 |
Correct |
28 ms |
3988 KB |
Output is correct |
38 |
Correct |
27 ms |
3984 KB |
Output is correct |
39 |
Correct |
29 ms |
3960 KB |
Output is correct |
40 |
Correct |
32 ms |
4036 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
6 ms |
984 KB |
Output is correct |
48 |
Correct |
6 ms |
1036 KB |
Output is correct |
49 |
Correct |
11 ms |
1652 KB |
Output is correct |
50 |
Runtime error |
25 ms |
5500 KB |
Execution killed with signal 6 |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
288 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
9 ms |
1620 KB |
Output is correct |
15 |
Correct |
17 ms |
2824 KB |
Output is correct |
16 |
Correct |
26 ms |
3876 KB |
Output is correct |
17 |
Correct |
24 ms |
3988 KB |
Output is correct |
18 |
Correct |
26 ms |
4032 KB |
Output is correct |
19 |
Correct |
32 ms |
3968 KB |
Output is correct |
20 |
Correct |
25 ms |
3916 KB |
Output is correct |
21 |
Correct |
25 ms |
3984 KB |
Output is correct |
22 |
Correct |
27 ms |
3972 KB |
Output is correct |
23 |
Correct |
26 ms |
3984 KB |
Output is correct |
24 |
Correct |
25 ms |
3936 KB |
Output is correct |
25 |
Correct |
25 ms |
4040 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
9 ms |
1620 KB |
Output is correct |
30 |
Correct |
18 ms |
2768 KB |
Output is correct |
31 |
Correct |
28 ms |
3984 KB |
Output is correct |
32 |
Correct |
27 ms |
3908 KB |
Output is correct |
33 |
Correct |
27 ms |
3896 KB |
Output is correct |
34 |
Correct |
28 ms |
3904 KB |
Output is correct |
35 |
Correct |
27 ms |
3904 KB |
Output is correct |
36 |
Correct |
28 ms |
3872 KB |
Output is correct |
37 |
Correct |
28 ms |
3988 KB |
Output is correct |
38 |
Correct |
27 ms |
3984 KB |
Output is correct |
39 |
Correct |
29 ms |
3960 KB |
Output is correct |
40 |
Correct |
32 ms |
4036 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
6 ms |
984 KB |
Output is correct |
48 |
Correct |
6 ms |
1036 KB |
Output is correct |
49 |
Correct |
11 ms |
1652 KB |
Output is correct |
50 |
Runtime error |
25 ms |
5500 KB |
Execution killed with signal 6 |
51 |
Halted |
0 ms |
0 KB |
- |