POJ3928 Ping pong
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Input
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 <= ai <= 100000, i = 1 ... N).
Output
Sample Input
1
3 1 2 3
Sample Output
1
Source
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
LL t[mxn+];
struct node{
int x;
int id;
}a[mxn];
int cmp(const node a,const node b){
return a.x<b.x;
}
int lowbit(int x){return x&-x;}
void add(int p,int v){//k==0 个数 k==1 距离
while(p<=mxn){t[p]+=v;p+=lowbit(p);}
}
LL smm(int x){
LL res=;
while(x){res+=t[x];x-=lowbit(x);}
return res;
}
int T;
int main(){
scanf("%d",&T);
int i,j;
while(T--){
memset(t,,sizeof t);
scanf("%d",&n);
for(i=;i<=n;i++){
a[i].x=read();
a[i].id=i;
}
LL ans=;
sort(a+,a+n+,cmp);
add(a[].id,);
for(i=;i<n;i++){
int ldn=smm(a[i].id);//所有id小于当前id,且x小于当前x的
int lup=a[i].id--ldn;//所有id小于当前id,但x大于当前x的
int rdn=(smm(mxn)-ldn);//所有id大于当前id,且x小于当前x的
int rup=n-rdn-a[i].id;//所有id大于当前id,且x大于当前x的
ans+=ldn*rup+lup*rdn;
add(a[i].id,);
}
printf("%lld\n",ans);
}
return ;
}
POJ3928 Ping pong的更多相关文章
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- POJ 3928 Ping pong(树状数组)
Ping pong Time Limit: 1000MS ...
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- Ping pong
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3928 Ping pong
题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
- Frequent values && Ping pong
Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
随机推荐
- 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名
public class MonitorTools { /// <summary> /// 获取具体进程的内存,线程等参数情况 /// </summary> /// <p ...
- 01_3_创建一个Servlet
01_3_创建一个Servlet 1.创建一个Servlet import java.io.IOException; import java.io.PrintWriter; import javax. ...
- linux 下使用 curl 访问带多参数,GET掉参数解决方案
url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用 $_GET是可以获取到所有的参数 curl -s ...
- C++ 学习笔记(三)string 类
在C语言中如果想要使用字符串那么有两种方法: 1.定义char型数组:char[10]; 然后将每个字符填充到对应的位置. 优点:这种方式将字符串放在内存所以每个位置都可以修改. 缺点:赋值比较麻烦, ...
- Goroutine 中执行匿名函数 坑
//相对应for 循环 goroutine跑到慢 所以这里很大概率只会打印最后一条数据 func goRun() { values := []int{1, 2, 3} for _, v := rang ...
- excel日期格式取年份
具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束
- /dev/sda is apparently in use by the system; will not make a filesystem here!解决方法
/dev/sda is apparently in use by the system; will not make a filesystem here! 翻译:系统显然在使用,不会在这里做文件系统 ...
- UVa 1407 树形背包 Caves
这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...
- 学习正则有感by魔芋(命名问题)
魔芋: 事实上,我是反感一些特殊的名词.一些名词看上去就让人感觉到抗拒. 关于一个概念用不同的名词来定义,简直是太糟糕了. 举个例子: 匹配一个后面带有exp2的exp1的正则. 写法: exp1(? ...
- Selenium WebDriver-操作复选框
#encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...