#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 4e5+5, BS = 500;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n, a[SZ], cnt[SZ];
vector<int> cprs, v1, v2, pos[SZ];
ll dp[SZ], res = 0;
ll calc(ll i, ll j, ll k){
ll l = k - i, r = k;
k = max(0LL, r - max(j + 1, l - 1));
l = max(l, 0LL);
r = min(r, j + 1);
if (l > r) return k * (j + 1);
return 1LL * (l + r) * (r - l + 1) / 2 + k * (j + 1);
}
void solve2(int c){
for (int i = 0; i < pos[c].size(); ++i){
for (int j = i; j < pos[c].size(); ++j){
int l = i ? pos[c][i - 1] : 0;
int r = (j + 1 == pos[c].size()) ? n + 1 : pos[c][j + 1];
int k = (j - i + 1) - (pos[c][j] - pos[c][i] - j + i);
res += calc(pos[c][i] - l - 1, r - pos[c][j] - 1, k);
}
}
}
void solve1(int c){
memset(dp, 0, sizeof(dp));
int cur = n + 4;
dp[cur] = 1;
for (int i = 1; i <= n; ++i){
if (a[i] == c){
++cur;
dp[cur] += dp[cur - 1];
}
else{
dp[cur] -= dp[cur - 1];
--cur;
}
res += dp[cur - 1];
++dp[cur];
}
}
int main()
{
init();
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
cprs.pb(a[i]);
}
sort(all(cprs));
for(int i = 1; i <= n; i++)
{
a[i] = lwb(all(cprs), a[i]) - cprs.begin() + 1;
cnt[a[i]]++;
pos[a[i]].pb(i);
}
for(int i = 1; i <= n; i++)
{
if(pos[i].empty()) continue;
if(cnt[i] >= BS) solve1(i);
else solve2(i);
}
cout << res;
}
Compilation message
Main.cpp: In function 'void solve2(int)':
Main.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < pos[c].size(); ++i){
| ~~^~~~~~~~~~~~~~~
Main.cpp:43:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int j = i; j < pos[c].size(); ++j){
| ~~^~~~~~~~~~~~~~~
Main.cpp:45:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | int r = (j + 1 == pos[c].size()) ? n + 1 : pos[c][j + 1];
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
5 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
4 ms |
12632 KB |
Output is correct |
10 |
Correct |
4 ms |
12632 KB |
Output is correct |
11 |
Correct |
4 ms |
12636 KB |
Output is correct |
12 |
Correct |
4 ms |
12636 KB |
Output is correct |
13 |
Correct |
5 ms |
12636 KB |
Output is correct |
14 |
Correct |
5 ms |
12636 KB |
Output is correct |
15 |
Correct |
5 ms |
12636 KB |
Output is correct |
16 |
Correct |
4 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16600 KB |
Output is correct |
2 |
Correct |
22 ms |
17196 KB |
Output is correct |
3 |
Correct |
14 ms |
16092 KB |
Output is correct |
4 |
Correct |
22 ms |
17220 KB |
Output is correct |
5 |
Correct |
22 ms |
17108 KB |
Output is correct |
6 |
Correct |
24 ms |
17368 KB |
Output is correct |
7 |
Correct |
24 ms |
17624 KB |
Output is correct |
8 |
Correct |
24 ms |
17872 KB |
Output is correct |
9 |
Correct |
24 ms |
17612 KB |
Output is correct |
10 |
Correct |
25 ms |
17740 KB |
Output is correct |
11 |
Correct |
18 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
5 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
4 ms |
12632 KB |
Output is correct |
10 |
Correct |
4 ms |
12632 KB |
Output is correct |
11 |
Correct |
4 ms |
12636 KB |
Output is correct |
12 |
Correct |
4 ms |
12636 KB |
Output is correct |
13 |
Correct |
5 ms |
12636 KB |
Output is correct |
14 |
Correct |
5 ms |
12636 KB |
Output is correct |
15 |
Correct |
5 ms |
12636 KB |
Output is correct |
16 |
Correct |
4 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
17 ms |
16600 KB |
Output is correct |
19 |
Correct |
22 ms |
17196 KB |
Output is correct |
20 |
Correct |
14 ms |
16092 KB |
Output is correct |
21 |
Correct |
22 ms |
17220 KB |
Output is correct |
22 |
Correct |
22 ms |
17108 KB |
Output is correct |
23 |
Correct |
24 ms |
17368 KB |
Output is correct |
24 |
Correct |
24 ms |
17624 KB |
Output is correct |
25 |
Correct |
24 ms |
17872 KB |
Output is correct |
26 |
Correct |
24 ms |
17612 KB |
Output is correct |
27 |
Correct |
25 ms |
17740 KB |
Output is correct |
28 |
Correct |
18 ms |
17236 KB |
Output is correct |
29 |
Correct |
26 ms |
18988 KB |
Output is correct |
30 |
Correct |
19 ms |
14044 KB |
Output is correct |
31 |
Correct |
27 ms |
15324 KB |
Output is correct |
32 |
Correct |
72 ms |
18580 KB |
Output is correct |
33 |
Correct |
28 ms |
15768 KB |
Output is correct |
34 |
Correct |
30 ms |
15836 KB |
Output is correct |
35 |
Correct |
19 ms |
14804 KB |
Output is correct |
36 |
Correct |
12 ms |
14044 KB |
Output is correct |
37 |
Correct |
16 ms |
14304 KB |
Output is correct |
38 |
Correct |
26 ms |
17616 KB |
Output is correct |
39 |
Correct |
27 ms |
17624 KB |
Output is correct |
40 |
Correct |
26 ms |
17616 KB |
Output is correct |
41 |
Correct |
28 ms |
17880 KB |
Output is correct |
42 |
Correct |
28 ms |
17620 KB |
Output is correct |
43 |
Correct |
38 ms |
19408 KB |
Output is correct |
44 |
Correct |
39 ms |
19152 KB |
Output is correct |
45 |
Correct |
39 ms |
19340 KB |
Output is correct |
46 |
Correct |
38 ms |
19152 KB |
Output is correct |
47 |
Correct |
42 ms |
19512 KB |
Output is correct |
48 |
Correct |
108 ms |
17616 KB |
Output is correct |
49 |
Correct |
109 ms |
17736 KB |
Output is correct |
50 |
Correct |
64 ms |
18036 KB |
Output is correct |
51 |
Correct |
63 ms |
18136 KB |
Output is correct |
52 |
Correct |
182 ms |
15308 KB |
Output is correct |
53 |
Correct |
147 ms |
17620 KB |
Output is correct |
54 |
Correct |
93 ms |
17612 KB |
Output is correct |