Submission #696674

# Submission time Handle Problem Language Result Execution time Memory
696674 2023-02-07T03:30:50 Z LeDaiKing Izbori (COCI22_izbori) C++14
110 / 110
849 ms 27388 KB
#include<bits/stdc++.h>

using namespace std;

#define NMOD 3
#define ll long long
#define fi first
#define se second
#define pb push_back
#define log 17
#define mask(i) (1ll << (i))
#define setp(x) setprecision(x)
#define ALL(v) v.begin(), v.end()
#define ck(n, i) (((n) >> (i)) & 1) 
#define getbit(x) __builtin_popcount(x)

const double PI = acos(-1);
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const long long MODo = 123456789;

const int oo = 1e9;
const long long oo15 = 1e15, oo18 = 1e18+3, oooo = 922372036854775807;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll sg[1600005], sg1[1600005], lazy[1600005], lazy1[1600005];
int a[200005];
vector<int> ldk[200005];

void dolazy(int id, int l, int mid, int r)
{
	sg[id*2] += 1ll*(mid - l + 1)*lazy[id];
	sg[id*2 + 1] += 1ll*(r - mid)*lazy[id];
	lazy[id*2] += lazy[id];
	lazy[id*2 + 1] += lazy[id];
	lazy[id] = 0;
}

void dolazy1(int id, int l, int mid, int r)
{
	sg1[id*2] += 1ll*(l + mid)*(mid - l + 1)/2*lazy1[id];
	sg1[id*2 + 1] += 1ll*(mid + 1 + r)*(r - mid)/2*lazy1[id];
	lazy1[id*2] += lazy1[id];
	lazy1[id*2 + 1] += lazy1[id];
	lazy1[id] = 0;
}


void upd(int id, int l, int r, int u, int v, int val)
{
	if (l > v || r < u) return;
	
	if (u <= l && r <= v)
	{
		sg[id] += 1ll*(r - l + 1)*val; 
		lazy[id] += val;
		return;
	}

	int mid = (l + r) >> 1;
	dolazy(id, l, mid, r);

	upd(id*2, l, mid, u, v, val); upd(id*2 + 1, mid + 1, r, u, v, val);
	sg[id] = sg[id*2] + sg[id*2+1];
}

void upd1(int id, int l, int r, int u, int v, int val)
{
	if (l > v || r < u) return;

	if (u <= l && r <= v)
	{
		sg1[id] += 1ll*(l + r)*(r - l + 1)/2*1ll*val;
		lazy1[id] += val;
		return;
	}

	int mid = (l + r) >> 1;
	dolazy1(id, l, mid, r);

	upd1(id*2, l, mid, u, v, val); upd1(id*2 + 1, mid + 1, r, u, v, val);
	sg1[id] = sg1[id*2] + sg1[id*2+1];
}

ll get(int id, int l, int r, int u, int v)
{
	if (l > v || r < u) return 0;
	if (u <= l && r <= v)
	{
		return sg[id];
	}

	int mid = (l + r) >> 1;
	dolazy(id, l, mid, r);

	return get(id*2, l, mid, u, v) + get(id*2 + 1, mid + 1, r, u, v);
}

ll get1(int id, int l, int r, int u, int v)
{
	if (l > v || r < u) return 0;
	if (u <= l && r <= v)
	{
		return sg1[id];
	}

	int mid = (l + r) >> 1;
	dolazy1(id, l, mid, r);

	return get1(id*2, l, mid, u, v) + get1(id*2 + 1, mid + 1, r, u, v);
}

int n;

void update(int l, int r, int val)
{
	l += n + 1; r += n + 1;
	upd(1, 1, 2*n + 1, l, r, val);
	upd1(1, 1, 2*n + 1, l, r, val);
}

ll getval(int l, int r)
{
	l += n + 1; r += n + 1;
	l = max(l, 1);

	if (l > r) return 0;
	
	if (l == r)
	{
		return get(1, 1, 2*n + 1, 1, l);
	}
	else
	{
		return 1ll*(r - l + 1)*get(1, 1, 2*n + 1, 1, l) - get1(1, 1, 2*n + 1, l + 1, r) + 1ll*(r + 1)*get(1, 1, 2*n + 1, l + 1, r);
	}
}

