# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
866238 |
2023-10-25T15:50:45 Z |
TAhmed33 |
Izbori (COCI22_izbori) |
C++ |
|
3000 ms |
13016 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll trig (ll x) { return ((x * (x + 1)) >> 1); }
inline ll get2 (ll a, ll b, ll c) {
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;
ll arr[MAXN], n;
vector <ll> occ[MAXN];
vector <int> d;
ll a[2 * MAXN + 25];
ll solve1 (int x) {
memset(a, 0, sizeof(a));
ll ret = 0; ll cur = 0; ll 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;
}
ll solve2 (ll x) {
ll ret = (ll)occ[x].size();
for (ll i = 0; i < (ll)occ[x].size() - 1; i--) {
for (ll j = i + 1; j < (ll)occ[x].size(); j++) {
ret += get2(occ[x][i] - (i == 0 ? 0 : occ[x][i - 1]), (j == (ll)occ[x].size() - 1 ? n + 1 : occ[x][j + 1]) - occ[x][j], 2 * (j - i + 1) - 2 - occ[x][j] + occ[x][i]);
}
}
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);
}
ll sum = 0;
ll 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 ((ll)occ[i].size() > x / 20) sum += solve1(i);
else {
sum += solve2(i);
}
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
4 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
4 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
7 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8792 KB |
Output is correct |
10 |
Correct |
3 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8904 KB |
Output is correct |
12 |
Correct |
3 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
8888 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11988 KB |
Output is correct |
2 |
Correct |
18 ms |
11748 KB |
Output is correct |
3 |
Correct |
9 ms |
10344 KB |
Output is correct |
4 |
Correct |
16 ms |
13016 KB |
Output is correct |
5 |
Correct |
17 ms |
12756 KB |
Output is correct |
6 |
Correct |
17 ms |
12184 KB |
Output is correct |
7 |
Correct |
17 ms |
12292 KB |
Output is correct |
8 |
Correct |
19 ms |
12344 KB |
Output is correct |
9 |
Correct |
17 ms |
13012 KB |
Output is correct |
10 |
Correct |
18 ms |
13016 KB |
Output is correct |
11 |
Correct |
15 ms |
12340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
4 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
7 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8792 KB |
Output is correct |
10 |
Correct |
3 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8904 KB |
Output is correct |
12 |
Correct |
3 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
8888 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
12 ms |
11988 KB |
Output is correct |
19 |
Correct |
18 ms |
11748 KB |
Output is correct |
20 |
Correct |
9 ms |
10344 KB |
Output is correct |
21 |
Correct |
16 ms |
13016 KB |
Output is correct |
22 |
Correct |
17 ms |
12756 KB |
Output is correct |
23 |
Correct |
17 ms |
12184 KB |
Output is correct |
24 |
Correct |
17 ms |
12292 KB |
Output is correct |
25 |
Correct |
19 ms |
12344 KB |
Output is correct |
26 |
Correct |
17 ms |
13012 KB |
Output is correct |
27 |
Correct |
18 ms |
13016 KB |
Output is correct |
28 |
Correct |
15 ms |
12340 KB |
Output is correct |
29 |
Correct |
21 ms |
12076 KB |
Output is correct |
30 |
Execution timed out |
3054 ms |
7648 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |