http://codeforces.com/problemset/problem/61/E

题意是求 i<j<k && a[i]>a[j]>a[k] 的对数

会树状数组求逆序数的话,这个推一下就能出结果:

做法:

1、离散化,由于a[i]能够达到1e9

2、插入a[i]的时候,记录x[i]=i-sum(a[i]); a[i]之前比a[i]大的有x[i]个

3、插入完毕后,求a[i] 之后比a[i]小的数的个数y[i]

ans=segma(x[i]*y[i])   注意x[i]*y[i]会超出int  由于这wa了一次

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <map>
  9. #include <set>
  10. #include <queue>
  11. using namespace std;
  12.  
  13. #define ls(rt) rt*2
  14. #define rs(rt) rt*2+1
  15. #define ll long long
  16. #define ull unsigned long long
  17. #define rep(i,s,e) for(int i=s;i<e;i++)
  18. #define repe(i,s,e) for(int i=s;i<=e;i++)
  19. #define CL(a,b) memset(a,b,sizeof(a))
  20. #define IN(s) freopen(s,"r",stdin)
  21. #define OUT(s) freopen(s,"w",stdout)
  22. const ll ll_INF = ((ull)(-1))>>1;
  23. const double EPS = 1e-8;
  24. const int INF = 100000000;
  25. const int MAXN = 1e6+100;
  26. ll x[MAXN],y[MAXN];
  27. int a[MAXN],tmp[MAXN];
  28. ll c[MAXN];
  29. int n;
  30.  
  31. inline int lowb(int x) {return x&(-x);}
  32.  
  33. void update(int x, int d)
  34. {
  35. while(x<=n)
  36. {
  37. c[x]+=d;
  38. x+=lowb(x);
  39. }
  40. }
  41.  
  42. ll sum(int x)
  43. {
  44. ll ret=0;
  45. while(x>0)
  46. {
  47. ret+=c[x];
  48. x-=lowb(x);
  49. }
  50. return ret;
  51. }
  52.  
  53. bool cmp(const int i, const int j)
  54. {
  55. return a[i]<a[j];
  56. }
  57.  
  58. void dis()
  59. {
  60. sort(tmp+1,tmp+1+n,cmp);
  61. int tt=0,pre=a[tmp[1]]-1;
  62. for(int i=1;i<=n;i++)
  63. {
  64. if(pre!=a[tmp[i]])
  65. {
  66. pre=a[tmp[i]];
  67. a[tmp[i]]=++tt;
  68. }
  69. else
  70. a[tmp[i]]=tt;
  71. }
  72. }
  73.  
  74. int main()
  75. {
  76. //IN("B.txt");
  77. ll ans=0;
  78. while(~scanf("%d",&n))
  79. {
  80. for(int i=1;i<=n;i++)
  81. {
  82. scanf("%d",&a[i]);
  83. tmp[i]=i;
  84. }
  85. dis();
  86. /////////////////
  87. /*for(int i=1;i<=n;i++)
  88. {
  89. printf("a[%d]=%d\n",i,a[i]);
  90. }*/
  91. /////////////////
  92. CL(c,0);
  93. ans=0;
  94. for(int i=1;i<=n;i++)
  95. {
  96. update(a[i],1);
  97. x[i]=i-sum(a[i]);//a[i]之前比a[i]大的有`x[i]个
  98. y[i]=sum(a[i]-1);//a[i]之前比a[i]小的数
  99. }
  100. for(int i=1;i<=n;i++)
  101. {
  102. y[i]=sum(a[i]-1)-y[i];//a[i] 之后比a[i]小的数的个数
  103. ans+=x[i]*y[i];
  104. }
  105. /////////////////
  106. /* for(int i=1;i<=n;i++)
  107. printf("i=%d x=%d y=%d\n",i,x[i],y[i]);*/
  108. /////////////////
  109. printf("%I64d\n",ans);
  110. }
  111.  
  112. return 0;
  113. }

CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序的更多相关文章

  1. HDU 1394 树状数组+离散化求逆序数

    对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...

  2. POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  3. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)

    题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...

  6. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  7. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场

    题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...

  9. HDU5877 Weak Pair dfs + 线段树/树状数组 + 离散化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: weak pair的要求: 1.u是v的祖先(注意不一定是父亲) 2.val[u]*va ...

随机推荐

  1. javascript MD5

    var MD5 = function (string) { function RotateLeft(lValue, iShiftBits) { return (lValue<<iShift ...

  2. 商品标签例子——CSS3 transform 属性

    积累很重要.从此开始记录前端生涯的点滴.... <!DOCTYPE html><html lang="en"><head> <meta c ...

  3. 各版本IIS安装方法

      各版本IIS安装方法 Windows 2000 V5.0 将操作系统安装光盘放入光驱,打开“控制面板”→“添加或删除程序”→“添加/删除 Windows 组件”,勾选“Internet信息服务(I ...

  4. 多层架构+MVC+EF+AUTOFAC+AUTOMAPPER

    最近使用ligerui搭建了一个简单的教务管理demo,将重要的地方记录,也希望能帮到有这方面需要园友. 一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证 ...

  5. CentOs上搭建git服务器

    CentOs上搭建git服务器 首先安装setuptools wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0 ...

  6. ESXi控制台TSM:弥补vSphere Client不足

    当vSphere Client不能完成某些任务时,主机的ESXi控制台及其技术支持模式(TSM)可能能派上用场. ESXi控制台允许管理员执行不能通过vSphere Client进行配置的管理任务,比 ...

  7. Objective-C学习篇10—NSDate与NSDateFormatter

    NSDate NSDate 时间类,继承自NSObject,其对象表示一个时间点 NSDate *date = [NSDate date]; NSLog(@"date = %@", ...

  8. python实现tailf

    # -*- coding:utf-8 -*- ''' Created on 2016年10月28日 @author: zhangsongbin ''' import time class file_r ...

  9. 获取MP3和M4A音乐文件的歌曲信息以及专辑图片--备用

    NSBundle* bundle = [NSBundle mainBundle];     NSString* path = [bundle bundlePath];     NSURL * file ...

  10. MPMoviePlayerController 电影播放器—备用

    MPMoviePlayerController 与AVAudioPlayer有点类似,前者播放视频,后者播放音频,不过也有很大不同,MPMoviePlayerController 可以直接通过远程UR ...