#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 = 2e5+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 |
2 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6628 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6532 KB |
Output is correct |
14 |
Correct |
4 ms |
6488 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6632 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9240 KB |
Output is correct |
2 |
Correct |
19 ms |
9688 KB |
Output is correct |
3 |
Correct |
12 ms |
8668 KB |
Output is correct |
4 |
Correct |
19 ms |
9684 KB |
Output is correct |
5 |
Correct |
20 ms |
9680 KB |
Output is correct |
6 |
Runtime error |
29 ms |
19408 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6628 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6532 KB |
Output is correct |
14 |
Correct |
4 ms |
6488 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6632 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
18 |
Correct |
16 ms |
9240 KB |
Output is correct |
19 |
Correct |
19 ms |
9688 KB |
Output is correct |
20 |
Correct |
12 ms |
8668 KB |
Output is correct |
21 |
Correct |
19 ms |
9684 KB |
Output is correct |
22 |
Correct |
20 ms |
9680 KB |
Output is correct |
23 |
Runtime error |
29 ms |
19408 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |