題目鏈接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=117
題意:漢語題。。。
哈希表不說了,樹狀數組插點問線。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<ctime>
#include<algorithm>
using namespace std;
#define clr(arr,v) memset(arr,v,sizeof(arr))
const int M = 10000007;
template<class T,int size>
class Hash{
public:
Hash(){}
void clear(){
pos = index = 0;
clr(head,-1);
}
T& operator[](T value){
return val[add(value)];
}
T add(T value){
int v = value / M;
for(int i = head[value%M];i != -1;i = next[i])
{
if(num[i] == v)
return i;
}
num[pos] = v;
val[pos] = ++index;
next[pos] = head[value%M];
head[value%M] = pos++;
return pos-1;
}
private:
int index,pos,val[size],next[size],head[M];
T num[size];
};
Hash<int,1000005> h;
int tree[1000005],num[1000005],num1[1000005];
int lowbit(int x) {return x&(-x);}
void update(int x)
{
if(x <= 0) return ;
tree[x] += 1;
update(x - lowbit(x));
}
int get_num(int x,int n)
{
if(x >= n) return tree[n];
return tree[x]+get_num(x+lowbit(x),n);
}
int main()
{
//freopen("1.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
h.clear();
memset(tree,0,sizeof(tree));
int n,val,t;
long long sum = 0;
scanf("%d",&n);
for(int i = 0;i < n;++i)
{
scanf("%d",&num[i]);
num1[i] = num[i];
}
sort(num1,num1+n);
for(int i = 0;i < n;++i)
h[ num1[i] ];
for(int i = 0;i < n;++i)
{
t = h[ num[i] ];
sum += get_num(t,n);
update(t-1);
}
printf("%lld\n",sum);
}
//printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}