#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int maxn = 4e5+5;
const int zr = 2e5+3;
const ll inf = 1e18+5;
ll s1[4*maxn], s2[4*maxn], sumid[4*maxn];
int lz[4*maxn];
pair<int, int> vec[maxn];
vector<int> pos[maxn];
int n;
vector<int> nd;
void build(int l = 1, int r = maxn-1, int id = 1){
if(l == r)sumid[id] = maxn - l;
else{
int mid = (l+r)>>1;
build(l, mid, id<<1);
build(mid+1, r, id<<1|1);
sumid[id] = sumid[id<<1] + sumid[id<<1|1];
}
}
void push(int l, int r, int id){
nd.push_back(id);
if(!lz[id])return;
s1[id]+=1LL*lz[id]*(r-l+1);
s2[id]+=1LL*lz[id]*sumid[id];
if(l < r){
lz[id<<1]+=lz[id];
nd.push_back(id<<1);
nd.push_back(id<<1|1);
lz[id<<1|1]+=lz[id];
}
lz[id] = 0;
}
void add(int l, int r, int ql, int qr, int id){
push(l, r, id);
if(qr < l || r < ql)return;
if(ql <= l && r <= qr){
lz[id]++;
push(l, r, id);
}
else{
int mid = (l+r)>>1;
add(l, mid, ql, qr, id<<1);
add(mid+1, r, ql, qr, id<<1|1);
s1[id]= s1[id<<1] + s1[id<<1|1];
s2[id] = s2[id<<1] + s2[id<<1|1];
}
}
ll q2(int l, int r, int ql, int qr, int id){
push(l, r, id);
if(qr < l || r < ql)return 0;
if(ql <= l && r <= qr)return s2[id] - s1[id]*(maxn-qr-1);
else{
int mid = (l+r)>>1;
return q2(l, mid, ql, qr, id<<1) + q2(mid+1, r, ql, qr, id<<1|1);
}
}
ll q1(int l, int r, int ql, int qr, int id){
push(l, r, id);
if(qr < l || r < ql)return 0;
if(ql <= l && r <= qr)return s1[id];
else{
int mid = (l+r)>>1;
return q1(l, mid, ql, qr, id<<1) + q1(mid+1, r, ql, qr, id<<1|1);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> vec[i].f;
vec[i].s = i;
}
int crr = 1;
sort(vec+1, vec+n+1);
pos[1].push_back(vec[1].s);
for(int i = 2; i <= n; i++){
if(vec[i].f != vec[i-1].f)crr++;
pos[crr].push_back(vec[i].s);
}
ll ans = 0;
build();
for(int i = 1; i <= n; i++){
if(pos[i].empty())break;
int crr = zr;
add(1, maxn-1, crr, crr, 1);
if(pos[i][0] > 1){
add(1, maxn-1, crr-pos[i][0]+1, crr-1, 1);
crr-=pos[i][0]-1;
}
crr++;
add(1, maxn-1, crr, crr, 1);
ans += q1(1, maxn-1, 1, crr-1, 1);
for(int j = 1; j < pos[i].size(); j++){
int d= pos[i][j] - pos[i][j-1]-1;
if(d){
ans += q2(1, maxn-1, crr-d-1, crr-2, 1);
ans += q1(1, maxn-1, 1, crr-d-2, 1);
add(1, maxn-1, crr-d, crr-1, 1);
crr-=d;
}
//cout << crr << "\n";
ans += q1(1, maxn-1, 1, crr, 1);
crr++;
add(1, maxn-1, crr, crr, 1);
}
if(pos[i].back() < n){
int d = n - pos[i].back();
ans += q2(1, maxn-1, crr-d-1, crr-2, 1);
ans += q1(1, maxn-1, 1, crr-d-2, 1);
}
for(auto v: nd){
s1[v] = 0;
s2[v] = 0;
lz[v] = 0;
}
nd.clear();
}
cout << ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:111:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int j = 1; j < pos[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
18004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
18044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
18132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
18080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
18004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |