Submission #707058

# Submission time Handle Problem Language Result Execution time Memory
707058 2023-03-08T11:09:40 Z Chal1shkan Izbori (COCI22_izbori) C++14
110 / 110
1173 ms 38992 KB
# include <bits/stdc++.h>
 
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 
# define pll pair <ll, ll>
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
const ll maxn = 2e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 1e9 + 123;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
 
using namespace std;
 
ll n, a[maxn];
ll b[maxn];
vector <ll> pos[maxn];
 
struct node
{
	ll sum1, sum2, upd, sz;	
} t[maxn * 8];
 
ll cnt (ll x)
{
	return x * (x + 1) / 2;
}

void push (ll v, ll tl, ll tr)
{
	if (t[v].upd && tl != tr)
	{
		ll tm = (tl + tr) >> 1;
		t[v * 2].sum1 += t[v].upd * (tm - tl + 1);
		t[v * 2 + 1].sum1 += t[v].upd * (tr - tm);
		
		t[v * 2].sum2 += t[v].upd * cnt(tm - tl + 1);
		t[v * 2 + 1].sum2 += t[v].upd * cnt(tr - tm);
		
		t[v * 2].upd += t[v].upd;
		t[v * 2 + 1].upd += t[v].upd;
		t[v].upd = 0;
	}
}
 
void update (ll l, ll r, ll x, ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	if (tr < l || r < tl) return;
	if (l <= tl && tr <= r)
	{
		t[v].sum1 += x * (tr - tl + 1);
		t[v].sum2 += x * cnt(tr - tl + 1);
		t[v].upd += x;
		return;
	}
	ll tm = (tl + tr) >> 1;
	update(l, r, x, v * 2, tl, tm);
	update(l, r, x, v * 2 + 1, tm + 1, tr);
	t[v].sum1 = t[v * 2].sum1 + t[v * 2 + 1].sum1;
	t[v].sum2 = t[v * 2].sum1 * (tr - tm) + t[v * 2].sum2 + t[v * 2 + 1].sum2;
}
 
node get (ll l, ll r, ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	if (tr < l || r < tl) return {0, 0, 0, 0};
	if (l <= tl && tr <= r) return {t[v].sum1, t[v].sum2, t[v].upd, tr - tl + 1};
	ll tm = (tl + tr) >> 1;
	node x = get(l, r, v * 2, tl, tm);
	node y = get(l, r, v * 2 + 1, tm + 1, tr);
	return {x.sum1 + y.sum1, x.sum1 * y.sz + x.sum2 + y.sum2, x.upd, x.sz + y.sz};
}
 
void out (ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	cout << tl << ' ' << tr << ' ' << t[v].sum2 << nl;
	if (tl == tr)
	{
		return;
	}
	ll tm = (tl + tr) >> 1;
	out(v * 2, tl, tm);
	out(v * 2 + 1, tm + 1, tr);
}

ll query (ll l, ll r)
{
	ll x = get(0, l - 1).sum1;
	ll y = get(l, r - 1).sum2;
	return x * (r - l + 1) + y;
}

void clear (ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	t[v].sum1 = t[v].sum2 = t[v].upd = 0;
	if (tl == tr)
	{
		return;
	}
	ll tm = (tl + tr) >> 1;
	clear(v * 2, tl, tm);
	clear(v * 2 + 1, tm + 1, tr);
}

