• Source
    1. /**************************************************************
    2.   Problem: 1106
    3.   User: zrts
    4.   Language: C++
    5.   Result: Accepted
    6.   Time:256 ms
    7.   Memory:1588 kb
    8. ****************************************************************/
    9.  
    10. #include<cstdio>
    11. #include<cstring>
    12. #include<algorithm>
    13. #define lowbit(x) (x&-x)
    14. //by zrt
    15. //problem:
    16. using namespace std;
    17. typedef long long ll;
    18. const double eps(1e-10);
    19. int n;
    20. int c[100005];
    21. void add(int pos,int k){
    22. for(;pos<=n;pos+=lowbit(pos)){
    23. c[pos]+=k;
    24. }
    25. }
    26. int ask(int pos){
    27. int ret=0;
    28. for(;pos;pos-=lowbit(pos)){
    29. ret+=c[pos];
    30. }
    31. return ret;
    32. }
    33. int last[100005];
    34. int main(){
    35. #ifdef LOCAL
    36. freopen("in.txt","r",stdin);
    37. freopen("out.txt","w",stdout);
    38. #endif
    39. scanf("%d",&n);
    40. n<<=1;
    41. int ans=0;
    42. for(int i=1,k;i<=n;i++){
    43. scanf("%d",&k);
    44. if(!last[k]) add(i,1),last[k]=i;
    45. else ans+=ask(i)-ask(last[k]),add(last[k],-1);
    46. }
    47. printf("%d\n",ans);
    48. return 0;
    49. }