做法:其实只要过了
13 5 4 2 这组数据基本没什么问题了。。。
哎,早知道用树状数组了...
题目中的裁判是要位置在两个运动员之间,而且能力也在他们之间的,好在每个人的rank都不一样。
可以先记录每个人的rank还有位置,然后按照rank进行排序。
建立三棵线段树,一棵的节点记录每个人,还有两棵是分别记录某个人左边比他rank低的人的数量,某个人右边rank低的人的数量。
然后就按照排序过后的顺序进行插入、查找
#include<cstdio>
#include<cstring>
#include<algorithm>
#define left l,m,x<<1
#define right m+1,r,x<<1|1
#define LL __int64
using namespace std;
const int LMT=200003;
struct ma
{
int ran,pos;
bool operator<(const ma &y)const
{
if(ran!=y.ran)return ran<y.ran;
return pos<y.pos;
}
}man[LMT];
struct no
{
int l,r,len;
}node[LMT<<2];
LL get[LMT<<2],get2[LMT<<2];
int sum[LMT<<2];
int input(void)
{
int res=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
{
res=res*10+c-'0';
c=getchar();
}
return res;
}
void build(int l,int r,int x)
{
get2[x]=get[x]=sum[x]=0;
node[x].l=l;node[x].r=r;
node[x].len=r-l+1;
if(l==r)return;
int m=(l+r)>>1;
build(left);
build(right);
}
void update(int is,int op,int L,int R,int x)
{
if(L<=node[x].l&&node[x].r<=R)
{
switch(is)
{
case 0:get[x]+=op;return;
case 1:sum[x]+=op;return;
case 2:get2[x]+=op;return;
}
}
int m=(node[x].l+node[x].r)>>1;
if(L<=m)update(is,op,L,R,x<<1);
if(R>m)update(is,op,L,R,x<<1|1);
switch(is)
{
case 0:get[x]=get[x<<1]+get[x<<1|1];break;
case 1:sum[x]=sum[x<<1]+sum[x<<1|1];break;
case 2:get2[x]=get2[x<<1]+get2[x<<1|1];break;
}
}
LL query(bool is,int L,int R,int x)
{
if(L<=node[x].l&&node[x].r<=R)
{
if(is)return get[x];
else return get2[x];
}
int m=(node[x].l+node[x].r)>>1;
LL res=0;
if(L<=m)res+=query(is,L,R,x<<1);
if(R>m)res+=query(is,L,R,x<<1|1);
return res;
}
int q_uery(int L,int R,int x)
{
if(L<=node[x].l&&node[x].r<=R)return sum[x];
int m=(node[x].l+node[x].r)>>1,res=0;
if(L<=m)res+=q_uery(L,R,x<<1);
if(R>m)res+=q_uery(L,R,x<<1|1);
return res;
}
int main(void)
{
int n,i,T;
scanf("%d",&T);
LL ans;
while(T--)
{
scanf("%d",&n);
build(1,n,1);
ans=0;
for(i=1;i<=n;i++)
{
man[i].pos=i;
man[i].ran=input();
}
sort(man+1,man+1+n);
for(i=1;i<=n;i++)
{
ans+=query(0,man[i].pos,n,1);
ans+=query(1,1,man[i].pos,1);
update(0,q_uery(1,man[i].pos,1),man[i].pos,man[i].pos,1);
update(2,q_uery(man[i].pos,n,1),man[i].pos,man[i].pos,1);
update(1,1,man[i].pos,man[i].pos,1);
}
printf("%I64d\n",ans);
}
return 0;
}