(4329)Ping pong
思路:树状数组。
考虑第i个人当裁判,那么只要计算出在他之前比他小的乘在他之后比他大的与在他之前比他大的乘在他之后比他小的,那么用两个树状数组维护一下就行了。复杂的(n*log(n))
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<math.h>
8 using namespace std;
9 typedef long long LL;
10 int bitl[200005];
11 int bitr[200005];
12 int lowbit(int x);
13 void addl(int x);
14 void addr(int x);
15 int askl(int x);
16 int askr(int x);
17 int ans[100005];
18 int bns[100005];
19 int aksl[200005];
20 int aksr[200005];
21 int main(void)
22 {
23 int T;
24 scanf("%d",&T);
25 while(T--)
26 {
27 int n;
28 scanf("%d",&n);
29 memset(bitl,0,sizeof(bitl));
30 memset(bitr,0,sizeof(bitr));
31 int i,j;
32 for(i = 1; i <= n; i++)
33 {
34 scanf("%d",&ans[i]);
35 }
36 int cn = 1;
37 for(i = n; i >= 1; i--)
38 {
39 bns[cn++] = ans[i];
40 }
41 for(i = 1; i <= n; i++)
42 {
43 aksl[i] = askl(ans[i]-1);
44 addl(ans[i]);
45 aksr[i] = askr(bns[i]-1);
46 addr(bns[i]);
47 }
48 LL sum = 0;
49 for(i = 2;i <= n-1;i++)
50 {
51 sum = sum + (LL)aksl[i]*(LL)(n-i-aksr[n-i+1])+(LL)(i - 1 - aksl[i])*(LL)aksr[n-i+1];
52 }
53 printf("%lld\n",sum);
54 }
55 return 0;
56 }
57 int lowbit(int x)
58 {
59 return x&(-x);
60 }
61 void addl(int x)
62 {
63 while(x <= 100000)
64 {
65 bitl[x]++;
66 x += lowbit(x);
67 }
68 }
69 void addr(int x)
70 {
71 while(x <= 100000)
72 {
73 bitr[x]++;
74 x += lowbit(x);
75 }
76 }
77 int askl(int x)
78 {
79 int sum = 0;
80 while(x > 0)
81 {
82 sum += bitl[x];
83 x -= lowbit(x);
84 }
85 return sum;
86 }
87 int askr(int x)
88 {
89 int sum = 0;
90 while(x > 0)
91 {
92 sum += bitr[x];
93 x -= lowbit(x);
94 }
95 return sum;
96 }
(4329)Ping pong的更多相关文章
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- ACM-ICPC LA 4329 Ping pong(树状数组)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- UVALive 4329 Ping pong(树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...
- BIT LA 4329 Ping pong
题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- LA 4329 Ping pong (树状数组)
题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
- UVALive 4329 Ping pong (BIT)
枚举中间的人,只要知道在这个人前面的技能值比他小的人数和后面技能值比他小的人数就能计算方案数了,技能值大的可有小的推出. 因此可以利用树状数组,从左到右往树上插点,每个点询问sum(a[i]-1)就是 ...
- LA 4329 Ping pong
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ; ; ...
随机推荐
- 【模板】有源汇有上下界最大流(网络流)/ZOJ3229
先导知识 无源汇有上下界可行流 题目链接 https://vjudge.net/problem/ZOJ-3229 https://www.luogu.com.cn/problem/P5192 (有改动 ...
- 日常Java 2021/10/11
抽象类 所有对象都是通过类描述的,但不是所有的类都是用来描述对象,就好比抽象类,此类中没有足够的信息描述一个对象. 抽象类不能实例化对象,所以抽象类必须的继承,才可以使用. 抽象方法 Abstract ...
- 学习java 7.22
学习内容: GridBagLayout GridBagLayout布局管理器的功能最强大,但也最复杂,与GridLayout布局管理器不同的是,在GridBagLayout布局管理器中,一个组件可以跨 ...
- OC-ARC,类扩展,block
总结 标号 主题 内容 一 autorelease autorelease基本概念/自动释放池/autorelease基本使用 二 autorelease注意事项 注意点/应用场景 三 ARC 什么是 ...
- 【Python】【Basic】【数据类型】运算符与深浅拷贝
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...
- Static data members in C++
Predict the output of following C++ program: 1 #include <iostream> 2 using namespace std; 3 4 ...
- 实现new Date(), 获取当前时间戳
JS 获取时间戳: 我相信大家找了很久了吧! 希望我写的这个对您有些帮助哦~ 大家是不是以为时间戳是关于时间的,都去 new Date() 里面找方法了啊,我来告诉你们正确的吧 其实大家用 JS 里的 ...
- 【C/C++】函数的默认参数/函数的占位参数/函数重载/注意事项
函数的默认参数 返回值类型 函数名(参数=默认值){} #include <iostream> using namespace std; int func(int a = 10, int ...
- 30个类手写Spring核心原理之Ioc顶层架构设计(2)
本文节选自<Spring 5核心原理> 1 Annotation(自定义配置)模块 Annotation的代码实现我们还是沿用Mini版本的,保持不变,复制过来便可. 1.1 @GPSer ...
- Mysql配置文件 扩展详细配置
目录 配置文件中有些特定参数 扩展配置 max_connections connect_timeout interactive_timeout|wait_timeout net_retry_count ...