传送门

  这题似乎不应该出现在这里。。

日常做法(归并):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define maxn 2000003
typedef long long LL;
LL n,ans=;
LL a[maxn],r[maxn];
inline LL read()
{
LL kr=,xs=;
char ls;
ls=getchar();
while(!isdigit(ls))
{
if(!(ls^))
kr=-;
ls=getchar();
}
while(isdigit(ls))
{
xs=(xs<<)+(xs<<)+(ls^);
ls=getchar();
}
return xs*kr;
}
void msort(LL s,LL t)
{
if(s==t) return ;
LL mid=(s+t)/;
msort(s,mid);
msort(mid+,t);
LL i=s,j=mid+,k=s;
while(i<=mid&&j<=t)
{
if(a[i]<=a[j])
r[k]=a[i],k++,i++;
else
{
r[k]=a[j],k++,j++;
ans+=mid-i+;
}
}
while(i<=mid)
r[k]=a[i],k++,i++;
while(j<=t)
r[k]=a[j],k++,j++;
for(LL i=s;i<=t;i++)
a[i]=r[i];
}
int main()
{
n=read();
for(LL i=;i<=n;i++)
a[i]=read();
msort(,n);
printf("%lld\n",ans);
return ;
}

权值线段树做法:

  需要先预处理,得到每个值在权值线段树中的位置;再按顺序将数字 a[ i ] 插入到树中相应的位置 ,接着询问树中比它大的元素个数,容易知道这些数都与当前的数 a[ i ] 形成逆序对,直接累加进 ans。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define maxn 500002
typedef long long LL;
LL n,ans=;
LL a[maxn];
struct hh
{
LL l_son,r_son,root;
}sum[maxn<<];
inline LL read()
{
LL kr=,xs=;
char ls;
ls=getchar();
while(!isdigit(ls))
{
if(!(ls^))
kr=-;
ls=getchar();
}
while(isdigit(ls))
{
xs=(xs<<)+(xs<<)+(ls^);
ls=getchar();
}
return xs*kr;
}
inline void build_sum(LL k,LL l,LL r)
{
sum[k].l_son=l;sum[k].r_son=r;
if(l==r) return ;
LL mid=sum[k].l_son+sum[k].r_son>>;
build_sum(k<<,l,mid);
build_sum(k<<|,mid+,r);
}
inline void up_date(LL k,LL q)
{
if(q==sum[k].l_son&&q==sum[k].r_son)
{
sum[k].root++;
return;
}
LL mid=sum[k].l_son+sum[k].r_son>>;
if(q<=mid) up_date(k<<,q) ;
else if(mid<q) up_date(k<<|,q);
sum[k].root=sum[k<<].root+sum[k<<|].root;
}
inline LL query(LL k,LL r,LL l)
{
if(r<=sum[k].l_son&&l>=sum[k].r_son )
return sum[k].root ;
LL mid=(sum[k].l_son+sum[k].r_son)>>;
if (r>mid) return query(k<<|,r,l);
else if(l<=mid) return query(k<<,r,l);
return query(k<<,r,mid)+query(k<<|,mid+,l);
}
int main()
{
n=read();
for(LL i=;i<=n;i++)
a[i]=read();
build_sum(,,n);
for(LL i=;i<=n;i++)
{
LL tmp=a[i];
up_date (,tmp);
ans+=i-query(,,tmp);
}
printf("%lld\n",ans);
return ;
}

P1908 逆序对的更多相关文章

  1. 洛谷P1908 逆序对

    P1908 逆序对 2.2K通过 4.4K提交 题目提供者该用户不存在 标签云端 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 归并排序党注意了!数组要开… ...

  2. 「杂烩」精灵魔法(P1908逆序对弱化版)

    「杂烩」精灵魔法(P1908逆序对弱化版) 题面: 题目描述 \(Tristan\)解决了英灵殿的守卫安排后,便到达了静谧的精灵领地--\(Alfheim\) .由于$ Midgard$ 处在$ Al ...

  3. 洛谷P1908 逆序对【递归】

    题目:https://www.luogu.org/problemnew/show/P1908 题意:给定一个数组,求逆序对个数. 思路: 是一个很经典的题目了.通过归并排序可以求逆序对个数. 现在有一 ...

  4. P1908 逆序对-(树状数组)

    https://www.luogu.org/problem/P1908 比较喜欢线段树,懒得用树状数组(只会套模板,位运算的精髓没有领悟到),一直没有记录树状数组代码,又得捡回来,趁这道题记录一下模板 ...

  5. P1908 逆序对-(cdq分治)

    https://www.luogu.org/problem/P1908 沿用归并排序的思想求逆序对. 坑1:结果爆int型,需要用longlong 坑2:相对于归并排序,在比较的时候多了一个等号 举例 ...

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

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

  7. P1908 逆序对——树状数组&离散化&快读快写の学习

    题目简述: 对于给定的一段正整数序列,逆序对就是序列中 a_i>a_jai​>aj​ 且 i<ji<j 的有序对. 输出序列中逆序对的数目. 知识补充: 树状数组: 这东西就是 ...

  8. 洛谷 P1908 逆序对 Label:归并排序||树状数组 不懂

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...

  9. 洛谷 P1908 逆序对

    \[传送门qwq\] 题目描述 猫猫\(TOM\)和小老鼠\(JERRY\)最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计. 最近,\(TOM\)老猫查阅 ...

  10. (逆序对 分治法)P1908 逆序对 洛谷

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...

随机推荐

  1. c++课设

    #include <stdio.h>#include <time.h>#include <math.h>#define C 60000;struct Student ...

  2. Ububtu 14.04 安装 Hadoop 2.7.3

    1.首先安装java,配置java开发环境 下载jdk:http://www.oracle.com/technetwork/java/javase/archive-139210.html选择你想要下载 ...

  3. height:100%

    子元素的高度设置为height:100%时 继承父元素的高度为border+height 子元素的高度为height: 此时父元素的 box-sizing:content-box: border-bo ...

  4. Gym 102056L - Eventual … Journey - [分类讨论][The 2018 ICPC Asia-East Continent Final Problem L]

    题目链接:https://codeforces.com/gym/102056/problem/L LCR is really an incredible being. Thinking so, sit ...

  5. [AI][tensorflow][keras] archlinux下 tersorflow and keras 安装

    tensorflow TensorFlow is an open-source machine learning library for research and production. https: ...

  6. 微信小游戏跳一跳简单手动外挂(基于adb 和 python)

    只有两个python文件,代码很简单. shell.py: #coding:utf-8 import subprocess import math import os def execute_comm ...

  7. springboot2.0集成shiro出现ShiroDialect报错找不到AbstractTextChildModifierAttrPr

    @Bean public ShiroDialect shiroDialect() { return new ShiroDialect(); } 报错出现找不到org/thymeleaf/process ...

  8. 利用django-crontab设定定时任务

    Django中想要设定定时任务的方法有很多,如celery.apscheduler.crontab等等,本文用crontab来实现. 想用apscheduler实现请看本人另一篇博客:使用APSche ...

  9. Python---http协议.md

    一.什么是URL? URL即统一资源定位符(Uniform Resource Locator),用来唯一地标识万维网中的某一个文档.URL由协议.主机和端口(默认为80)以及文件名三部分构成,如: h ...

  10. mybatis 转义

    当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...