#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; 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 |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 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 |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
7 ms |
6748 KB |
Output is correct |
10 |
Correct |
8 ms |
6748 KB |
Output is correct |
11 |
Correct |
7 ms |
6968 KB |
Output is correct |
12 |
Correct |
8 ms |
6884 KB |
Output is correct |
13 |
Correct |
7 ms |
6736 KB |
Output is correct |
14 |
Correct |
8 ms |
6744 KB |
Output is correct |
15 |
Correct |
8 ms |
6756 KB |
Output is correct |
16 |
Correct |
8 ms |
6748 KB |
Output is correct |
17 |
Correct |
5 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
573 ms |
29048 KB |
Output is correct |
2 |
Correct |
777 ms |
37440 KB |
Output is correct |
3 |
Correct |
425 ms |
25208 KB |
Output is correct |
4 |
Correct |
805 ms |
41144 KB |
Output is correct |
5 |
Correct |
885 ms |
42160 KB |
Output is correct |
6 |
Correct |
912 ms |
43712 KB |
Output is correct |
7 |
Correct |
904 ms |
43700 KB |
Output is correct |
8 |
Correct |
896 ms |
43708 KB |
Output is correct |
9 |
Correct |
909 ms |
43708 KB |
Output is correct |
10 |
Correct |
899 ms |
43680 KB |
Output is correct |
11 |
Correct |
536 ms |
43712 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 |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
7 ms |
6748 KB |
Output is correct |
10 |
Correct |
8 ms |
6748 KB |
Output is correct |
11 |
Correct |
7 ms |
6968 KB |
Output is correct |
12 |
Correct |
8 ms |
6884 KB |
Output is correct |
13 |
Correct |
7 ms |
6736 KB |
Output is correct |
14 |
Correct |
8 ms |
6744 KB |
Output is correct |
15 |
Correct |
8 ms |
6756 KB |
Output is correct |
16 |
Correct |
8 ms |
6748 KB |
Output is correct |
17 |
Correct |
5 ms |
6748 KB |
Output is correct |
18 |
Correct |
573 ms |
29048 KB |
Output is correct |
19 |
Correct |
777 ms |
37440 KB |
Output is correct |
20 |
Correct |
425 ms |
25208 KB |
Output is correct |
21 |
Correct |
805 ms |
41144 KB |
Output is correct |
22 |
Correct |
885 ms |
42160 KB |
Output is correct |
23 |
Correct |
912 ms |
43712 KB |
Output is correct |
24 |
Correct |
904 ms |
43700 KB |
Output is correct |
25 |
Correct |
896 ms |
43708 KB |
Output is correct |
26 |
Correct |
909 ms |
43708 KB |
Output is correct |
27 |
Correct |
899 ms |
43680 KB |
Output is correct |
28 |
Correct |
536 ms |
43712 KB |
Output is correct |
29 |
Correct |
568 ms |
45188 KB |
Output is correct |
30 |
Correct |
166 ms |
12816 KB |
Output is correct |
31 |
Correct |
362 ms |
19840 KB |
Output is correct |
32 |
Correct |
980 ms |
43196 KB |
Output is correct |
33 |
Correct |
386 ms |
20272 KB |
Output is correct |
34 |
Correct |
410 ms |
22152 KB |
Output is correct |
35 |
Correct |
258 ms |
16584 KB |
Output is correct |
36 |
Correct |
144 ms |
12276 KB |
Output is correct |
37 |
Correct |
177 ms |
13356 KB |
Output is correct |
38 |
Correct |
995 ms |
43740 KB |
Output is correct |
39 |
Correct |
995 ms |
43756 KB |
Output is correct |
40 |
Correct |
993 ms |
43740 KB |
Output is correct |
41 |
Correct |
967 ms |
43712 KB |
Output is correct |
42 |
Correct |
986 ms |
43712 KB |
Output is correct |
43 |
Correct |
1024 ms |
45372 KB |
Output is correct |
44 |
Correct |
960 ms |
45244 KB |
Output is correct |
45 |
Correct |
973 ms |
45340 KB |
Output is correct |
46 |
Correct |
1000 ms |
45504 KB |
Output is correct |
47 |
Correct |
969 ms |
45200 KB |
Output is correct |
48 |
Correct |
941 ms |
44216 KB |
Output is correct |
49 |
Correct |
955 ms |
44056 KB |
Output is correct |
50 |
Correct |
933 ms |
43940 KB |
Output is correct |
51 |
Correct |
945 ms |
43708 KB |
Output is correct |
52 |
Correct |
925 ms |
43964 KB |
Output is correct |
53 |
Correct |
925 ms |
44096 KB |
Output is correct |
54 |
Correct |
923 ms |
43820 KB |
Output is correct |