https://www.lydsy.com/JudgeOnline/problem.php?id=1106
树状数组
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <cmath>
#include <vector>
#define INF 99999999999999
using namespace std;
typedef long long LL;
int N,W,ans,trie[100005],idx[50005];
void updata(int x,int val)
{
while(x<=W)
{
trie[x]+=val;
x+=(x&-x);
}
}
int sum(int x)
{
int ans=0;
while(x>0)
{
ans+=trie[x];
x-=(x&-x);
}
return ans;
}
int main()
{
scanf("%d",&N);
W=N<<1;
int x;
for(int i=1;i<=W;i++)
{
scanf("%d",&x);
if(!idx[x])
{
idx[x]=i;
updata(i,1);
}
else
{
ans+=sum(i)-sum(idx[x]);
updata(idx[x],-1);
}
}
printf("%d\n",ans);
return 0;
}