#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 2e5 + 10;
int a[N]; ll ans = 0;
ve<int> occ[N];
inline static ll F(const int &l, const int &r) { return (l + r) * 1ll * (r - l + 1) / 2ll; }
inline void cnt(const int &l1, const int &r1,
const int &l2, const int &r2,
const int &C) {
int R = min(r2, r1 + C - 1);
int L = max(l2, l1 + C);
ans += max(0, R - l2 + 1) * 1ll * (r1 + 1);
ans -= l1 * 1ll * max(0, min(R, l1 + C - 1) - l2 + 1);
ans += max(0, R + 1 - max(l2, l1 + C) - 1 + 1) * 1ll * C;
if (L <= R) ans -= F(L + 1, R + 1);
}
inline void solve() {
int n;
cin >> n;
ve<int> al;
for (int i = 0; i < n; ++i) cin >> a[i], al.pb(a[i]);
sort(all(al)), al.erase(unique(all(al)), al.end());
for (int i = 0; i < n; ++i) a[i] = lower_bound(all(al), a[i]) - al.begin();
for (int i = 0; i < n; ++i) occ[a[i]].pb(i);
const int b = 1800;
ve<int> p(n);
for (int el = 0; el < sz(al); ++el) {
if (sz(occ[el]) > b) {
oset<int> st; st.insert(0);
for (int i = 0; i < n; ++i) {
p[i] = (a[i] == el ? 1 : -1);
if (i) p[i] += p[i - 1];
ans += st.order_of_key(p[i]);
st.insert(p[i]);
}
}
else {
for (int l = 0; l < sz(occ[el]); ++l) {
for (int r = l; r < sz(occ[el]); ++r) {
int r1 = occ[el][l] - 1, l1 = (!l ? -1 : occ[el][l - 1]);
int l2 = occ[el][r], r2 = (r + 1 == sz(occ[el]) ? n - 1 : occ[el][r + 1] - 1);
cnt(l1, r1, l2, r2, 2 * (r - l + 1));
}
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5044 KB |
Output is correct |
14 |
Correct |
4 ms |
5060 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
13920 KB |
Output is correct |
2 |
Correct |
134 ms |
16264 KB |
Output is correct |
3 |
Correct |
74 ms |
11528 KB |
Output is correct |
4 |
Correct |
142 ms |
16872 KB |
Output is correct |
5 |
Correct |
151 ms |
17352 KB |
Output is correct |
6 |
Correct |
156 ms |
18072 KB |
Output is correct |
7 |
Correct |
156 ms |
17988 KB |
Output is correct |
8 |
Correct |
155 ms |
17856 KB |
Output is correct |
9 |
Correct |
159 ms |
18068 KB |
Output is correct |
10 |
Correct |
157 ms |
18004 KB |
Output is correct |
11 |
Correct |
145 ms |
17940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5044 KB |
Output is correct |
14 |
Correct |
4 ms |
5060 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5172 KB |
Output is correct |
18 |
Correct |
102 ms |
13920 KB |
Output is correct |
19 |
Correct |
134 ms |
16264 KB |
Output is correct |
20 |
Correct |
74 ms |
11528 KB |
Output is correct |
21 |
Correct |
142 ms |
16872 KB |
Output is correct |
22 |
Correct |
151 ms |
17352 KB |
Output is correct |
23 |
Correct |
156 ms |
18072 KB |
Output is correct |
24 |
Correct |
156 ms |
17988 KB |
Output is correct |
25 |
Correct |
155 ms |
17856 KB |
Output is correct |
26 |
Correct |
159 ms |
18068 KB |
Output is correct |
27 |
Correct |
157 ms |
18004 KB |
Output is correct |
28 |
Correct |
145 ms |
17940 KB |
Output is correct |
29 |
Correct |
153 ms |
19452 KB |
Output is correct |
30 |
Correct |
16 ms |
6868 KB |
Output is correct |
31 |
Correct |
29 ms |
8364 KB |
Output is correct |
32 |
Correct |
72 ms |
12172 KB |
Output is correct |
33 |
Correct |
31 ms |
8596 KB |
Output is correct |
34 |
Correct |
32 ms |
8684 KB |
Output is correct |
35 |
Correct |
21 ms |
7512 KB |
Output is correct |
36 |
Correct |
13 ms |
6536 KB |
Output is correct |
37 |
Correct |
16 ms |
6836 KB |
Output is correct |
38 |
Correct |
304 ms |
18188 KB |
Output is correct |
39 |
Correct |
312 ms |
18164 KB |
Output is correct |
40 |
Correct |
302 ms |
18196 KB |
Output is correct |
41 |
Correct |
309 ms |
18168 KB |
Output is correct |
42 |
Correct |
299 ms |
18120 KB |
Output is correct |
43 |
Correct |
629 ms |
19960 KB |
Output is correct |
44 |
Correct |
676 ms |
19840 KB |
Output is correct |
45 |
Correct |
623 ms |
19780 KB |
Output is correct |
46 |
Correct |
642 ms |
19848 KB |
Output is correct |
47 |
Correct |
632 ms |
19860 KB |
Output is correct |
48 |
Correct |
437 ms |
9104 KB |
Output is correct |
49 |
Correct |
434 ms |
9028 KB |
Output is correct |
50 |
Execution timed out |
3068 ms |
18504 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |