线段树菜鸟一题+归并排序【求逆序数】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 ...
随机推荐
- 46黑名单显示的bug---(优化ListView)convertView复用带来的问题
是这种需求: 在黑名单的列表中前三个显示特殊的颜色,后面的列表显示其它的颜色,如图: 可是当翻到第二屏的时候.我们发现了: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- JAVA ANDROID SOCKET通信检测(SERVER)连接是否断开
Pre 解决思路 代码后记: 客户端app上的部分代码 调用: 服务器上: 客户端判断服务器是否还活着代码: PRE 在利用socket写通讯程序的时候,想检测服务器是否还活着. 从网上找了很多资料, ...
- const关键字详解
const在函数前与函数后的区别 一 const基础 如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况: int b = 500; ...
- PHP - FTP上传文件类
/** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...
- IOS SWIFT 网络请求JSON解析 基础一
前言:移动互联网时代,网络通信已经是手机端必不可少的功能.应用中也必不可少地使用了网络通信,增强客户端与服务器交互.使用NSURLConnection实现HTTP的通信.NSURLConnection ...
- gethostbyname()函数说明
gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件 #include <netdb.h> #include <sys/socket.h> ...
- Storyboard中使用UIscrollView添加约束的开发总结
第一次在项目中用storyboard做界面,一般的界面直接添加约束非常爽快 然后有个界面有scrollview,添加了约束还总是出错 刚开始使用了 wCompact,hRegular,滑动出现问题,有 ...
- 自定义类似QMutexLocker的CMutexLocker
最近做项目遇到一个需求,有一个buttonSlot()执行要耗点时间,为了防止用户无限制的乱点出现问题,考虑加一个互斥锁,使得每次执行完后才允许执行下一次.大概意思是: //QMutex m_mut ...
- android intent收集转载汇总
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); ComponentName comp = ...
- Swift学习笔记十三:继承
一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性 一.基本的语法 class Human{ var name :String init(){ na ...