이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# 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 pref[maxn];
ll t[maxn * 8];
 
void clear (ll v = 1, ll tl = 0, ll tr = n + n)
{
	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 update (ll pos, ll v = 1, ll tl = 0, ll tr = n + n)
{
	if (tl == tr)
	{
		t[v]++;
		return;
	}
	ll tm = (tl + tr) >> 1;
	if (pos <= tm)
	{
		update(pos, v * 2, tl, tm);
	}
	else
	{
		update(pos, 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)
{
	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 ma1n (/* SABR */)
{
	cin >> n;
	set <ll> st;
	for (ll i = 1; i <= n; ++i)
	{
		cin >> a[i];
		st.insert(a[i]);
	}
	ll ans = n;
	for (ll x : st)
	{
		pref[0] = n;
		for (ll i = 1; i <= n; ++i)
		{
			if (a[i] == x)
			{
				pref[i] = 1;
				pref[i] += pref[i - 1];
			}
			else
			{
				pref[i] = -1;
				pref[i] += pref[i - 1];
			}
			ans += get(0, pref[i] - 1);
			update(pref[i]);
		}
		clear(); 
	}
	cout << ans << nl;
}
 
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 | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |