#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pw(x) (1ll << x)
#define sz(x) (int)((x).size())
#define pb push_back
#define endl '\n'
#define mp make_pair
#define vec vector
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef unsigned long long ull;
template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
struct fen {
vector <int> t;
int norm;
fen (int n = 0) : norm(n >> 1), t(n + 100) {}
inline void upd (int i, int x) { for (i += norm; i < sz(t); i += (i & -i)) t[i] += x; }
inline int get (int i) { int res = 0; for (i += norm; i; i -= (i & -i)) res += t[i]; return res; }
inline int get (int l, int r) { return get(r) - get(l - 1); }
};
inline void solve () {
int n; cin >> n;
vector <int> a (n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i], --a[i];
map <int, vector <int>> mp;
for (int i = 1; i <= n; ++i) mp[a[i]].pb(i);
const int N = n * 4 + 100;
const int B = 1800;
ll ans = 0;
function <ll(ll, ll, ll)> ff = [&] (ll L, ll R, ll k) {
umin(L, k); umin(R, k);
assert(L <= 5);
ll rett = 0;
for (int xs = 0; xs <= L; ++xs) rett += min(R, k - xs) + 1;
return rett;
};
for (auto &[_, pos] : mp) {
if (sz(pos) > B) {
vector <int> c (n + 1);
fen t (N); t.upd(0, 1);
for (int i = 1; i <= n; ++i) c[i] = c[i - 1] + (a[i] == _ ? +1 : 0);
for (int i = 1; i <= n; ++i) {
ans += t.get(2 * c[i] - i - 1);
t.upd(2 * c[i] - i, 1);
}
}
else {
for (int i = 0; i < sz(pos); ++i) {
for (int j = i; j < sz(pos); ++j) {
int c = j - i + 1;
int len = pos[j] - pos[i] + 1;
int k = 2 * c - len - 1;
int L = (i ? pos[i] - pos[i - 1] - 1 : pos[i] - 1);
int R = (j + 1 < sz(pos) ? pos[j + 1] - pos[j] - 1 : n - pos[j]);
ans += ff(L, R, k);
}
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1; // cin >> t;
while (t--) solve();
return 0;
}
Compilation message
Main.cpp: In constructor 'fen::fen(long long int)':
Main.cpp:33:13: warning: 'fen::norm' will be initialized after [-Wreorder]
33 | int norm;
| ^~~~
Main.cpp:32:22: warning: 'std::vector<long long int> fen::t' [-Wreorder]
32 | vector <int> t;
| ^
Main.cpp:34:9: warning: when initialized here [-Wreorder]
34 | fen (int n = 0) : norm(n >> 1), t(n + 100) {}
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7820 KB |
Output is correct |
2 |
Correct |
31 ms |
9900 KB |
Output is correct |
3 |
Correct |
17 ms |
6004 KB |
Output is correct |
4 |
Correct |
30 ms |
10480 KB |
Output is correct |
5 |
Correct |
32 ms |
10760 KB |
Output is correct |
6 |
Correct |
36 ms |
11516 KB |
Output is correct |
7 |
Correct |
35 ms |
11452 KB |
Output is correct |
8 |
Correct |
35 ms |
11552 KB |
Output is correct |
9 |
Correct |
35 ms |
11408 KB |
Output is correct |
10 |
Correct |
33 ms |
11336 KB |
Output is correct |
11 |
Correct |
22 ms |
11340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
24 ms |
7820 KB |
Output is correct |
19 |
Correct |
31 ms |
9900 KB |
Output is correct |
20 |
Correct |
17 ms |
6004 KB |
Output is correct |
21 |
Correct |
30 ms |
10480 KB |
Output is correct |
22 |
Correct |
32 ms |
10760 KB |
Output is correct |
23 |
Correct |
36 ms |
11516 KB |
Output is correct |
24 |
Correct |
35 ms |
11452 KB |
Output is correct |
25 |
Correct |
35 ms |
11552 KB |
Output is correct |
26 |
Correct |
35 ms |
11408 KB |
Output is correct |
27 |
Correct |
33 ms |
11336 KB |
Output is correct |
28 |
Correct |
22 ms |
11340 KB |
Output is correct |
29 |
Correct |
31 ms |
11332 KB |
Output is correct |
30 |
Correct |
15 ms |
4308 KB |
Output is correct |
31 |
Correct |
38 ms |
7884 KB |
Output is correct |
32 |
Correct |
146 ms |
15404 KB |
Output is correct |
33 |
Correct |
59 ms |
8260 KB |
Output is correct |
34 |
Correct |
48 ms |
8560 KB |
Output is correct |
35 |
Correct |
24 ms |
5976 KB |
Output is correct |
36 |
Correct |
17 ms |
3924 KB |
Output is correct |
37 |
Correct |
17 ms |
4564 KB |
Output is correct |
38 |
Correct |
63 ms |
11440 KB |
Output is correct |
39 |
Correct |
59 ms |
11440 KB |
Output is correct |
40 |
Correct |
59 ms |
11368 KB |
Output is correct |
41 |
Correct |
69 ms |
11428 KB |
Output is correct |
42 |
Correct |
66 ms |
11340 KB |
Output is correct |
43 |
Correct |
137 ms |
11752 KB |
Output is correct |
44 |
Correct |
116 ms |
11788 KB |
Output is correct |
45 |
Correct |
120 ms |
11524 KB |
Output is correct |
46 |
Correct |
119 ms |
11664 KB |
Output is correct |
47 |
Correct |
119 ms |
11756 KB |
Output is correct |
48 |
Correct |
488 ms |
3996 KB |
Output is correct |
49 |
Correct |
472 ms |
4044 KB |
Output is correct |
50 |
Correct |
766 ms |
12500 KB |
Output is correct |
51 |
Correct |
769 ms |
12524 KB |
Output is correct |
52 |
Correct |
269 ms |
3716 KB |
Output is correct |
53 |
Correct |
265 ms |
3560 KB |
Output is correct |
54 |
Correct |
471 ms |
3752 KB |
Output is correct |