void solve() 
{
	cin >> n;
	vector<int> diff;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		diff.pb(a[i]);
	}	
	sort(ALL(diff)); diff.resize(unique(ALL(diff)) - diff.begin());
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		a[i] = lower_bound(ALL(diff), a[i]) - diff.begin();
		ldk[a[i]].pb(i);
	}

	ll ans = 0;

	for (int i = 0; i < (int)diff.size(); i++)
	{
		update(-ldk[i][0] + 1, 0, 1);
		ldk[i].pb(n + 1);
		int su = 0;
		for (int j = 0; j + 1 < (int)ldk[i].size(); j++)
		{	
			su++;
			int r = 2*su - ldk[i][j], l = 2*su - ldk[i][j + 1] + 1; 
			ans += getval(l - 1, r - 1);
			update(l, r, 1);
		}

		update(-ldk[i][0] + 1, 0, -1);
		su = 0;
		for (int j = 0; j + 1 < (int)ldk[i].size(); j++)
		{	
			su++;
			int r = 2*su - ldk[i][j], l = 2*su - ldk[i][j + 1] + 1; 
			update(l, r, -1);
		}
	}

	cout << ans;
}

int main() 
{   
   
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);  
    int testcase = 1;
    //cin >> testcase;
    while(testcase--)
        solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5020 KB Output is correct
7 Correct 6 ms 5200 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 6 ms 5204 KB Output is correct
10 Correct 6 ms 5264 KB Output is correct
11 Correct 7 ms 5204 KB Output is correct
12 Correct 5 ms 5156 KB Output is correct
13 Correct 6 ms 5204 KB Output is correct
14 Correct 5 ms 5268 KB Output is correct
15 Correct 5 ms 5264 KB Output is correct
16 Correct 6 ms 5204 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 7644 KB Output is correct
2 Correct 256 ms 8112 KB Output is correct
3 Correct 128 ms 6948 KB Output is correct
4 Correct 276 ms 8176 KB Output is correct
5 Correct 276 ms 8052 KB Output is correct
6 Correct 275 ms 8232 KB Output is correct
7 Correct 268 ms 8168 KB Output is correct
8 Correct 265 ms 8372 KB Output is correct
9 Correct 277 ms 8280 KB Output is correct
10 Correct 292 ms 8172 KB Output is correct
11 Correct 242 ms 24164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5020 KB Output is correct
7 Correct 6 ms 5200 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 6 ms 5204 KB Output is correct
10 Correct 6 ms 5264 KB Output is correct
11 Correct 7 ms 5204 KB Output is correct
12 Correct 5 ms 5156 KB Output is correct
13 Correct 6 ms 5204 KB Output is correct
14 Correct 5 ms 5268 KB Output is correct
15 Correct 5 ms 5264 KB Output is correct
16 Correct 6 ms 5204 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 196 ms 7644 KB Output is correct
19 Correct 256 ms 8112 KB Output is correct
20 Correct 128 ms 6948 KB Output is correct
21 Correct 276 ms 8176 KB Output is correct
22 Correct 276 ms 8052 KB Output is correct
23 Correct 275 ms 8232 KB Output is correct
24 Correct 268 ms 8168 KB Output is correct
25 Correct 265 ms 8372 KB Output is correct
26 Correct 277 ms 8280 KB Output is correct
27 Correct 292 ms 8172 KB Output is correct
28 Correct 242 ms 24164 KB Output is correct
29 Correct 319 ms 24544 KB Output is correct
30 Correct 181 ms 10768 KB Output is correct
31 Correct 362 ms 16280 KB Output is correct
32 Correct 849 ms 27388 KB Output is correct
33 Correct 447 ms 16436 KB Output is correct
34 Correct 411 ms 16536 KB Output is correct
35 Correct 226 ms 11476 KB Output is correct
36 Correct 126 ms 10696 KB Output is correct
37 Correct 158 ms 10924 KB Output is correct
38 Correct 310 ms 18120 KB Output is correct
39 Correct 312 ms 18156 KB Output is correct
40 Correct 323 ms 18116 KB Output is correct
41 Correct 319 ms 18240 KB Output is correct
42 Correct 317 ms 18192 KB Output is correct
43 Correct 374 ms 21960 KB Output is correct
44 Correct 377 ms 21868 KB Output is correct
45 Correct 454 ms 21832 KB Output is correct
46 Correct 385 ms 21956 KB Output is correct
47 Correct 382 ms 22068 KB Output is correct
48 Correct 528 ms 24288 KB Output is correct
49 Correct 575 ms 24372 KB Output is correct
50 Correct 491 ms 24464 KB Output is correct
51 Correct 514 ms 24512 KB Output is correct
52 Correct 566 ms 24300 KB Output is correct
53 Correct 505 ms 24124 KB Output is correct
54 Correct 561 ms 24156 KB Output is correct