HDU5792 World is Exploding(树状数组)
一共6种情况,a < b且Aa < Ab, c < d 且Ac > Ad,这两种情况数量相乘,再减去a = c, a = d, b = c, b = d这四种情况,使用树状数组维护,le[i]表示i左边比他小的数数量,le1[i]表示i左边比他大的数数量,ri[i]表示i右边比他小的数数量,ri1[i]表示i右边比他大的数数量。
跑两次树状数组,求出这四个数组值。
注意处理数值相等的情况。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 60008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
int C[N];
int n;
int le[N], ri[N], le1[N], ri1[N];
int val[N];
int b[N], tp[N];
inline int lowbit(int x){
return x&-x;
}
inline void add(int x, int val){
for(int i=x;i<=n;i+=lowbit(i)){
C[i] += val;
}
}
inline int sum(int x){
int ret = 0;
for(int i=x;i>0;i-=lowbit(i)){
ret+=C[i];
}
return ret;
} int main(){
while(~scanf("%d", &n)){
for(int i = 1; i <= n; i++){
scanf("%d", &val[i]);
tp[i] = val[i];
} sort(val + 1, val + n + 1);
for(int i = 1; i <= n; i++){
b[i] = lower_bound(val + 1, val + n + 1, tp[i]) - val;
}
MS(C , 0);
for(int i = 1; i <= n; i++){
le[i] = sum(b[i] - 1);
le1[i] = sum(n) - sum(b[i]);
add(b[i], 1);
} MS(C, 0);
for(int i = n; i >= 1; i--){
ri[i] = sum(b[i] - 1);
ri1[i] = sum(n) - sum(b[i]);
add(b[i], 1);
}
LL ans= 0;
LL sum = 0;
for(int i = n; i >= 1; i--){
sum += ri1[i];
}
ans = sum;
sum = 0;
for(int i = n; i >= 1; i--){
sum += ri[i];
}
ans *= sum;
sum = 0;
for(int i = 1; i <= n; i++){
sum += (LL)ri[i] * (LL)ri1[i];
sum += (LL)ri1[i] * (LL)le1[i];
sum += (LL)le[i] * (LL)le1[i];
sum += (LL)le[i] * (LL)ri[i];
}
ans -= sum;
printf("%I64d\n", ans);
}
return 0;
}
HDU5792 World is Exploding(树状数组)的更多相关文章
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu_5792_World is Exploding(树状数组+逆序对)
题目链接:hdu_5792_World is Exploding 题意: 给你一个数列,让你找有多少个(a,b,c,d)满足a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- hdu-5792 World is Exploding(容斥+树状数组)
题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU-5792 World is Exploding(树状数组)
题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...
- hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...
随机推荐
- git cherry-pick简介
本文编辑整理自: http://sg552.iteye.com/blog/1300713 http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.tx ...
- POJ 1917
http://poj.org/problem?id=1917 poj的字符串的一道水题. 题意么无关紧要, 反正输出的第一行就是把那个<>去掉,s1<s2>s3<s4&g ...
- 数位DP题目汇总
Google Code Jam 2014 Round 1B Problem B hdu 2089 hdu 3555 uestc 250 (原1307) hdu 3652 hdu 3709 Light ...
- ios8 新增的 showViewController 和 showDetailViewController
1.showViewController 先看看说明: You use this method to decouple the need to display a view controller fr ...
- android上的图片占用内存问题
近日正在把ios程序移植到android上,以前没做过android的程序,于是,想当然地把ios的图片资源放到了android工程的drawable文件夹下,这些图片都是png. 程序界面也很正常. ...
- Qt 使用sqlserver
1. pro 添加 QT +=sql 2. void MainWindow::connectSqlServer() { QSettings *setIni = new QSettings( ...
- UrlEncoder url编码
public static string PercentEncode(string s) { var bytes = Encoding.UTF8.GetBytes( ...
- 【linux】sudo su切换到root权限
在用户有sudo权限但不知道root密码时可用 sudo su切换到root用户
- HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- linux 用户、用户组不能是全数字
今天封装命令行,需要创建用户.用户组,遇到下面问题,如图: 当时我和迷茫,为什么明明存在‘1111’这个用户组,但是却提示不存在呢??难道是linux的一个bug??? 接着我又试了几个: 发现规律了 ...