题意:

求逆序对个数 没有重复数字

线段树实现:

离散化。 单点修改,区间求和

// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long ans=0;
int n,t,f[2555555],sum[2555555],a[2555555];
bool cmp(int i,int j){return a[i]<a[j];}
void change(int l,int r,int pos){
if(l==r){sum[pos]=1;return;}
int mid=(l+r)>>1;
if(mid<a[t])change(mid+1,r,pos<<1|1);
else change(l,mid,pos<<1);
sum[pos]=sum[pos<<1]+sum[pos<<1|1];
}
int query(int l,int r,int pos){
if(l>a[t]&&r<=n)return sum[pos];
int mid=(l+r)>>1;
if(mid<=a[t])return query(mid+1,r,pos<<1|1);
else return query(mid+1,r,pos<<1|1)+query(l,mid,pos<<1);
}
int main(){
while(scanf("%d",&n)&&n){
ans=0;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(f+1,f+1+n,cmp);
for(int i=1;i<=n;i++)a[f[i]]=i;
for(t=1;t<=n;t++)change(1,n,1),ans+=query(1,n,1);
printf("%lld\n",ans);
}
}

归并排序(掌握得不好,,,,以后还是用segtree吧。。。):

#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
int n,a[500005],q[500005],ans=0;
void solve(int l,int r){
if(r==l)return;
int mid=(l+r)/2;
solve(l,mid);solve(mid+1,r);
int jy1=l,jy2=mid+1,jy=l;
while(jy1<=mid||jy2<=r){
if(jy2>r ||(jy1<=mid&&a[jy1]<=a[jy2]))q[jy++]=a[jy1++];
else{
if(jy1<=mid) ans+=jy2-jy;
q[jy++]=a[jy2++];
}
}
for(int i=l;i<=r;i++)a[i]=q[i];
}
signed main(){
while(scanf("%lld",&n)&&n){
ans=0;
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
solve(1,n);printf("%lld\n",ans);
}
}

POJ 2299 求逆序对个数 归并排序 Or数据结构的更多相关文章

  1. P1136 超车 归并排序 求逆序对个数

    这道题从看到它开始到做出来,已经过了快两周[因为第一次思路完全跑偏写的是暴力模拟想水过]: 题意是这样的:  jzabc除了对多米诺骨牌感兴趣外,对赛车也很感兴趣.上个周末他观看了一场赛车比赛.他总是 ...

  2. Ultra-QuickSort POJ - 2299 (逆序对)

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  3. poj 2299 Ultra-QuickSort 逆序对模版题

    用树状数组求逆序数 唯一的坑点就是sum要用long long存 直接贴代码了 以后忘了还能直接看 2333…… PS:和hdu3743代码是一样的,因为两个都是逆序对模版题…… #include&l ...

  4. poj 2299 求逆序数

    #include <iostream> ; int a[MAX]; int swap[MAX]; //临时数组 int n; //数组a的长度 __int64 result; //数组a中 ...

  5. JDOJ 1927 求逆序对

    洛谷 P1908 逆序对 洛谷传送门 JDOJ 1927: 求逆序对 JDOJ传送门 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现 ...

  6. 求逆序对[树状数组] jdoj

    求逆序对 题目大意:给你一个序列,求逆序对个数. 注释:n<=$10^5$. 此题显然可以跑暴力.想枚举1到n,再求在i的后缀中有多少比i小的,统计答案即可.这显然是$n^2$的.这...显然过 ...

  7. codevs 4163 求逆序对的数目 -树状数组法

    4163 hzwer与逆序对  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description hzwer在研究逆序对. 对于数列{a},如果 ...

  8. P1908 逆序对(归并排序)

    https://www.luogu.com.cn/problem/P1908 归并排序是用来求逆序对的 归并排序的思想就是分治 #include <bits/stdc++.h> using ...

  9. 归并排序求逆序对(poj 2299)

    归并排序求逆序对 题目大意 给你多个序列,让你求出每个序列中逆序对的数量. 输入:每组数据以一个数 n 开头,以下n行,每行一个数字,代表这个序列: 输出:对于输出对应该组数据的逆序对的数量: 顺便在 ...

随机推荐

  1. 闲着无聊时写的一个调用天气 API 的小 Demo

    分为两个部分--调用以及实现,并且由于不想折腾,直接使用了 Console 来调用. 通过firefox直接调用 Main 入口,调用以及输出 调用部分没什么好说的,主要是针对 dynamic 类型的 ...

  2. 从输入url到页面展示出来经历了哪些过程

    本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址.  此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...

  3. XML的解析方式

    //解析和输出XML public void showXml() { string filepath = Application.dataPath + @"/my.xml"; if ...

  4. 【转载】IDEA的这八条配置你一定要改!

    引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她!那 ...

  5. 【转载】JavaWeb之DBUtils QueryRunner类对数据表的增、删、查(8种结果集处理方式)、改操作

    一.使用QueryRunner类,实现对数据表的 insert delete update package com.shuhuadream.queryrunner; import java.sql.C ...

  6. Problem 16

    Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数 ...

  7. openc下cv::Mat和IplImage的相互转换

    opencv2.0的类CV::Mat和opencv1.0的IplImage之间烦人转换: cv::Mat matimg = cv::imread ("girl.jpg"); Ipl ...

  8. ASP的Global.asa使用说明

    /*-------------------ASP文档参考集-----------------------*/ *-->作者:草履虫 *-->时间:2007-4.28---2007-4.30 ...

  9. HDU 3007

    基本小圆覆盖模板题 #include <iostream> #include <algorithm> #include <cmath> using namespac ...

  10. Red Hat Linux分辨率调整

    在/etc/X11下手动产生xorg.conf文件, # vi /etc/X11/xorg.conf 文件中内容如下: ##Add the following codes: Section " ...