# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
866306 |
2023-10-25T19:56:03 Z |
TAhmed33 |
Izbori (COCI22_izbori) |
C++ |
|
127 ms |
12932 KB |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC target ("popcnt")
typedef long long ll;
inline ll trig (ll x) { return ((x * (x + 1)) >> 1); }
inline ll get2 (ll a, ll b, ll c) {
if (c < 0) return 0;
a = min(a, c); b = min(b, c);
c = min(c, a + b);
return (c - b) * (b + 1) + (c + 1) * (a - c + b + 1) - trig(a) + trig(c - b - 1);
}
const int MAXN = 2e5 + 25;
int arr[MAXN], n;
vector <int> occ[MAXN];
vector <int> d;
int a[2 * MAXN + 25];
ll solve1 (int x) {
memset(a, 0, sizeof(a));
ll ret = 0; int cur = 0; int sum = 0; a[MAXN] = 1;
for (int i = 1; i <= n; i++) {
if (arr[i] == x) {
sum++; cur += a[sum - 1 + MAXN];
} else {
cur -= a[sum - 1 + MAXN]; sum--;
}
ret += cur; a[sum + MAXN]++;
}
return ret;
}
array <int, 500> p;
ll solve2 (int x) {
for (int i = 0; i < (int)occ[x].size(); i++) p[i] = occ[x][i];
int u = occ[x].size(); ll ret = 0;
//cout << x << ": \n";
for (int i = 0; i < u - 1; i++) {
for (int j = i + 1; j < u; j++) {
if (2 * (j - i + 1) - 2 - p[j] + p[i] < 0) continue;
ret += get2(p[i] - (i == 0 ? 0 : p[i - 1]) - 1, (j == u - 1 ? n + 1 : p[j + 1]) - p[j] - 1, 2 * (j - i + 1) - 2 - p[j] + p[i]);
//cout << ret << '\n';
if (j - i + 1 <= (p[j] - p[i] + 1) / 2) ret -= get2(p[i] - (i == 0 ? 0 : p[i - 1]) - 1, (j == u - 1 ? n + 1 : p[j + 1]) - p[j] - 1, (p[j] - p[i] + 1) / 2 - (j - i + 1));
//cout << i << " | " << j << " " << ret << '\n';
}
}
ret += u;
return ret;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
d.push_back(arr[i]);
}
sort(d.begin(), d.end()); d.resize(unique(d.begin(), d.end()) - d.begin());
for (int i = 1; i <= n; i++) arr[i] = lower_bound(d.begin(), d.end(), arr[i]) - d.begin();
for (int i = 1; i <= n; i++) {
occ[arr[i]].push_back(i);
}
bool flag = 1; for (int i = 1; i <= n; i++) flag |= (arr[i] <= 2);
if (n >= 1e5 && !flag) return 0;
ll sum = 0;
int x = sqrt(n); while (x * x < n) x++; while (x * x > n) x--;
for (int i = 0; i < n; i++) {
if (occ[i].empty()) continue;
if ((int)occ[i].size() > x) sum += solve1(i);
else {
sum += solve2(i);
}
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6756 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6656 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
3 ms |
6840 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9176 KB |
Output is correct |
2 |
Correct |
16 ms |
9684 KB |
Output is correct |
3 |
Correct |
14 ms |
8412 KB |
Output is correct |
4 |
Correct |
16 ms |
9688 KB |
Output is correct |
5 |
Correct |
17 ms |
9624 KB |
Output is correct |
6 |
Correct |
22 ms |
9944 KB |
Output is correct |
7 |
Correct |
17 ms |
9940 KB |
Output is correct |
8 |
Correct |
18 ms |
9944 KB |
Output is correct |
9 |
Correct |
17 ms |
9940 KB |
Output is correct |
10 |
Correct |
24 ms |
9924 KB |
Output is correct |
11 |
Correct |
14 ms |
9944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6756 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6656 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
3 ms |
6840 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
12 ms |
9176 KB |
Output is correct |
19 |
Correct |
16 ms |
9684 KB |
Output is correct |
20 |
Correct |
14 ms |
8412 KB |
Output is correct |
21 |
Correct |
16 ms |
9688 KB |
Output is correct |
22 |
Correct |
17 ms |
9624 KB |
Output is correct |
23 |
Correct |
22 ms |
9944 KB |
Output is correct |
24 |
Correct |
17 ms |
9940 KB |
Output is correct |
25 |
Correct |
18 ms |
9944 KB |
Output is correct |
26 |
Correct |
17 ms |
9940 KB |
Output is correct |
27 |
Correct |
24 ms |
9924 KB |
Output is correct |
28 |
Correct |
14 ms |
9944 KB |
Output is correct |
29 |
Correct |
24 ms |
10704 KB |
Output is correct |
30 |
Correct |
12 ms |
8160 KB |
Output is correct |
31 |
Correct |
23 ms |
9436 KB |
Output is correct |
32 |
Correct |
57 ms |
12932 KB |
Output is correct |
33 |
Correct |
25 ms |
9692 KB |
Output is correct |
34 |
Correct |
32 ms |
9940 KB |
Output is correct |
35 |
Correct |
16 ms |
8672 KB |
Output is correct |
36 |
Correct |
10 ms |
7904 KB |
Output is correct |
37 |
Correct |
12 ms |
8044 KB |
Output is correct |
38 |
Correct |
19 ms |
9944 KB |
Output is correct |
39 |
Correct |
23 ms |
9944 KB |
Output is correct |
40 |
Correct |
20 ms |
9928 KB |
Output is correct |
41 |
Correct |
19 ms |
9916 KB |
Output is correct |
42 |
Correct |
21 ms |
9688 KB |
Output is correct |
43 |
Correct |
36 ms |
11424 KB |
Output is correct |
44 |
Correct |
31 ms |
11468 KB |
Output is correct |
45 |
Correct |
31 ms |
11472 KB |
Output is correct |
46 |
Correct |
30 ms |
11472 KB |
Output is correct |
47 |
Correct |
31 ms |
11468 KB |
Output is correct |
48 |
Correct |
81 ms |
9936 KB |
Output is correct |
49 |
Correct |
91 ms |
9948 KB |
Output is correct |
50 |
Correct |
48 ms |
10192 KB |
Output is correct |
51 |
Correct |
47 ms |
10192 KB |
Output is correct |
52 |
Correct |
127 ms |
9940 KB |
Output is correct |
53 |
Correct |
120 ms |
9688 KB |
Output is correct |
54 |
Correct |
82 ms |
9676 KB |
Output is correct |