线段树菜鸟一题+归并排序【求逆序数】POJ2299
题目链接: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的更多相关文章
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
随机推荐
- signal()函数说明
表头文件#include<signal.h> 功 能:设置某一信号的对应动作 函数原型:void (*signal(int signum,void(* handler)(int)))(in ...
- 网页制作之JavaScript部分 2 - DOM操作
1.DOM的基本概念 htmlDOM是一种面向对象的树的模型,它包含html中的所有元素:通过html可以找到所有包含在dom中的元素. DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对 ...
- CentOS6.5 配置防火墙+允许指定ip访问端口
参考博文: iptables防火墙只允许指定ip连接指定端口.访问指定网站 一.配置防火墙 打开配置文件 [root@localhost ~]# vi /etc/sysconfig/iptables ...
- 07-UIKit(tableview的编辑模式、accessoryView)
目录: 一.tableview的编辑模式-增删改查 二.不使用继承创建tableview 三.accessoryView辅助视图 回到顶部 一.tableview的编辑模式-增删改查 [1-conta ...
- xp对opengl的支持问题
我在项目中遇到的xp显示问题是因为xp对opengl的支持问题,是通过void QCoreApplication::setAttribute(Qt::ApplicationAttribute attr ...
- OPENSSL库的使用-DES篇
一.单DES算法ECB模式加解密 1.使用函数DES_set_key_unchecked设置密钥 2.使用函数DES_ecb_encrypt来进行数据加解密 void DES_ecb_encrypt( ...
- JQuery学习(3)
创建精灵界面导航: 有以下图,合理的布局让图片正确显示: 先写导航栏html代码: <div id="navMenu"> <ul id="spriteN ...
- WCF技术剖析之十七:消息(Message)详解(下篇)
原文:WCF技术剖析之十七:消息(Message)详解(下篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话)]]< ...
- 基于visual Studio2013解决C语言竞赛题之1012连接字符串
题目 解决代码及点评 /* 编写一个函数JOIN,让它实现字符串连接运算功能. */ #include <stdio.h> #include <stdl ...
- hdu5338 ZZX and Permutations
hdu5338 ZZX and Permutations 非原创,来自多校题解 不是自己写的,惭愧ing…… 留着以后自己参考…… lower_bound {1,2,4,5} 询问 2,返回的是 2 ...