题目链接:http://poj.org/problem?id=2299

归并排序解法链接:http://blog.csdn.net/lyy289065406/article/details/6647346

然后是自己写的线段树:

注意点在代码中。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. #define lson rt<<1,l,m
  8. #define rson rt<<1|1,m+1,r//2. 这一块的l,1分清楚
  9.  
  10. const int maxn=500005;
  11.  
  12. struct M
  13. {
  14. int x;
  15. int id;
  16. }m[maxn];
  17.  
  18. int b[maxn];
  19. struct Tree
  20. {
  21. int l,r,sum;
  22. }tree[maxn*4];// 1.此处的结点maxn*4? 开maxn*2会RE
  23.  
  24. int cmp(struct M a,struct M b)
  25. {
  26. return a.x<b.x;
  27. }
  28. void Pushup(int rt)
  29. {
  30. tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
  31. }
  32.  
  33. void build(int rt,int l,int r)
  34. {
  35. if(l==r) {tree[rt].sum=0;return;}
  36. else
  37. {
  38. int m=(l+r)>>1;
  39. build(lson);
  40. build(rson);
  41. }
  42. Pushup(rt);
  43. //tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
  44. }
  45. void update(int p,int add,int rt,int l,int r)
  46. {
  47. if(p>r) return;
  48. if(l==r)
  49. {
  50. tree[rt].sum+=add;
  51. return;
  52. }
  53. else
  54. {
  55. int m=(l+r)>>1;
  56. if(p<=m) update(p,add,lson);//3. 要p的作用哪。只更新一半 。因为下面有Pushup
  57. else update(p,add,rson);
  58. }
  59. Pushup(rt);
  60. }
  61. int query(int a,int b,int rt,int l,int r)
  62. {
  63. int ans=0;
  64. if(a<=l&&b>=r)
  65. {
  66. return tree[rt].sum;
  67. }
  68. else
  69. {
  70. int m=(l+r)>>1;
  71. if(a<=m)
  72. ans+=query(a,b,lson);
  73. if(b>m) //4. 这地方是r>m果断而不是r>=m
  74. ans+=query(a,b,rson);
  75. }
  76. return ans;
  77. }
  78. int main()
  79. {
  80. int n;
  81. while(scanf("%d",&n)!=EOF)
  82. {
  83. if(!n) break;
  84.  
  85. for(int i=1;i<=n;i++)
  86. {
  87. scanf("%d",&m[i].x);
  88. m[i].id=i;
  89. }
  90.  
  91. sort(m+1,m+1+n,cmp);
  92.  
  93. //离散化
  94. b[m[1].id]=1;
  95. for(int i=2;i<=n;i++)
  96. {
  97. if(m[i].x==m[i-1].x)
  98. b[m[i].id]=b[m[i-1].id];
  99. else b[m[i].id]=i;
  100. }
  101.  
  102. build(1,1,n);
  103.  
  104. long long ans=0;
  105. for(int i=1;i<=n;i++)
  106. {
  107. ans+=query(b[i]+1,n,1,1,n);
  108. update(b[i],1,1,1,n);
  109. }
  110. printf("%lld\n",ans);
  111.  
  112. }
  113. return 0;
  114. }
  115. //还有不懂的时候调试一下,出现奇怪的地方一般是自己错了。 或者模板敲错。

线段树菜鸟一题+归并排序【求逆序数】POJ2299的更多相关文章

  1. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  2. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  3. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  4. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  5. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  6. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  7. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  8. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  9. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

随机推荐

  1. 46黑名单显示的bug---(优化ListView)convertView复用带来的问题

    是这种需求: 在黑名单的列表中前三个显示特殊的颜色,后面的列表显示其它的颜色,如图: 可是当翻到第二屏的时候.我们发现了: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  2. JAVA ANDROID SOCKET通信检测(SERVER)连接是否断开

    Pre 解决思路 代码后记: 客户端app上的部分代码 调用: 服务器上: 客户端判断服务器是否还活着代码: PRE 在利用socket写通讯程序的时候,想检测服务器是否还活着. 从网上找了很多资料, ...

  3. const关键字详解

    const在函数前与函数后的区别 一   const基础         如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况:         int   b   =   500; ...

  4. PHP - FTP上传文件类

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  5. IOS SWIFT 网络请求JSON解析 基础一

    前言:移动互联网时代,网络通信已经是手机端必不可少的功能.应用中也必不可少地使用了网络通信,增强客户端与服务器交互.使用NSURLConnection实现HTTP的通信.NSURLConnection ...

  6. gethostbyname()函数说明

    gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件    #include <netdb.h>    #include <sys/socket.h> ...

  7. Storyboard中使用UIscrollView添加约束的开发总结

    第一次在项目中用storyboard做界面,一般的界面直接添加约束非常爽快 然后有个界面有scrollview,添加了约束还总是出错 刚开始使用了 wCompact,hRegular,滑动出现问题,有 ...

  8. 自定义类似QMutexLocker的CMutexLocker

    最近做项目遇到一个需求,有一个buttonSlot()执行要耗点时间,为了防止用户无限制的乱点出现问题,考虑加一个互斥锁,使得每次执行完后才允许执行下一次.大概意思是: //QMutex  m_mut ...

  9. android intent收集转载汇总

    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);                 ComponentName comp = ...

  10. Swift学习笔记十三:继承

    一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性 一.基本的语法 class Human{ var name :String init(){ na ...