题目地址: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. anchor box聚类

    fast rcnn和rfcn中使用的都是默认的anchor box设置,都是9种,比例为0.5 .1. 2,大小为128.256.512.但我的数据集的gt框更小,需要找到适合我的数据集的anchor ...

  2. 【东软实训】SQL函数

    SQL函数 SQL是用于访问和处理数据库的标准的计算机语言,我们所使用的的是Oracle SQL 一个数据库通常包含一个或多个表,每个表有一个名字表示,下图即为一个名为“emp”的表,接下来的操作都将 ...

  3. ssh架包下载地址

    1.连接MySQL数据库所需架包点击进入官网下载 2.连接Oracle数据库所需架包点击进入官网下载 3.JUnit测试所需架包点击进入官网下载或者点击进入官网下载 4.Struts所需架包点击进入官 ...

  4. pycharm connect to mysql

    1.download mysql installer community 5.7.20 https://dev.mysql.com/downloads/file/?id=473605 or 链接:ht ...

  5. 初学微信小程序 TodoList

    微信小程序的学习 微信小程序的开始尝试 TodoList 微信开发者工具生成 目录如下: . |-- app.js |-- app.json |-- app.wxss |-- pages | |-- ...

  6. 15Spring泛型依赖注入

    Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用 BaseService<T>:有RoleService和UserService两的子类 BaseRepepositr ...

  7. python 项目部署virtualenv

    安装virtualenv---------------------------------------- mac: pip3 install virtualenv ubuntu: apt instal ...

  8. 去面试Python工程师,这几个基础问题一定要能回答,Python面试题No4

    今天的面试题以基础为主,去面试Python工程师,这几个基础问题不能答错 第1题:列表和元组有什么不同? 列表和元组是Python中最常用的两种数据结构,字典是第三种. 相同点: 都是序列 都可以存储 ...

  9. 程序包javax.servlet.http不存在

    在maven test项目时,出现错误: java:[7,26] 程序包javax.servlet.http不存在 原因:pom.xml中未引入javax.servlert-api相关的包 <d ...

  10. [bzoj3668][Noi2014][起床困难综合症] (按位贪心)

    Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...