比赛 |
2024暑假C班集训5 |
评测结果 |
AAAAAAAAAA |
题目名称 |
充电宝 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
0.309 s |
代码语言 |
C++ |
内存使用 |
11.32 MiB |
提交时间 |
2024-07-05 08:54:34 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
const int N = 2e5+10;
const ll inf = 1e17;
ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
int n;
int a[N],l[N];
vector<int>c[N];
struct BIT{
int c[N];
in int lowbit(int x){return x & (-x);}
void add(int x,int val){for(;x <= n;x += lowbit(x))c[x] += val;}
in int ask(int x){
if(x <= 0)return 0;
int ans = 0;
for(;x > 0;x -= lowbit(x))ans += c[x];
return ans;
}
in ll ask(int l,int r){
if(l > r)return 0;
return (ll)ask(r) - ask(l-1);
}
}t;
ll ans = 0;
int main(){
freopen("charger.in","r",stdin);
freopen("charger.out","w",stdout);
n = read();
for(int i = 1;i <= n;i++)a[i] = read(),c[a[i]].push_back(i);
for(int i = 1;i <= n;i++)
if(c[i].size())t.add(c[i][0],1);
for(int i = 1;i <= n;i++){
int x = a[i],r = 0;
if(l[x] + 1 == c[x].size())r = n;
else r = c[x][l[x]+1] - 1;
ans += (ll)t.ask(i+1,r);
t.add(c[x][l[x]],-1);
if(l[x] + 1 != c[x].size())t.add(c[x][++l[x]],1);
}
printf("%lld\n",ans);
return 0;
}