#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
template<class T>
struct range_add_range_sum_query_solver{
int n;
vector<T> data0, data1;
range_add_range_sum_query_solver(){ }
// O(n)
range_add_range_sum_query_solver(int n): n(n), data0(n), data1(n){ }
// O(n)
range_add_range_sum_query_solver(int n, T init): range_add_range_sum_query_solver(vector<T>(n, init)){ }
// O(n)
range_add_range_sum_query_solver(const vector<T> &v): n((int)v.size()), data0(n), data1(v){
for(auto i = 1; i <= n; ++ i) if(i + (i & -i) <= n) data1[i + (i & -i) - 1] += data1[i - 1];
}
void update(int ql, int qr, T x){
assert(0 <= ql && ql <= qr && qr <= n);
if(ql == qr) return;
for(auto l = ql + 1; l <= n; l += l & -l) data0[l - 1] += x, data1[l - 1] -= ql * x;
for(auto r = qr + 1; r <= n; r += r & -r) data0[r - 1] -= x, data1[r - 1] += qr * x;
}
T pref(int qr) const{
assert(0 <= qr && qr <= n);
T sum0 = {}, sum1 = {};
for(auto r = qr; r > 0; r -= r & -r) sum0 += data0[r - 1], sum1 += data1[r - 1];
return qr * sum0 + sum1;
}
T query(int l, int r) const{
assert(0 <= l && l <= r && r <= n);
return pref(r) - pref(l);
}
template<class output_stream>
friend output_stream &operator<<(output_stream &out, const range_add_range_sum_query_solver<T> &solver){
out << "[";
for(auto i = 0; i < solver.n; ++ i){
out << solver.query(i, i + 1);
if(i != solver.n - 1) out << ", ";
}
return out << ']';
}
};
const int N = 2e5 + 5;
range_add_range_sum_query_solver <int> seg(2 * N);
int n;
int a[N];
vi b[N];
void compress(){
vi b(a + 1, a + n + 1);
sort(bend(b));
b.erase(unique(bend(b)), b.end());
ForE(i, 1, n){
a[i] = lower_bound(bend(b), a[i]) - b.begin() + 1;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n;
ForE(i, 1, n){
cin >> a[i];
}
compress();
ForE(i, 1, n){
b[a[i]].emplace_back(i);
}
ll ans = 0;
ForE(val, 1, n){
vpii updates;
updates.emplace_back(0 + N, 1 + N);
seg.update(0 + N, 1 + N, 1);
int sum = 0;
int l = 1;
Fora(i, b[val]){
int r = i - 1;
if (l <= r){
int vall = 2 * sum - r, valr = 2 * sum - l;
for (int j = valr; j >= vall; j--){
int tans = seg.pref(j + N);
if (tans == 0){
break;
}
ans += tans;
}
updates.emplace_back(vall + N, valr + 1 + N);
seg.update(vall + N, valr + 1 + N, 1);
}
l = r + 1;
sum++;
}
int r = n;
int vall = 2 * sum - r, valr = 2 * sum - l;
for (int j = valr; j >= vall; j--){
int tans = seg.pref(j + N);
if (tans == 0){
break;
}
ans += tans;
}
Fora(update, updates){
seg.update(update.fi, update.se, -1);
}
}
cout << ans << endl;
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8176 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8176 KB |
Output is correct |
4 |
Correct |
5 ms |
8096 KB |
Output is correct |
5 |
Correct |
4 ms |
8172 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8176 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8176 KB |
Output is correct |
4 |
Correct |
5 ms |
8096 KB |
Output is correct |
5 |
Correct |
4 ms |
8172 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
6 ms |
8148 KB |
Output is correct |
10 |
Correct |
6 ms |
8180 KB |
Output is correct |
11 |
Correct |
6 ms |
8148 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8128 KB |
Output is correct |
16 |
Correct |
6 ms |
8176 KB |
Output is correct |
17 |
Correct |
5 ms |
8220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
11244 KB |
Output is correct |
2 |
Correct |
113 ms |
11712 KB |
Output is correct |
3 |
Correct |
65 ms |
10268 KB |
Output is correct |
4 |
Correct |
111 ms |
12076 KB |
Output is correct |
5 |
Correct |
110 ms |
12012 KB |
Output is correct |
6 |
Correct |
125 ms |
12132 KB |
Output is correct |
7 |
Correct |
125 ms |
12028 KB |
Output is correct |
8 |
Correct |
144 ms |
12120 KB |
Output is correct |
9 |
Correct |
118 ms |
12080 KB |
Output is correct |
10 |
Correct |
128 ms |
12156 KB |
Output is correct |
11 |
Correct |
130 ms |
13112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8176 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8176 KB |
Output is correct |
4 |
Correct |
5 ms |
8096 KB |
Output is correct |
5 |
Correct |
4 ms |
8172 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
6 ms |
8148 KB |
Output is correct |
10 |
Correct |
6 ms |
8180 KB |
Output is correct |
11 |
Correct |
6 ms |
8148 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8128 KB |
Output is correct |
16 |
Correct |
6 ms |
8176 KB |
Output is correct |
17 |
Correct |
5 ms |
8220 KB |
Output is correct |
18 |
Correct |
82 ms |
11244 KB |
Output is correct |
19 |
Correct |
113 ms |
11712 KB |
Output is correct |
20 |
Correct |
65 ms |
10268 KB |
Output is correct |
21 |
Correct |
111 ms |
12076 KB |
Output is correct |
22 |
Correct |
110 ms |
12012 KB |
Output is correct |
23 |
Correct |
125 ms |
12132 KB |
Output is correct |
24 |
Correct |
125 ms |
12028 KB |
Output is correct |
25 |
Correct |
144 ms |
12120 KB |
Output is correct |
26 |
Correct |
118 ms |
12080 KB |
Output is correct |
27 |
Correct |
128 ms |
12156 KB |
Output is correct |
28 |
Correct |
130 ms |
13112 KB |
Output is correct |
29 |
Correct |
138 ms |
14656 KB |
Output is correct |
30 |
Correct |
36 ms |
9572 KB |
Output is correct |
31 |
Correct |
70 ms |
10864 KB |
Output is correct |
32 |
Correct |
198 ms |
13900 KB |
Output is correct |
33 |
Correct |
77 ms |
11044 KB |
Output is correct |
34 |
Correct |
88 ms |
11204 KB |
Output is correct |
35 |
Correct |
50 ms |
10236 KB |
Output is correct |
36 |
Correct |
35 ms |
9408 KB |
Output is correct |
37 |
Correct |
38 ms |
9704 KB |
Output is correct |
38 |
Correct |
122 ms |
11164 KB |
Output is correct |
39 |
Correct |
118 ms |
11164 KB |
Output is correct |
40 |
Correct |
124 ms |
11196 KB |
Output is correct |
41 |
Correct |
140 ms |
11296 KB |
Output is correct |
42 |
Correct |
113 ms |
11240 KB |
Output is correct |
43 |
Correct |
132 ms |
12520 KB |
Output is correct |
44 |
Correct |
154 ms |
12424 KB |
Output is correct |
45 |
Correct |
132 ms |
12424 KB |
Output is correct |
46 |
Correct |
132 ms |
12436 KB |
Output is correct |
47 |
Correct |
133 ms |
12492 KB |
Output is correct |
48 |
Correct |
153 ms |
10676 KB |
Output is correct |
49 |
Correct |
143 ms |
10692 KB |
Output is correct |
50 |
Correct |
133 ms |
10932 KB |
Output is correct |
51 |
Correct |
143 ms |
11060 KB |
Output is correct |
52 |
Correct |
151 ms |
10588 KB |
Output is correct |
53 |
Correct |
139 ms |
10496 KB |
Output is correct |
54 |
Correct |
138 ms |
10556 KB |
Output is correct |