#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n;
int a[maxn];
vector<int> byPosition[maxn];
void read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
}
int b[maxn];
int pref[maxn];
long long divideAndConquer(int l, int r)
{
if (l > r)
{
return 0;
}
if (l == r)
{
return 1;
}
int mid = (l + r) / 2;
set<int> candidates;
unordered_map<int, int> cnt;
for (int i = mid - 1; i >= l; --i)
{
++cnt[a[i]];
if (cnt[a[i]] > (mid - i) / 2)
{
candidates.insert(a[i]);
}
}
cnt.clear();
for (int i = mid; i <= r; ++i)
{
++cnt[a[i]];
if (cnt[a[i]] > (i - mid + 1) / 2)
{
candidates.insert(a[i]);
}
}
long long ans = 0;
for (auto currentCandidate : candidates)
{
for (int i = l; i <= r; ++i)
{
if (a[i] == currentCandidate)
{
b[i] = +1;
}
else
{
b[i] = -1;
}
}
cnt.clear();
int sum = 0;
++cnt[sum];
for (int i = l; i <= mid - 1; ++i)
{
sum += b[i];
++cnt[sum];
}
int upTo = (r - l + 1);
for (int i = -upTo; i <= upTo; ++i)
{
cnt[i] += cnt[i - 1];
}
for (int i = mid; i <= r; ++i)
{
sum += b[i];
ans += cnt[sum - 1];
}
}
return ans + divideAndConquer(l, mid - 1) + divideAndConquer(mid + 1, r);
}
void solve()
{
cout << divideAndConquer(1, n) << endl;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
7 ms |
6744 KB |
Output is correct |
10 |
Correct |
7 ms |
6748 KB |
Output is correct |
11 |
Correct |
8 ms |
6748 KB |
Output is correct |
12 |
Correct |
7 ms |
6748 KB |
Output is correct |
13 |
Correct |
7 ms |
6748 KB |
Output is correct |
14 |
Correct |
7 ms |
6748 KB |
Output is correct |
15 |
Correct |
7 ms |
6748 KB |
Output is correct |
16 |
Correct |
7 ms |
6744 KB |
Output is correct |
17 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
570 ms |
29052 KB |
Output is correct |
2 |
Correct |
785 ms |
37472 KB |
Output is correct |
3 |
Correct |
425 ms |
24956 KB |
Output is correct |
4 |
Correct |
826 ms |
40892 KB |
Output is correct |
5 |
Correct |
858 ms |
41660 KB |
Output is correct |
6 |
Correct |
876 ms |
43368 KB |
Output is correct |
7 |
Correct |
886 ms |
43600 KB |
Output is correct |
8 |
Correct |
883 ms |
43348 KB |
Output is correct |
9 |
Correct |
880 ms |
43204 KB |
Output is correct |
10 |
Correct |
884 ms |
43368 KB |
Output is correct |
11 |
Correct |
524 ms |
43200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
7 ms |
6744 KB |
Output is correct |
10 |
Correct |
7 ms |
6748 KB |
Output is correct |
11 |
Correct |
8 ms |
6748 KB |
Output is correct |
12 |
Correct |
7 ms |
6748 KB |
Output is correct |
13 |
Correct |
7 ms |
6748 KB |
Output is correct |
14 |
Correct |
7 ms |
6748 KB |
Output is correct |
15 |
Correct |
7 ms |
6748 KB |
Output is correct |
16 |
Correct |
7 ms |
6744 KB |
Output is correct |
17 |
Correct |
4 ms |
6748 KB |
Output is correct |
18 |
Correct |
570 ms |
29052 KB |
Output is correct |
19 |
Correct |
785 ms |
37472 KB |
Output is correct |
20 |
Correct |
425 ms |
24956 KB |
Output is correct |
21 |
Correct |
826 ms |
40892 KB |
Output is correct |
22 |
Correct |
858 ms |
41660 KB |
Output is correct |
23 |
Correct |
876 ms |
43368 KB |
Output is correct |
24 |
Correct |
886 ms |
43600 KB |
Output is correct |
25 |
Correct |
883 ms |
43348 KB |
Output is correct |
26 |
Correct |
880 ms |
43204 KB |
Output is correct |
27 |
Correct |
884 ms |
43368 KB |
Output is correct |
28 |
Correct |
524 ms |
43200 KB |
Output is correct |
29 |
Correct |
566 ms |
43200 KB |
Output is correct |
30 |
Correct |
172 ms |
12648 KB |
Output is correct |
31 |
Correct |
352 ms |
18900 KB |
Output is correct |
32 |
Correct |
954 ms |
41888 KB |
Output is correct |
33 |
Correct |
383 ms |
19652 KB |
Output is correct |
34 |
Correct |
400 ms |
21444 KB |
Output is correct |
35 |
Correct |
259 ms |
16324 KB |
Output is correct |
36 |
Correct |
140 ms |
12132 KB |
Output is correct |
37 |
Correct |
179 ms |
12896 KB |
Output is correct |
38 |
Correct |
990 ms |
43232 KB |
Output is correct |
39 |
Correct |
937 ms |
43524 KB |
Output is correct |
40 |
Correct |
986 ms |
43684 KB |
Output is correct |
41 |
Correct |
970 ms |
43260 KB |
Output is correct |
42 |
Correct |
987 ms |
43196 KB |
Output is correct |
43 |
Correct |
962 ms |
43200 KB |
Output is correct |
44 |
Correct |
966 ms |
43196 KB |
Output is correct |
45 |
Correct |
954 ms |
43200 KB |
Output is correct |
46 |
Correct |
963 ms |
43344 KB |
Output is correct |
47 |
Correct |
958 ms |
43172 KB |
Output is correct |
48 |
Correct |
943 ms |
43452 KB |
Output is correct |
49 |
Correct |
938 ms |
43476 KB |
Output is correct |
50 |
Correct |
921 ms |
43196 KB |
Output is correct |
51 |
Correct |
934 ms |
43344 KB |
Output is correct |
52 |
Correct |
926 ms |
43452 KB |
Output is correct |
53 |
Correct |
915 ms |
43600 KB |
Output is correct |
54 |
Correct |
914 ms |
43172 KB |
Output is correct |