uva 1428 - Ping pong(树状数组)
来源:互联网 发布:unity3d vs2017 编辑:程序博客网 时间:2024/05/21 17:29
integers a1, a2...aN follow, indicating the skill rank of each player, in the order of west to east (1ai100000 , i = 1...N ).
Output
For each test case, output a single line contains an integer, the total number of different games.
Sample Input
13 1 2 3
Sample Output
1
以前没用过树状数组,都是用线段树的,不过既然碰到了这个典型例题,就还是自己手敲一遍吧,树状数组代码确实简单许多。
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int INF = 1000000000;const int maxn = 100000 + 5;typedef long long LL;int a[maxn],c[maxn];LL S_left[maxn],B_left[maxn];int n;int lowbit(int x){return x&-x;}LL sum(int x){ LL ret = 0; while(x > 0){ ret += c[x]; x -= lowbit(x); } return ret;}void add(int x){ while(x <= maxn){ c[x] += 1; x += lowbit(x); }}int main(){ int t,Max; scanf("%d",&t); while(t--){ Max = -1; memset(c,0,sizeof(c)); scanf("%d",&n); for(int i = 1;i <= n;i++){ scanf("%d",&a[i]); add(a[i]); Max = max(Max,a[i]); S_left[i] = sum(a[i]-1); B_left[i] = max((LL)0,sum(Max)-sum(a[i])); } LL ans = 0; for(int i = 1;i <= n;i++){ if(i == 1 || i == n) continue; ans += S_left[i] * ((sum(Max)-sum(a[i]))-B_left[i]); ans += B_left[i] * (sum(a[i]-1)-S_left[i]); } printf("%lld\n",ans); } return 0;}
- uva 1428 - Ping pong(树状数组)
- UVa:1428 Ping pong (树状数组)
- UVA 1428 - Ping pong(树状数组)
- 【uva】1428 - Ping pong(树状数组)
- uva 1428 - Ping pong(树状数组)
- uva 1428 Ping pong (树状数组)
- UVa 1428 Ping pong (树状数组)
- UVA 1428 Ping Pong(树状数组)
- uva 1428 - Ping pong(树状数组,4级)
- uva 1428 - Ping pong (树状数组的应用)
- 树状数组(二叉索引树)(uva 1428 - Ping pong )
- Uva 1428 Ping pong (树状数组,Fenwick树)
- Ping pong(树状数组)
- hdu2492 Ping pong (树状数组)
- UVA1428 Ping pong(树状数组)
- POJ3928 Ping pong 树状数组
- UVA1428 - Ping pong(树状数组)
- 树状数组 POJ2492 Ping Pong
- PHP如何删除文件或文件夹
- 如何激励你的员工?
- 结网@改变世界的互联网产品经理(修订版)》
- PHP10个常用处理字符串的方法
- 如何查看系统的开机时间
- uva 1428 - Ping pong(树状数组)
- vc_文件读写权限
- hdu 1851 A Simple Game
- DotProject 安装
- EFI Shell 命令参考
- 通过xslt将xml转换为html代码
- windows 如何查看端口占用情况?
- Canvas and Drawables
- SurfaceFlinger调用framebuffer模块、gralloc模块流程(3)