void ma1n (/* SABR */)
{
	cin >> n;
	for (ll i = 1; i <= n; ++i)
	{
		cin >> a[i];
		b[i] = a[i];	
	}	
	sort(b + 1, b + 1 + n);
	set <ll> st;
	for (ll i = 1; i <= n; ++i)
	{
		a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
		st.insert(a[i]);
		pos[a[i]].pb(i);
	}
	ll ans = 0;
	for (ll x : st)
	{
		pos[x].pb(0);
		pos[x].pb(n + 1);
		sort(pos[x].begin(), pos[x].end());
		vector <pll> clr;
		ll cur = n;
		update(n, n, 1);
		clr.pb({n, n});
		for (ll i = 1; i < sz(pos[x]); ++i)
		{
			ll siz = pos[x][i] - pos[x][i - 1] - 1; 
			ans += query(cur - siz, cur - 1);
			update(cur - siz, cur - 1, 1);
			clr.pb({cur - siz, cur - 1});
			if (pos[x][i] != n + 1)
			{
				ans += get(0, cur - siz).sum1;
				update(cur - siz + 1, cur - siz + 1, 1);
				clr.pb({cur - siz + 1, cur - siz + 1});
			}
			cur = cur - siz + 1;	
		}
		for (pll p : clr)
		{
			update(p.ff, p.ss, -1);
		}
	}
	cout << ans;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("spainting.in", "r", stdin);
//  freopen("spainting.out", "w", stdout);
    int ttt = 1;
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5096 KB Output is correct
4 Correct 3 ms 5080 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5096 KB Output is correct
4 Correct 3 ms 5080 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 6 ms 5264 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 6 ms 5204 KB Output is correct
13 Correct 6 ms 5204 KB Output is correct
14 Correct 6 ms 5260 KB Output is correct
15 Correct 6 ms 5260 KB Output is correct
16 Correct 6 ms 5204 KB Output is correct
17 Correct 5 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 249 ms 14508 KB Output is correct
2 Correct 322 ms 16044 KB Output is correct
3 Correct 179 ms 11124 KB Output is correct
4 Correct 348 ms 16416 KB Output is correct
5 Correct 374 ms 16612 KB Output is correct
6 Correct 413 ms 17248 KB Output is correct
7 Correct 378 ms 17024 KB Output is correct
8 Correct 398 ms 17136 KB Output is correct
9 Correct 372 ms 17024 KB Output is correct
10 Correct 389 ms 17032 KB Output is correct
11 Correct 396 ms 32524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5096 KB Output is correct
4 Correct 3 ms 5080 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 6 ms 5264 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 6 ms 5204 KB Output is correct
13 Correct 6 ms 5204 KB Output is correct
14 Correct 6 ms 5260 KB Output is correct
15 Correct 6 ms 5260 KB Output is correct
16 Correct 6 ms 5204 KB Output is correct
17 Correct 5 ms 5332 KB Output is correct
18 Correct 249 ms 14508 KB Output is correct
19 Correct 322 ms 16044 KB Output is correct
20 Correct 179 ms 11124 KB Output is correct
21 Correct 348 ms 16416 KB Output is correct
22 Correct 374 ms 16612 KB Output is correct
23 Correct 413 ms 17248 KB Output is correct
24 Correct 378 ms 17024 KB Output is correct
25 Correct 398 ms 17136 KB Output is correct
26 Correct 372 ms 17024 KB Output is correct
27 Correct 389 ms 17032 KB Output is correct
28 Correct 396 ms 32524 KB Output is correct
29 Correct 430 ms 32564 KB Output is correct
30 Correct 215 ms 13904 KB Output is correct
31 Correct 498 ms 22176 KB Output is correct
32 Correct 1173 ms 38992 KB Output is correct
33 Correct 527 ms 22620 KB Output is correct
34 Correct 513 ms 22916 KB Output is correct
35 Correct 327 ms 15840 KB Output is correct
36 Correct 223 ms 13092 KB Output is correct
37 Correct 233 ms 14200 KB Output is correct
38 Correct 440 ms 23076 KB Output is correct
39 Correct 436 ms 22996 KB Output is correct
40 Correct 432 ms 22968 KB Output is correct
41 Correct 432 ms 23220 KB Output is correct
42 Correct 441 ms 23016 KB Output is correct
43 Correct 505 ms 25092 KB Output is correct
44 Correct 496 ms 25224 KB Output is correct
45 Correct 537 ms 25020 KB Output is correct
46 Correct 493 ms 25240 KB Output is correct
47 Correct 549 ms 25136 KB Output is correct
48 Correct 648 ms 26624 KB Output is correct
49 Correct 611 ms 26592 KB Output is correct
50 Correct 610 ms 27096 KB Output is correct
51 Correct 592 ms 27384 KB Output is correct
52 Correct 568 ms 26288 KB Output is correct
53 Correct 610 ms 26224 KB Output is correct
54 Correct 589 ms 26496 KB Output is correct