UVALive 4329 Ping pong
来源:互联网 发布:java分布式编程 编辑:程序博客网 时间:2024/06/03 12:28
#include <stdio.h>#include <string.h>using namespace std;struct fwtree{ int C[101000], n; void init(int k){ memset(C, 0, sizeof(C)); n = k; } int lowbit(int x) { return x&-x; } void add(int x, int d) { while(x<=n){ C[x] += d; x += lowbit(x); } } long long sum(int x) { long long ans=0; while(x>=1){ ans += C[x]; x -= lowbit(x); } return ans; }};fwtree f;int main(){ int t, n; int maxa; long long a[20200][4]; int p[20200]; long long ans; scanf("%d", &t); while(t--){ scanf("%d", &n); maxa = -1; for(int i=1; i<=n; i++){ scanf("%d", &p[i]); if(p[i]>maxa) maxa = p[i]; } f.init(maxa); ans = 0; for(int i=1; i<=n; i++){ f.add(p[i], 1); a[i][0] = f.sum(p[i]-1); //a[i][0]在左边比a[i]小 a[i][1]为在左边比a[i]大 a[i][1] = i-a[i][0]-1; //家在i前面的有i-1个 } f.init(maxa); for(int i=n; i>=1; i--){ f.add(p[i], 1); a[i][2] = f.sum(p[i]-1); //a[i][2]在右边比a[i]小 a[i][3]为在右边比a[i]大 a[i][3] = n-i-a[i][2]; } for(int i=0; i<n; i++) ans += a[i][0]*a[i][3] + a[i][1]*a[i][2]; printf("%lld\n", ans); } return 0;}
0 0
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive - 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive-4329-Ping-pong
- UVALive 4329 Ping pong(线段树)
- UVALive - 4329 Ping pong (树状数组)
- uvalive 4329 Ping pong---树状数组
- 4329 - Ping pong uvalive+树状数组
- uvalive 4329 Ping pong 树状数组
- UVALIVE 4329 Ping pong(树状数组)
- uvalive 4329 Ping pong (树状数组)
- UVALive - 4329 Ping pong 二叉索引树
- UVALive 4329--Ping pong+树状数组
- UVALive - 4329 Ping pong 数状数组
- UVALive - 4329 Ping pong (树状数组)
- UVALive 4329 Ping pong(树状数组)
- 云计算服务之Amazon Web services
- C++不容易明白的关键字(一)
- Qt 线程基础(QThread、QtConcurrent、QThreadPool等)
- SharedPreference、文件存储 (FIleInputStream/FileOutputStream).SQLite
- 动态规划练习题:概率DP总结,求解数学期望或概率的题目
- UVALive 4329 Ping pong
- 求两个数的最大公约数和最小公倍数
- Java, I am coming....
- sgu 140
- DS1302时钟与LCD1602液晶显示
- C++ 继承和静态成员
- Java学习笔记——折半查找与逐个查找方式
- HDU 2026
- 让你的eclipse实现写JAVA代码,HTML,CSS,JAVASCRIPT代码提示