Title Link: BZOJ-1106
Problem analysis
Enumerates each location from 1 to 2n.
If the number is enumerated to a number that is already the second occurrence, then it depends on how many times it has not been matched to the position of the first occurrence, and how many exchanges will be made without matching.
Code
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath > #include <algorithm>using namespace std;const int maxn = 100000 + 5;int N, ans;int A[MAXN], T[MAXN], l[maxn];voi d Add (int x, int Num) {for (int i = x; i <= n; i + = i & i) t[i] + = Num;} int Get (int x) {int ret = 0;for (int i = x; i; i-= i & i) ret + = T[i];return ret;} int main () {scanf ("%d", &n), n <<= 1;for (int i = 1; I <= n; ++i) scanf ("%d", &a[i]); Ans = 0;for (int i = 1; I <= n; ++i) {if (l[a[i] = = 0) {L[a[i]] = i; ADD (i, 1);} else {Ans + = get (i)-get (L[a[i]); ADD (L[a[i]],-1);}} printf ("%d\n", Ans); return 0;}
[Bzoj 1106] [POI2007] cube big battle tet "tree Array"