线段树菜鸟一题+归并排序【求逆序数】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 ...
随机推荐
- JAVA操作Hbase基础例子
package com.cma.hbase.test; import java.io.BufferedInputStream; import java.io.BufferedReader; impor ...
- c++,operator=
operator=为什么值得注意? 从语法上讲,下面的程序可以编译通过,我在另一篇笔记示例里面也这样用了. class A1 { public: int operator=(int a)//参数是in ...
- aop编程 环绕round
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 用CentOS,fedora等访问局域网中的Windows共享
来到Linux世界中已有一段时间了,感觉上好像自己的电脑成了一个孤岛.周围的人都还是用Windows系统,能相互共享文件,我用Linux系统,别人的共享文件都还不知道怎么访问?通过网上查资料学习,现在 ...
- linux环境设置:使ftp不输密码
# cd ~; # vi .netrc; Add beloew words, 可以追加多个账户: machine yourservername login yourname password your ...
- c语言,内存字节对齐
引用:内存字节对齐 写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的变量总长度要大,这是怎么回事呢?讲讲字节对齐吧. /************* ...
- xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)
链接地址:http://blog.csdn.net/mad1989/article/details/8167529 打包发布APP流程真机测试和APP发布流程APP提交审核流程真机测试打包发布上传出错 ...
- protected的一些功能
java的访问限制有private.protected.public.这里只想进一步认识一下protected. 我对protected有4个疑问: 1,在相同包中,是否可以调用其它类的protect ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- Swift - 制作一个录音机(声音的录制与播放)
1,技术介绍 (1)AVFoundation.framework框架提供了AVAudioRecorder类.它可以实现录音功能. (2)而使用该框架的AVAudioPlayer类,可以实现声音的播放. ...