BZOJ 1106 立方体大作战

时间:2022-03-29 00:54:27

BIT.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200050
using namespace std;
int n,t[maxn],x,pos[maxn],ans=;
int lowbit(int x)
{
return (x&(-x));
}
int query(int x)
{
int ret=;
for (int i=x;i>=;i-=lowbit(i))
ret+=t[i];
return ret;
}
void add(int x,int val)
{
for (int i=x;i<=*n+;i+=lowbit(i))
t[i]+=val;
}
int main()
{
scanf("%d",&n);
for (int i=;i<=*n;i++)
{
scanf("%d",&x);
if (!pos[x]) pos[x]=i;
else
{
ans+=(i+query(i))-(pos[x]+query(pos[x]))-;
add(pos[x]+,-);add(i,);
add(i+,-);add(*n+,);
}
}
printf("%d\n",ans);
return ;
}