#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define int long long
#define inf ((int)1e18)
using namespace std;
struct segtree {
int n;
vector <int> sum, lazy, tri;
vector <array<int, 3> > updates;
void init(int size) {
n = size;
sum.resize(n * 8);
lazy.resize(n * 8);
tri.resize(n * 8);
}
inline void push(int v, int l, int r) {
if(!lazy[v]) return;
int m = (l + r) / 2;
int lz = lazy[v];
lazy[v] = 0;
// for left
int sz = (m - l + 1);
sum[v * 2] += lz * sz;
tri[v * 2] += lz * sz * (sz + 1) / 2;
lazy[v * 2] += lz;
// for right
sz = (r - m);
sum[v * 2 + 1] += lz * sz;
tri[v * 2 + 1] += lz * sz * (sz + 1) / 2;
lazy[v * 2 + 1] += lz;
}
void update(int l, int r, int val) {
l += n / 2, r += n / 2;
updates.push_back({l, r, val});
// cout << "update: " << l << " " << r << " " << val << "\n";
return range_upd(1, 1, n, l, r, val);
}
void range_upd(int v, int l, int r, int ul, int ur, int val) {
// cout << v << " " << l << " " << r << " " << ul << " " << ur << " " << val << endl;
if(l >= ul and r <= ur) {
lazy[v] += val;
int sz = (r - l + 1);
sum[v] += val * sz;
tri[v] += val * sz * (sz + 1) / 2;
return;
}
push(v, l, r);
int m = (l + r) / 2;
if(ul <= m) {
range_upd(v * 2, l, m, ul, ur, val);
}
if(ur > m) {
range_upd(v * 2 + 1, m + 1, r, ul, ur, val);
}
sum[v] = sum[v * 2] + sum[v * 2 + 1];
tri[v] = tri[v * 2] + tri[v * 2 + 1] + (m - l + 1) * sum[v * 2 + 1];
}
int normquery(int l) {
l += n / 2;
// cout << "query: " << l+1 << " = ";
int ret = sumq(1, 1, n, l + 1, n);
// cout << ret << ".\n";
return ret;
}
int sumq(int v, int l, int r, int ql, int qr) {
if(l >= ql and r <= qr) {
return sum[v];
}
push(v, l, r);
int m = (l + r) / 2;
int ret = 0;
if(ql <= m) {
ret += sumq(v * 2, l, m, ql, qr);
}
if(qr > m) {
ret += sumq(v * 2 + 1, m + 1, r, ql, qr);
}
return ret;
}
int mega_brain_query(int l, int r) {
int ret = normquery(r);
l += n / 2, r += n / 2;
int sz = r - l + 1;
ret *= sz;
// cout << "HEY: " << ret / sz << " " << l << " " << r << "\n";
return ret + triquery(1, 1, n, l + 1, r).first;
}
pair<int, int> triquery(int v, int l, int r, int ql, int qr) {
if(l >= ql and r <= qr) {
// cout << l - n / 2 << " " << r - n / 2 << " " << tri[v] << " " << sum[v] << "\n";
return {tri[v], sum[v]};
}
push(v, l, r);
int m = (l + r) / 2;
pair<int, int> ret = {0, 0};
if(ql <= m) {
auto tmp = triquery(v * 2, l, m, ql, qr);
ret = tmp;
}
if(qr > m) {
int lsize = max(0ll, m - max(ql, l) + 1);
auto tmp = triquery(v * 2 + 1, m + 1, r, ql, qr);
ret.first += tmp.first + lsize * tmp.second;
ret.second += tmp.second;
// cout << m - n / 2 << " " << ql - n / 2 << " " << lsize << ". ";
}
// cout << l - n / 2 << " " << r - n / 2 << " " << ret.first << " " << ret.second << "\n";
return ret;
}
void reverse_updates() {
for(auto [l, r, val]:updates) {
range_upd(1, 1, n, l, r, -val);
}
updates.clear();
}
}t;
int32_t main(){
fast
int n;
cin >> n;
vector <int> arr(n);
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
auto compress = [&](vector <int> &a) {
vector <pair<int, int> > v(a.size());
for(int i = 0; i < n; i++) {
v[i] = {a[i], i};
}
sort(v.begin(), v.end());
for(int i = 0, ind = 0; i < n; i++) {
if(i and v[i].first != v[i - 1].first) ind++;
a[v[i].second] = ind;
}
};
compress(arr);
vector <vector<int> > val(n, vector<int>(1, -1));
for(int i = 0; i < n; i++) {
val[arr[i]].push_back(i);
}
t.init(n * 2);
int ans = 0, color = 0;
for(auto &it:val) {
// cout << color++ << ":\n";
if(it.size() == 1) continue;
vector <int> comp(1), pre;
for(int i = 1; i < it.size(); i++) {
int l = it[i - 1] + 1;
if(-(it[i] - l)) comp.push_back(-(it[i] - l));
comp.push_back(1);
}
if(it.back() != n - 1) comp.push_back(-(n - it.back() - 1));
int prev = 0;
for(int i = 0; i < comp.size(); i++) {
pre.push_back(prev += comp[i]);
}
int m = comp.size();
for(int i = m - 1; i > 0; i--) {
if(pre[i] < pre[i - 1]) {
int r = pre[i - 1] - 1, l = pre[i];
int huh = ans;
ans += t.mega_brain_query(l + 1, r + 1);
// cout << l << " " << r << " " << ans - huh << "\n";
// if(l == -7 and r == -2) return 0;
t.update(l, r, 1);
}
else {
t.update(pre[i], pre[i], 1);
ans += t.normquery(pre[i - 1]);
// cout << pre[i] << " " << t.normquery(pre[i - 1]) << "\n";
}
}
t.reverse_updates();
// cout << "\n\n";
}
cout << ans;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:150:20: 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]
150 | for(int i = 1; i < it.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:157:20: 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]
157 | for(int i = 0; i < comp.size(); i++) {
| ~~^~~~~~~~~~~~~
Main.cpp:165:9: warning: unused variable 'huh' [-Wunused-variable]
165 | int huh = ans;
| ^~~
Main.cpp:145:15: warning: unused variable 'color' [-Wunused-variable]
145 | int ans = 0, color = 0;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
3 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1272 KB |
Output is correct |
16 |
Correct |
2 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
67380 KB |
Output is correct |
2 |
Correct |
160 ms |
86704 KB |
Output is correct |
3 |
Correct |
97 ms |
51996 KB |
Output is correct |
4 |
Correct |
205 ms |
94080 KB |
Output is correct |
5 |
Correct |
202 ms |
96888 KB |
Output is correct |
6 |
Correct |
190 ms |
102560 KB |
Output is correct |
7 |
Correct |
192 ms |
102416 KB |
Output is correct |
8 |
Correct |
203 ms |
102436 KB |
Output is correct |
9 |
Correct |
194 ms |
102672 KB |
Output is correct |
10 |
Correct |
200 ms |
102676 KB |
Output is correct |
11 |
Correct |
176 ms |
103984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
3 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1272 KB |
Output is correct |
16 |
Correct |
2 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
124 ms |
67380 KB |
Output is correct |
19 |
Correct |
160 ms |
86704 KB |
Output is correct |
20 |
Correct |
97 ms |
51996 KB |
Output is correct |
21 |
Correct |
205 ms |
94080 KB |
Output is correct |
22 |
Correct |
202 ms |
96888 KB |
Output is correct |
23 |
Correct |
190 ms |
102560 KB |
Output is correct |
24 |
Correct |
192 ms |
102416 KB |
Output is correct |
25 |
Correct |
203 ms |
102436 KB |
Output is correct |
26 |
Correct |
194 ms |
102672 KB |
Output is correct |
27 |
Correct |
200 ms |
102676 KB |
Output is correct |
28 |
Correct |
176 ms |
103984 KB |
Output is correct |
29 |
Correct |
151 ms |
104172 KB |
Output is correct |
30 |
Correct |
131 ms |
17160 KB |
Output is correct |
31 |
Correct |
280 ms |
34536 KB |
Output is correct |
32 |
Correct |
676 ms |
85000 KB |
Output is correct |
33 |
Correct |
279 ms |
36688 KB |
Output is correct |
34 |
Correct |
310 ms |
38420 KB |
Output is correct |
35 |
Correct |
179 ms |
25036 KB |
Output is correct |
36 |
Correct |
104 ms |
14932 KB |
Output is correct |
37 |
Correct |
149 ms |
18148 KB |
Output is correct |
38 |
Correct |
249 ms |
96168 KB |
Output is correct |
39 |
Correct |
258 ms |
96068 KB |
Output is correct |
40 |
Correct |
240 ms |
96276 KB |
Output is correct |
41 |
Correct |
245 ms |
96540 KB |
Output is correct |
42 |
Correct |
247 ms |
96380 KB |
Output is correct |
43 |
Correct |
279 ms |
93160 KB |
Output is correct |
44 |
Correct |
275 ms |
93204 KB |
Output is correct |
45 |
Correct |
300 ms |
93408 KB |
Output is correct |
46 |
Correct |
280 ms |
93416 KB |
Output is correct |
47 |
Correct |
280 ms |
93260 KB |
Output is correct |
48 |
Correct |
371 ms |
90392 KB |
Output is correct |
49 |
Correct |
368 ms |
90384 KB |
Output is correct |
50 |
Correct |
339 ms |
91152 KB |
Output is correct |
51 |
Correct |
356 ms |
91156 KB |
Output is correct |
52 |
Correct |
409 ms |
90024 KB |
Output is correct |
53 |
Correct |
398 ms |
89748 KB |
Output is correct |
54 |
Correct |
379 ms |
89888 KB |
Output is correct |