#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 * 4);
lazy.resize(n * 4);
tri.resize(n * 4);
}
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});
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;
return sumq(1, 1, n, l + 1, n);
}
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) {
l += n / 2, r += n / 2;
int sz = r - l + 1;
int ret = sz * normquery(r);
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) {
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 - ql + 1);
auto tmp = triquery(v * 2 + 1, m + 1, r, ql, qr);
ret.first += tmp.first + lsize * tmp.second;
ret.second += tmp.second;
}
return ret;
}
// void print() {
// cout << "sum:\n";
// for(auto it:sum) {
// cout << it << " ";
// }
// cout << "\n";
// cout << "lazy:\n";
// for(auto it:lazy) {
// cout << it << " ";
// }
// cout << "\n";
// // cout << "tri:\n";
// // for(auto it:tri) {
// // cout << it << " ";
// // }
// cout << "\n";
// }
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 l = pre[i - 1] - 1, r = pre[i];
ans += t.mega_brain_query(l - 1, r - 1);
// cout << l << " " << r << " " << t.mega_brain_query(l - 1, r - 1) << "\n";
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 << ans;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:158: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]
158 | for(int i = 1; i < it.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:165: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]
165 | for(int i = 0; i < comp.size(); i++) {
| ~~^~~~~~~~~~~~~
Main.cpp:153:15: warning: unused variable 'color' [-Wunused-variable]
153 | int ans = 0, color = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
108 ms |
42908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |