我用的线段树写的。

num数组表示已插入的数值的个数。

由于a[i]数值很大,但是n不是很大,所以要离散化处理

9 1 0 5 4

离散化后

4 1 0 3 2

这样保证最大值不会超过n

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std; typedef long long ll;
const int MAXN = 543210;
int a[MAXN], mp[MAXN], num[MAXN<<2]; void push_up(int rt)
{
num[rt] = num[rt<<1] + num[rt<<1|1];
} void update(int p, int l, int r, int rt)
{
if(l == r)
{
num[rt]++;
return;
}
int m = (l + r) >> 1;
if(p <= m) update(p, lson);
else update(p, rson);
push_up(rt);
} int query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R) return num[rt];
int m = (l + r) >> 1;
int ret = 0;
if(L <= m) ret += query(L, R, lson);
if(R > m) ret += query(L, R, rson);
return ret;
} bool cmp(int A, int B)
{
return a[A] < a[B];
} int main()
{
// freopen("in.txt", "r" ,stdin);
int n;
while(~scanf("%d", &n) && n)
{
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
mp[i] = i;
}
//离散化,排序后处理
sort(mp, mp+n, cmp); //离散化排序
for(int i=0; i<n; i++)
a[mp[i]] = i; //离散化处理
ll ans = 0;
memset(num, 0, sizeof(num));
for(int i=0; i<n; i++)
{
ans += query(a[i], n-1, 0, n-1, 1);
update(a[i], 0, n-1, 1);
}
printf("%I64d\n", ans);
}
return 0;
}

POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化的更多相关文章

  1. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

  2. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  3. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  4. Poj 2299 Ultra-QuickSort(归并排序求逆序数)

    一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...

  5. poj 2299 Ultra-QuickSort(求逆序对)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52778   Accepted: 19348 ...

  6. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  7. 求逆序数的方法--线段树法&归并排序法

    逆序数的概念:对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆 ...

  8. poj 3067 Japan(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

  9. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

随机推荐

  1. mate-notification-daemon stopping, Mate notifications timeout

    mate-notification-daemon stopping, Mate notifications timeout Ask Question Asked 9 days ago Viewed 1 ...

  2. 基于Centos7.4搭建prometheus+grafana+altertManger监控Spring Boot微服务(docker版)

    目的:给我们项目的微服务应用都加上监控告警.在这之前你需要将 Spring Boot Actuator引入 本章主要介绍 如何集成监控告警系统Prometheus 和图形化界面Grafana 如何自定 ...

  3. mysql基础之日志管理(查询日志、慢查询日志、错误日志、二进制日志、中继日志、事务日志)

    日志文件记录了MySQL数据库的各种类型的活动,MySQL数据库中常见的日志文件有 查询日志,慢查询日志,错误日志,二进制日志,中继日志 ,事务日志. 修改配置或者想要使配置永久生效需将内容写入配置文 ...

  4. 如何设置 web 项目打开的默认页面

    引言 我们在创建 Web 项目启动 Tomcat 会自动打开一个默认 index.jsp 页面,这个页面是创建 Web 项目时就自动生成的.那么,如何设置 web 项目打开的这个的默认页面,改为自己的 ...

  5. MVC RadioButton控件

    @Html.RadioButtonFor(m => m.IfValid, 1, new { @id = "radio1", @name = "rdolstState ...

  6. STM32的VDD与VDDA

    http://bbs.21ic.com/icview-1651072-1-1.html VDD VSS 就是平常的电源与地.后面带A的都是模拟量的电源.

  7. 使用Python操作InfluxDB时序数据库

    使用Python操作InfluxDB时序数据库 安装python包 influxdb,这里我安装的是5.3.0版本 pip install influxdb==5.3.0   使用 from infl ...

  8. GO语言常用标准库01---strings包

    package main import ( "fmt" "strings" ) func main031() { fmt.Printf("字符形式:% ...

  9. 计图(Jittor) 1.1版本:新增骨干网络、JIT功能升级、支持多卡训练

    计图(Jittor) 1.1版本:新增骨干网络.JIT功能升级.支持多卡训练 深度学习框架-计图(Jittor),Jittor的新版本V1.1上线了.主要变化包括: 增加了大量骨干网络的支持,增强了辅 ...

  10. gpgpu-sim卡分配程序设计实例分析

    gpgpu-sim卡分配程序设计实例分析 运行代码地址:https://github.com/gpgpu-sim/gpgpu-sim_distribution 一.概述 此文件包含有关安装.生成和运行 ...