Submission #707028

# Submission time Handle Problem Language Result Execution time Memory
707028 2023-03-08T10:02:44 Z Chal1shkan Izbori (COCI22_izbori) C++14
0 / 110
98 ms 14664 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];
ll t[maxn * 4];
ll z[maxn * 4];

ll cnt (ll x)
{
	return x * (x + 1) / 2;
}

void push (ll v, ll tl, ll tr)
{
	if (z[v] && tl != tr)
	{
		ll tm = (tl + tr) >> 1;
		t[v * 2] += z[v] * (tm - tl + 1);
		t[v * 2 + 1] += z[v] * (tr - tm);
		z[v * 2] += z[v];
		z[v * 2 + 1] += z[v];
		z[v] = 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] += x * (tr - tl + 1);
		z[v] += 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] = t[v * 2] + t[v * 2 + 1];
}

ll 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;
	if (l <= tl && tr <= r) return t[v];
	ll tm = (tl + tr) >> 1;
	return get(l, r, v * 2, tl, tm) + get(l, r, v * 2 + 1, tm + 1, tr);
}

void out (ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	cout << tl << ' ' << tr << ' ' << t[v] << endl; 
	if (tl == tr)
	{
		return;
	}
	ll tm = (tl + tr) >> 1;
	out(v * 2, tl, tm);
	out(v * 2 + 1, tm + 1, tr);
}

void clear (ll v = 1, ll tl = 0, ll tr = n + n)
{
	push(v, tl, tr);
	t[v] = 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)
	{
		ll cur = n;
		update(n, n, 1);
		for (ll i = 1; i <= n; ++i)
		{
			ll j = i;
			if (a[i] == x)
			{
				cur++;
				ans += get(0, cur - 1);		
				update(cur, cur, 1);
			}	
			else
			{
				while (j + 1 <= n && a[j + 1] != x) ++j;
				ll siz = j - i + 1;
				ans += get(0, cur - siz - 1) * siz;
				update(cur - siz, cur - 1, 1);
				cur -= siz;
				i = j;
			}
		}	
	//	cout << x << ' ' << ans << nl;
		clear();
	}
	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 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 98 ms 14664 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -