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

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

然后是自己写的线段树:

注意点在代码中。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r//2. 这一块的l,1分清楚 const int maxn=500005; struct M
{
int x;
int id;
}m[maxn]; int b[maxn];
struct Tree
{
int l,r,sum;
}tree[maxn*4];// 1.此处的结点maxn*4? 开maxn*2会RE int cmp(struct M a,struct M b)
{
return a.x<b.x;
}
void Pushup(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
} void build(int rt,int l,int r)
{
if(l==r) {tree[rt].sum=0;return;}
else
{
int m=(l+r)>>1;
build(lson);
build(rson);
}
Pushup(rt);
//tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void update(int p,int add,int rt,int l,int r)
{
if(p>r) return;
if(l==r)
{
tree[rt].sum+=add;
return;
}
else
{
int m=(l+r)>>1;
if(p<=m) update(p,add,lson);//3. 要p的作用哪。只更新一半 。因为下面有Pushup
else update(p,add,rson);
}
Pushup(rt);
}
int query(int a,int b,int rt,int l,int r)
{
int ans=0;
if(a<=l&&b>=r)
{
return tree[rt].sum;
}
else
{
int m=(l+r)>>1;
if(a<=m)
ans+=query(a,b,lson);
if(b>m) //4. 这地方是r>m果断而不是r>=m
ans+=query(a,b,rson);
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(!n) break; for(int i=1;i<=n;i++)
{
scanf("%d",&m[i].x);
m[i].id=i;
} sort(m+1,m+1+n,cmp); //离散化
b[m[1].id]=1;
for(int i=2;i<=n;i++)
{
if(m[i].x==m[i-1].x)
b[m[i].id]=b[m[i-1].id];
else b[m[i].id]=i;
} build(1,1,n); long long ans=0;
for(int i=1;i<=n;i++)
{
ans+=query(b[i]+1,n,1,1,n);
update(b[i],1,1,1,n);
}
printf("%lld\n",ans); }
return 0;
}
//还有不懂的时候调试一下,出现奇怪的地方一般是自己错了。 或者模板敲错。

线段树菜鸟一题+归并排序【求逆序数】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. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  2. eclipse java快捷模板 快捷键大全

    建议没事研究研究自己吃饭的工具,俗话说工欲善其事必先利其器嘛. 首先,快捷键这种东西大家都会知道点,但是很少人重视javaEditorTemplate这块.先介绍下Template java编辑模板 ...

  3. shell中判断用法

    测试结构: 测试命令可用于测试表达式条件的真假,true,则返回0,false,则返回非0:这一点c/c++有区别:       格式: test  expression #expression是一个 ...

  4. 文件操作中的FLAG(O_RDONLY,O_WRONLY )的值

    #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> void main(void) { ...

  5. Ibatis调用存储过程实现增删改以及分页查询

    1.Ibatis实现增删改操作很简单了,通常我是将某一模块的增删改功能写在一个存储过程里,通过一个标识符去区分执行增加还是修改抑或删除操作. statement: <!-- 存储过程:实现学生的 ...

  6. Java--CyclicBarrier使用简介

    CyclicBarrier介绍 (一)一 个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时 ...

  7. OAuth2.0认证过程

    本文以腾讯微博为例,详细介绍OAuth2.0的认证过程. 在使用腾讯微博平台提供的API前,您需要做以下两步工作: 成为开发者,并申请appkey和appsecret 授权获取accesstoken ...

  8. 演练5-5:Contoso大学校园管理系统5

    Contoso University示例网站演示如何使用Entity Framework 5创建ASP.NET MVC 4应用程序. Entity Framework有三种处理数据的方式:  Data ...

  9. 旧版本mysql下载大全,爽~~

    http://mirror.cogentco.com/pub/mysql/MySQL-4.1/ http://mirror.cogentco.com/pub/mysql/MySQL-5.0/ http ...

  10. IOS系统对fixed定位支持不好的解决方法

    问题: IOS 中所有浏览器,当页面上的输入框获得焦点时,呼出键盘. 页面底部的导航栏(position:fixed)会被键盘顶到页面的中间. 而当输入框失去焦点时,导航栏停留在页面中间,造成页面错乱 ...