Submission #628825

# Submission time Handle Problem Language Result Execution time Memory
628825 2022-08-13T17:53:20 Z vovamr Izbori (COCI22_izbori) C++17
0 / 110
25 ms 7764 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
 
const int N = 2e5 + 10;
int a[N]; ll ans = 0;
ve<int> occ[N];
 
inline static ll F(const int &l, const int &r) { return (l + r) * 1ll * (r - l + 1) / 2ll; }
inline void cnt(const int &l1, const int &r1, 
		const int &l2, const int &r2,
		const int &C) {
	int R = min(r2, r1 + C - 1);
	int L = max(l2, l1 + C);
	ans += max(0, R - l2 + 1) * 1ll * (r1 + 1);
	ans -= l1 * 1ll * max(0, min(R, l1 + C - 1) - l2 + 1);
	ans += max(0, R - L + 1) * 1ll * C;
	if (L <= R) ans -= F(L + 1, R + 1);
}
 
const int Z = 2e5 + 5;
const int M = 4e5 + 10; int f[M];
inline void upd(int i, int x) { i += 2 + Z; for (; i < M; i += i & -i) f[i] += x; }
inline int get(int i) { i += 2 + Z; int ans = 0; for (; i; i -= i & -i){ans+=f[i];} return ans; }
 
inline void solve() {
	int n;
	cin >> n;
	ve<int> al;
	for (int i = 0; i < n; ++i) cin >> a[i], al.pb(a[i]);
	sort(all(al)), al.erase(unique(all(al)), al.end());
	for (int i = 0; i < n; ++i) a[i] = lower_bound(all(al), a[i]) - al.begin();
	for (int i = 0; i < n; ++i) occ[a[i]].pb(i);
 
	int b = 0;
	{
		ll s1 = 0;
		ll c = n * (31 - __builtin_ctz(n));
 
		ve<int> hui;
		for (int i = 0; i < sz(al); ++i) hui.pb(sz(occ[i]));
		sort(all(hui));

		ll opt = c * 1ll * sz(hui);
		for (int i = 0; i < sz(hui); ) {
			int j = i;
			while (j < sz(hui) && hui[j] == hui[i]) {
				s1 += hui[j] * 1ll * hui[j];
				++j;
			}
 
			if (chmin(opt, s1 + (sz(hui) - j) * 1ll * c)) {
				b = hui[i];
			}
			i = j;
		}
	}
 
	ve<int> p(n);
	for (int el = 0; el < sz(al); ++el) {
		if (sz(occ[el]) > b) {
			for (int i = 0; i < n; ++i) {
				p[i] = (a[i] == el ? 1 : -1);
				if (i) p[i] += p[i - 1];
				ans += get(p[i] - 1);
				upd(p[i], +1);
			}
			for (int i = 0; i < n; ++i) upd(p[i], -1);
		}
		else {
			for (int l = 0; l < sz(occ[el]); ++l) {
				for (int r = l; r < sz(occ[el]); ++r) {
					int r1 = occ[el][l] - 1, l1 = (!l ? -1 : occ[el][l - 1]);
					int l2 = occ[el][r], r2 = (r + 1 == sz(occ[el]) ? n - 1 : occ[el][r + 1] - 1);
					cnt(l1, r1, l2, r2, 2 * (r - l + 1));
				}
			}
		}
	}
 
	cout << ans;
}
 
signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 7764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -