一共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右边比他大的数数量。

  跑两次树状数组,求出这四个数组值。

  注意处理数值相等的情况。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. #include<cmath>
  11. #include<utility>
  12. using namespace std;
  13. typedef long long LL;
  14. const int N = 60008, INF = 0x3F3F3F3F;
  15. #define MS(a, num) memset(a, num, sizeof(a))
  16. #define PB(A) push_back(A)
  17. #define FOR(i, n) for(int i = 0; i < n; i++)
  18. int C[N];
  19. int n;
  20. int le[N], ri[N], le1[N], ri1[N];
  21. int val[N];
  22. int b[N], tp[N];
  23. inline int lowbit(int x){
  24. return x&-x;
  25. }
  26. inline void add(int x, int val){
  27. for(int i=x;i<=n;i+=lowbit(i)){
  28. C[i] += val;
  29. }
  30. }
  31. inline int sum(int x){
  32. int ret = 0;
  33. for(int i=x;i>0;i-=lowbit(i)){
  34. ret+=C[i];
  35. }
  36. return ret;
  37. }
  38.  
  39. int main(){
  40. while(~scanf("%d", &n)){
  41. for(int i = 1; i <= n; i++){
  42. scanf("%d", &val[i]);
  43. tp[i] = val[i];
  44. }
  45.  
  46. sort(val + 1, val + n + 1);
  47. for(int i = 1; i <= n; i++){
  48. b[i] = lower_bound(val + 1, val + n + 1, tp[i]) - val;
  49. }
  50. MS(C , 0);
  51. for(int i = 1; i <= n; i++){
  52. le[i] = sum(b[i] - 1);
  53. le1[i] = sum(n) - sum(b[i]);
  54. add(b[i], 1);
  55. }
  56.  
  57. MS(C, 0);
  58. for(int i = n; i >= 1; i--){
  59. ri[i] = sum(b[i] - 1);
  60. ri1[i] = sum(n) - sum(b[i]);
  61. add(b[i], 1);
  62. }
  63. LL ans= 0;
  64. LL sum = 0;
  65. for(int i = n; i >= 1; i--){
  66. sum += ri1[i];
  67. }
  68. ans = sum;
  69. sum = 0;
  70. for(int i = n; i >= 1; i--){
  71. sum += ri[i];
  72. }
  73. ans *= sum;
  74. sum = 0;
  75. for(int i = 1; i <= n; i++){
  76. sum += (LL)ri[i] * (LL)ri1[i];
  77. sum += (LL)ri1[i] * (LL)le1[i];
  78. sum += (LL)le[i] * (LL)le1[i];
  79. sum += (LL)le[i] * (LL)ri[i];
  80. }
  81. ans -= sum;
  82. printf("%I64d\n", ans);
  83. }
  84. return 0;
  85. }

  

HDU5792 World is Exploding(树状数组)的更多相关文章

  1. 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]< ...

  2. 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≤ ...

  3. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  4. hdu 5792 World is Exploding 树状数组+离散化+容斥

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. 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 ...

  6. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  7. hdu-5792 World is Exploding(容斥+树状数组)

    题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Jav ...

  8. HDU-5792 World is Exploding(树状数组)

    题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...

  9. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...

随机推荐

  1. 将kindeditor在线编辑器制作成smarty插件

    在web开发中,在线编辑器是经常要用到的功能模块,虽说配置在线编辑器没有多大难度,但是每一次编写重复的代码,总是让人感觉不爽. 本篇,就来讲解一下,如何将kindeditor制作成smarty的一个自 ...

  2. Docker 存储设置

    docker默认单个容器可以使用数据空间大小10GB,docker可用数据总空间100GB,元数据可用总空间2GB.用docker info信息可以查看Data Space Total.Metadat ...

  3. JAVA8 十大新特性详解

    前言: Java8 已经发布很久了,很多报道表明Java 8 是一次重大的版本升级.在Java Code Geeks上已经有很多介绍Java 8新特性的文章, 例如Playing with Java ...

  4. Linq查询非泛型集合要指定Student类型(比如List)

    #region Linq to 集合查询非泛型集合要指定Student类型            //ArrayList list = new ArrayList();            //li ...

  5. c#.netGr idView1在div不局中

    <div style="margin:0 auto;text-align:center;" >//可以用GridView剧中 <asp:GridView ID=& ...

  6. JS中的timestamp

    http://blog.163.com/lijy_980720@126/blog/static/75574626201261783343834/

  7. VMware Workstation 下进行 桥连接

    大家都知道进行桥连接的时候,需要我们的宿主机与虚拟机同处于一个网络段, 使得mask与默认网关相同即可进行连接 ; 本地的IP .掩码 . 网关: 虚拟机的Ip 掩码,网关: // 当然这里的DNS ...

  8. nginx和apache的一些比较

    1.两者所用的驱动模式不同. nginx使用的是epoll的非阻塞模式事件驱动. apache使用的是select的阻塞模式事件驱动. 2.fastcgi和cgi的区别 当用户请求web服务的时候,w ...

  9. Effective C++ -----条款10: 令operator=返回一个reference to *this

    比如: Widget& operator=(const Widget& rhs) { ... return* this; } 令赋值(assignment)操作符返回一个referen ...

  10. simple demo how to get the list of online users

    using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...