/*
I find it wholesome to be alone the greater part of the time.
To be in company, even with the best, is soon wearisome and dissipating.
I love to be alone.
I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 4e5 + 20;
//const int Mod = 1e9 + 7;
//const ll inf = 0x3f3f3f3f3f3f3f3f;
int n;
vector <int> S[maxN];
int a[maxN];
int dp[maxN];
ll ans;
ll calc(int i, int j, int k){
int l = k - i;
int 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 Light(int c){
for (int i = 0; i < signed(S[c].size()); ++i){
for (int j = i; j < signed(S[c].size()); ++j){
int l = i ? S[c][i - 1] : 0;
int r = (j + 1 == S[c].size()) ? n + 1 : S[c][j + 1];
int k = (j - i + 1) - (S[c][j] - S[c][i] - j + i);
ans += calc(S[c][i] - l - 1, r - S[c][j] - 1, k);
}
}
}
void Heavy(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;
}
ans += dp[cur - 1];
++dp[cur];
}
}
void Init(){
cin >> n;
vector <int> node;
for (int i = 1; i <= n; ++i){
cin >> a[i];
node.pb(a[i]);
}
sort(node.begin(), node.end());
node.resize(unique(node.begin(), node.end()) - node.begin());
for (int i = 1; i <= n; ++i){
a[i] = upper_bound(node.begin(), node.end(), a[i]) - node.begin();
S[a[i]].pb(i);
}
for (int i = 1; i <= n; ++i){
if (S[i].empty()) continue;
if (S[i].size() <= 318) Light(i);
else Heavy(i);
}
cout << ans;
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
Main.cpp: In function 'void Light(long long int)':
Main.cpp:41:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | int r = (j + 1 == S[c].size()) ? n + 1 : S[c][j + 1];
| ~~~~~~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
8 ms |
9812 KB |
Output is correct |
10 |
Correct |
8 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9816 KB |
Output is correct |
17 |
Correct |
7 ms |
12884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
17100 KB |
Output is correct |
2 |
Correct |
24 ms |
18252 KB |
Output is correct |
3 |
Correct |
17 ms |
16072 KB |
Output is correct |
4 |
Correct |
25 ms |
18316 KB |
Output is correct |
5 |
Correct |
25 ms |
18252 KB |
Output is correct |
6 |
Correct |
30 ms |
18884 KB |
Output is correct |
7 |
Correct |
28 ms |
18904 KB |
Output is correct |
8 |
Correct |
26 ms |
18976 KB |
Output is correct |
9 |
Correct |
29 ms |
18488 KB |
Output is correct |
10 |
Correct |
26 ms |
18460 KB |
Output is correct |
11 |
Correct |
23 ms |
17628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
8 ms |
9812 KB |
Output is correct |
10 |
Correct |
8 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9816 KB |
Output is correct |
17 |
Correct |
7 ms |
12884 KB |
Output is correct |
18 |
Correct |
20 ms |
17100 KB |
Output is correct |
19 |
Correct |
24 ms |
18252 KB |
Output is correct |
20 |
Correct |
17 ms |
16072 KB |
Output is correct |
21 |
Correct |
25 ms |
18316 KB |
Output is correct |
22 |
Correct |
25 ms |
18252 KB |
Output is correct |
23 |
Correct |
30 ms |
18884 KB |
Output is correct |
24 |
Correct |
28 ms |
18904 KB |
Output is correct |
25 |
Correct |
26 ms |
18976 KB |
Output is correct |
26 |
Correct |
29 ms |
18488 KB |
Output is correct |
27 |
Correct |
26 ms |
18460 KB |
Output is correct |
28 |
Correct |
23 ms |
17628 KB |
Output is correct |
29 |
Correct |
33 ms |
19580 KB |
Output is correct |
30 |
Correct |
18 ms |
11548 KB |
Output is correct |
31 |
Correct |
35 ms |
13396 KB |
Output is correct |
32 |
Correct |
96 ms |
17904 KB |
Output is correct |
33 |
Correct |
38 ms |
13528 KB |
Output is correct |
34 |
Correct |
35 ms |
13800 KB |
Output is correct |
35 |
Correct |
24 ms |
12392 KB |
Output is correct |
36 |
Correct |
16 ms |
11348 KB |
Output is correct |
37 |
Correct |
19 ms |
11784 KB |
Output is correct |
38 |
Correct |
29 ms |
19452 KB |
Output is correct |
39 |
Correct |
31 ms |
18884 KB |
Output is correct |
40 |
Correct |
30 ms |
19140 KB |
Output is correct |
41 |
Correct |
36 ms |
19144 KB |
Output is correct |
42 |
Correct |
30 ms |
18884 KB |
Output is correct |
43 |
Correct |
43 ms |
20456 KB |
Output is correct |
44 |
Correct |
45 ms |
20464 KB |
Output is correct |
45 |
Correct |
45 ms |
20340 KB |
Output is correct |
46 |
Correct |
44 ms |
20416 KB |
Output is correct |
47 |
Correct |
44 ms |
20448 KB |
Output is correct |
48 |
Correct |
114 ms |
18684 KB |
Output is correct |
49 |
Correct |
107 ms |
18624 KB |
Output is correct |
50 |
Correct |
61 ms |
19196 KB |
Output is correct |
51 |
Correct |
66 ms |
19244 KB |
Output is correct |
52 |
Correct |
156 ms |
18456 KB |
Output is correct |
53 |
Correct |
146 ms |
18352 KB |
Output is correct |
54 |
Correct |
96 ms |
18500 KB |
Output is correct |