解题报告

题意:

求逆序数。

思路:

线段树离散化处理。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
using namespace std;
LL sum[2001000],num[501000],_hash[501000];
void push_up(int rt)
{
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
void update(int rt,int l,int r,int p,LL v)
{
if(l==r)
{
sum[rt]=+v;
return;
}
int mid=(l+r)/2;
if(p<=mid)update(rt*2,l,mid,p,v);
else update(rt*2+1,mid+1,r,p,v);
push_up(rt);
}
LL q_sum(int rt,int l,int r,int ql,int qr)
{
if(ql>r||qr<l)return 0;
if(ql<=l&&r<=qr)return sum[rt];
int mid=(l+r)/2;
return q_sum(rt*2,l,mid,ql,qr)+q_sum(rt*2+1,mid+1,r,ql,qr);
}
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
LL ans=0;
if(!n)break;
memset(_hash,0,sizeof(_hash));
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
for(i=0; i<n; i++)
{
scanf("%LLd",&num[i]);
_hash[i]=num[i];
}
sort(_hash,_hash+n);
int m=unique(_hash,_hash+n)-_hash;
for(i=0; i<n; i++)
{
int t=lower_bound(_hash,_hash+m,num[i])-_hash+1;
ans+=q_sum(1,1,m,t+1,m);
update(1,1,m,t,1);
}
printf("%lld\n",ans);
}
return 0;
}

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 41278   Accepted: 14952

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is
sorted in ascending order. For the input sequence 

9 1 0 5 4 ,


Ultra-QuickSort produces the output 

0 1 4 5 9 .


Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence
element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source

POJ训练计划2299_Ultra-QuickSort(线段树/单点更新)的更多相关文章

  1. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  2. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  5. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  6. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  7. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  9. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

随机推荐

  1. SQL Server 2008 空间数据存储摘抄(SRID 点 MultiPoint LineString MultiLineString 多边形 MultiPolygon GeometryCollection)

    有两种类型的空间数据.geometry 数据类型支持平面或欧几里得(平面球)数据.geometry 数据类型符合适用于 SQL 规范的开放地理空间联盟 (OGC) 简单特征 1.1.0 版. 另外,S ...

  2. navicate备份与还原数据库

    参考:https://jingyan.baidu.com/article/574c521977dea06c8d9dc1b1.html 1.备份数据库 首先新建数据库 选中数据库下方的“备份”,右击“新 ...

  3. Node.js实现简单的爬取

    学习[node.js]也有几天时间了,所以打算写着练练手:索然我作为一个后端的选手,写起来还有那么一丝熟悉的感觉.emmm~~  ‘货’不多讲 ,开搞........ 首先是依赖选择: 代码块如下: ...

  4. Tomcat server.xml配置文件

    server.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to ...

  5. css选择器(1)——元素选择器、类名和id选择器

    css的主要优点之一就是它能很容易地向所有同类型的元素应用一组样式.当然它是通过选择器来实现这一点的. 基本规则结构: 语法= 选择器 +声明块 1.元素选择器——直接使用html元素名,指向文档元素 ...

  6. 杭电 2141 Can you find it? (二分法)

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  7. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

  8. 安装MySQL for Windows 数据库

    在官网下载MySql数据库windows版本:http://dev.mysql.com/downloads/file/?id=459075 下载到本地解压出来,如下图: 复制上面目录下的my-defa ...

  9. 大数据学习——面试用sql——累计报表

    create table t_access_times(username string,month string,salary int)row format delimited fields term ...

  10. Linux 下eval命令揭秘

    eval 命令中文意思是“重新计算求出参数内容”,该命令大致有以下几个作用 将变量名替换后并执行 beautifulsoup4- Desktop Documents hc.war lxml- mybl ...