【暑假】[实用数据结构]UVAlive 4329 Ping pong

UVAlive 4329 Ping pong

题目:

Ping pong
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

【暑假】[实用数据结构]UVAlive 4329 Ping pong

N(3【暑假】[实用数据结构]UVAlive 4329 Ping pongN【暑假】[实用数据结构]UVAlive 4329 Ping pong20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee's house. For some reason, the contestants can't choose a referee whose skill rank is higher or lower than both of theirs. The contestants have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?

Input

The first line of the input contains an integer T(1【暑假】[实用数据结构]UVAlive 4329 Ping pongT【暑假】[实用数据结构]UVAlive 4329 Ping pong20) , indicating the number of test cases, followed by T lines each of which describes a test case.

Every test case consists of N + 1 integers. The first integer is N , the number of players. Then N distinct integers a1, a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1【暑假】[实用数据结构]UVAlive 4329 Ping pongai【暑假】[实用数据结构]UVAlive 4329 Ping pong100000 , i = 1...N ).

Output

For each test case, output a single line contains an integer, the total number of different games.

Sample Input

1
3 1 2 3

Sample Output

1

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:
求一共能组织多少种比赛。如果裁判位于i,那么这种情况可以组织c[i]*(n-i-d[i]) + d[i]*(i-1-c[i])种比赛,其中c[i]为i左边比a[i]小的数目而d[i]为i右边比a[i]小的数目。问题转化为求c[i]与d[i],引入数组x,x[j]表示到目前为止是否有a[i]=j有则为1,那么c[i]就是x[i]的前缀和。d同理。ans需要枚举裁判的位置才能得到,因此需要不断修改x(add目前考虑位置的x),是动态前缀和的问题。代码由FenwickTree实现。 代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ,INF=<<; inline int lowbit(int u) { return u&-u; } struct FenwickTree { //动态范围求和问题
int n;
vector<int> C; //当数组用 void resize(int n){ this->n=n;C.resize(n); } //重新调节大小
void clear(){ fill(C.begin(),C.end(),); } //清零C int sum(int u){
int ret=;
while(u>){
ret += C[u]; u -= lowbit(u);
}
return ret;
}
void add(int u,int d){
while(u <= n){
C[u] += d; u += lowbit(u);
}
}
}; FenwickTree f;
int c[maxn],d[maxn],a[maxn];
int n; int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d",&n);
int maxa=-INF;
FOR(i,,n+) {
scanf("%d",&a[i]);maxa=max(maxa,a[i]);
}
f.resize(maxa); //将f.n载为maxa
f.clear(); //clear1
FOR(i,,n+) {
f.add(a[i],); //x[a[i]]=1
c[i]=f.sum(a[i]-);
//sum(x,1,a[i]-1)
}
f.clear(); //clear2
for(int i=n;i;i--){ //注意是--
f.add(a[i],);
d[i]=f.sum(a[i]-);
} long long ans=;
FOR(i,,n+)
ans += (long long)c[i]*(n-i-d[i]) + (long long)(i-c[i]-)*d[i];
printf("%lld\n",ans);
}
return ;
}
上一篇:个人作业—Week2:微软必应词典案例分析


下一篇:CodeForces 721D Maxim and Array