题目地址:POJ 2299

这题以前用归并排序做过。线段树加上离散化也能够做。一般线段树的话会超时。

这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w。能够从数字大的開始向树上加点,然后统计下标比它小即在它左边的数的个数。由于每加一个数的时候。比该数大的数已经加完了,这时候坐标在它左边的就是一对逆序数。

可是该题另一个问题,就是数字反复的问题。这时候能够在排序的时候让下标大的在前面,这样就能够保证加点的时候下标比他小的数中不会出现反复的。

这题须要注意的是要用__int64。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
LL sum[2100000];
struct node
{
LL x, id;
} fei[600000];
bool cmp(node x, node y)
{
if(x.x==y.x)
return x.id>y.id;
return x.x>y.x;
}
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(LL x, int l, int r, int rt)
{
if(l==r)
{
sum[rt]++;
return ;
}
int mid=l+r>>1;
if(x<=mid) update(x,lson);
else update(x,rson);
PushUp(rt);
}
LL query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
LL ans=0;
int mid=l+r>>1;
if(ll<=mid) ans+=query(ll,rr,lson);
if(rr>mid) ans+=query(ll,rr,rson);
return ans;
}
int main()
{
int n, i, j;
LL ans;
while(scanf("%d",&n)!=EOF&&n)
{
memset(sum,0,sizeof(sum));
for(i=1; i<=n; i++)
{
scanf("%I64d",&fei[i].x);
fei[i].id=i;
}
sort(fei+1,fei+n+1,cmp);
ans=0;
for(i=1; i<=n; i++)
{
ans+=query(1,fei[i].id,1,n,1);
update(fei[i].id,1,n,1);
}
printf("%I64d\n",ans);
}
return 0;
}

POJ 2299 Ultra-QuickSort(线段树+离散化)的更多相关文章

  1. poj 1151 (未完成) 扫描线 线段树 离散化

    #include<iostream> #include<vector> #include<cmath> #include<algorithm> usin ...

  2. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  4. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  5. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  6. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  7. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  8. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  9. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  10. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

随机推荐

  1. caffe和图像一些基础知识

    1.卷积层的参数放置在convoluytion_param{}中,pad默认是0,stride默认是1,如果在convoluytion_param中没有写pad = 什么,或者stride = 什么, ...

  2. 【计算机网络】2.6 P2P应用

    第二章第六节 P2P应用 在本节内容开始前,我们要先来对P2P架构有一个宏观的认知: P2P:(Peer to Peer 对等结构)   以对等方式进行通信,并不区分客户端和服务端,而是平等关系进行通 ...

  3. 对散列进行sort排序,只是会产生一个顺序,但这顺序和初始化时的顺序不一致

    109 my $slice_layouts;    110 my $vertical_assignment;    111 my %map_function_indices = (    112    ...

  4. IIS+php服务器无法上传图片解决办法

    查找网上资料,发现php.ini下面有2个地方关于上传的配置: file_uploads = On  这里设置是否允许HTTP上传,默认应该为ON的 ;upload_tmp_dir=  这里设置上传文 ...

  5. Web项目ConcurrentModificationException异常

    后台SSH在做Session删除的时候,遇到了ConcurrentModificationException异常. 参考资料:http://blog.csdn.net/idesvo/article/d ...

  6. MapReduce实例——查询缺失扑克牌

    问题: 解决: 首先分为两个过程,Map过程将<=10的牌去掉,然后只针对于>10的牌进行分类,Reduce过程,将Map传过来的键值对进行统计,然后计算出少于3张牌的的花色 1.代码 1 ...

  7. 数组排序函数-php数组函数(一)

    数组排序,共13个 函数中有u的,能自定义比较函数:有k的,按照键来排序:有r(reverse)的,倒序:有a(association)的,一定是键值关联,除了rsort() usort() sort ...

  8. UVa 806 四分树

    题意: 分析: 类似UVa 297, 模拟四分树四分的过程, 就是记录一个左上角, 记录宽度wideth, 然后每次w/2这样递归下去. 注意全黑是输出0, 不是输出1234. #include &l ...

  9. HTML文件中css样式的引用

    1.1.直接在div中使用css样式制作div+css网页 如: <div style="font-size:14px; color:#FF0000;">内容</ ...

  10. 九度oj 题目1181:遍历链表

    题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3483 解决:1465 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...