Codeforces Beta Round #57 (Div. 2)E---Enemy is weak(树状数组+离散化)
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: “A lion is never afraid of a hundred sheep”.
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur’s opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Sample test(s)
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1
两次树状数组维护,第一次维护求出每一个位置上的逆序数的对数
第二次维护到目前为止,插入到树状数组上的逆序对的总数目
数据较大,离散化一下
/*************************************************************************> File Name: CF-57-E.cpp> Author: ALex> Mail: zchao1995@gmail.com > Created Time: 2015年03月23日 星期一 21时00分16秒************************************************************************/#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;const int N = 1010000;
LL tree[N];
LL inver[N];
int val[N];
LL pre[N];
int xis[N];
int cnt;int binsearch(int x)
{int l = 1;int r = cnt;int mid;while (l <= r){mid = (l + r) >> 1;if (xis[mid] > x){r = mid - 1;}else if (xis[mid] < x){l = mid + 1;}else{break;}}return mid;
}int lowbit(int x)
{return x & (-x);
}void add(int x, int cnt)
{for (int i = x; i <= N; i += lowbit(i)){tree[i] += cnt;}
}LL sum(int x)
{LL ans = 0;for (int i = x; i; i -= lowbit(i)){ans += tree[i];}return ans;
}int main()
{int n;while (~scanf("%d", &n)){cnt = 0;LL ans = 0;memset(pre, 0, sizeof(pre));memset(inver, 0, sizeof(inver));memset(tree, 0, sizeof(tree));for (int i = 1; i <= n; ++i){scanf("%d", &val[i]);xis[++cnt] = val[i];}sort(xis + 1, xis + 1 + cnt);cnt = unique(xis + 1, xis + 1 + cnt) - xis - 1;for (int i = 1; i <= n; ++i){int x = binsearch(val[i]);add(x, 1);inver[i] = i - sum(x);pre[i] = pre[i - 1] + inver[i];}memset(tree, 0, sizeof(tree));for (int i = 1; i <= n; ++i){int x = binsearch(val[i]);add(x, inver[i]);ans += pre[i] - sum(x);}printf("%lld\n", ans);}return 0;
}
Codeforces Beta Round #57 (Div. 2)E---Enemy is weak(树状数组+离散化)相关推荐
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新
http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...
- Codeforces Round #782 (Div. 2)-D. Reverse Sort Sum(树状数组)
题目链接:Problem - D - Codeforces 题意:给定一个 01 序列 ,对 区间分别进行递增排序,得到 个序列,现给定 序列, ,即所有序列第 位的和.求给定 序列所对应的原 ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)
题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...
- Codeforces Beta Round #4 (Div. 2 Only)
Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...
- Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp
D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...
- Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题
B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...
- Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心
D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...
- Codeforces Beta Round #9 (Div. 2 Only) C. Hexadecimal's Numbers dfs
C. Hexadecimal's Numbers 题目连接: http://www.codeforces.com/contest/9/problem/C Description One beautif ...
最新文章
- [给12306支招]取消车票预订-采用全额预售(充值)
- error: device not found - waiting for device -
- sql server clr 集成系列之二 简单的sql 函数
- 【转载】C#中StreamReader读取中文文本出现乱码的解决方法
- 怎样用springboot开发cs_springboot开发之配置自定义的错误界面和错误信息
- GDB与远程(交叉)GDB调试
- 易语言源代码毁来者来了!!
- slf4j 与log4j 日志管理
- HTML 事件响应函数,HTML5: 事件处理函数的this指向问题
- 【flutter环境问题】Downloading Dart SDK Flutter engine时卡着不动
- php sqlsrv 分页,sqlsrv php分页
- Atcoder Grand Contest 036 D - Negative Cycle
- CSS综合征病例,ChurgStrauss 综合征 (css) 变应性嗜酸性肉芽肿课件
- Tomcat默认端口,关闭不必要的安全隐患
- itunes不能读取iPhone的内容,请前往iPhone“偏好设置”的“摘要”选项卡,然后单击“恢复”
- STM32 之十二 FLASH 使用详解 及 LL 库 FLASH 驱动实现
- 使用ARM芯片的小型NAS设备的一次存储故障的检修
- 校验集装箱号;java校验集装箱号并附解析
- 关于uni-app中uni-forms表单验证时“多级结构对象数据”如何做数据校验的解决方案
- root下备份mysql_如何用指令行备份mysql下所有数据库
热门文章
- 点击a标签弹出iframe_iframe标签与a标签
- Completed 404 NOT_FOUND,Whitelabel Error Page
- html文件如何创建文件,怎么创建html文件
- JUC系列ReentrantLock
- win10键盘全部没反应_win10笔记本键盘全部没反应 win10键盘全部没反应解决方法...
- Vxe Table一些简单的应用和踩坑记录
- wifi android透传源代码,【终极版】ESP8266远程控制wifi透传模块带调试app
- 对交换机与路由器的认知
- CPU, GPU, FPGA计算能力
- DataGear 轻松制作支持图表联动的全国地图、省级地图数据可视化看板