hdu 5273 Dylans loves sequence 逆序数简单递推
Dylans loves sequence
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5273
Description
有Q个问题,每个问题形如(L,R)
他需要求出L−R这些数中的逆序对个数。
更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y]
Input
第一行有两个数N和Q。
第二行给出N个数字a[1]...a[N]。
接下来的Q行,每行给出两个数L,R。
N≤1000,Q≤100000,L≤R,1≤a[i]≤231−1
Output
对于每个询问,输出逆序对个数。
Sample Input
3 2
3 2 1
1 2
1 3
Sample Output
1
3
HINT
题意
题解:
N只有1000,于是想怎么来就怎么来。
最容易想到的是枚举开头,然后Nlog(N)时间里去算逆序对,用一个树状数组维护。
(可惜BC不给卡。。。呜呜呜)
仔细一想发现可以很简单地做到N2.
设ans[l][r]为l∼r的逆序对数量。首先我们暴力地先算好ans[1][1..N]。
然后i从2∼N枚举,每次计算从i开始的逆序对。
那么ans[i][j]比ans[i−1][j]少了什么呢?没错,少了a[i−1]这个数的贡献。
我们再开一个累加器cnt。枚举j从i∼N,如果a[i−1]和a[j]产生逆序对就cnt[j]=−1
然后我们从右往左累加cnt(因为贡献是前缀和性质的)
最后ans[i][j]=ans[i−1][j]+cnt[j]。
预处理完所有的答案就可以O(1)的询问啦。
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int d[][];
int sum[][];
int a[];
int main()
{
int n=read(),q=read();
for(int i=;i<=n;i++)
a[i]=read();
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
if(a[j]<a[i])
d[i][j]=d[i][j-]+;
else d[i][j]=d[i][j-];
}
for(int j=i-;j>=;j--)
{
if(a[j]>a[i])
d[i][j]=d[i][j+]+;
else
d[i][j]=d[i][j+];
}
}
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
sum[i][j]=sum[i][j-]+d[j][i];
}
while(q--)
{
int x=read(),y=read();
printf("%d\n",sum[x][y]);
}
}

hdu 5273 Dylans loves sequence 逆序数简单递推的更多相关文章
- HDU 5273 Dylans loves sequence (逆序对,暴力)
题意: 给定一个序列,对于q个询问:(L,R)之间有几个逆序对?序列元素个数上限1000,q上限10万.仅1测试例子. 思路: [L,R]的逆序对数量可以这么算,假设L<=K<R,将区间拆 ...
- hdu 5273 Dylans loves sequence
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...
- HDU 5273 Dylans loves sequence 暴力递推
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5273 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5273 Dylans loves sequence【 树状数组 】
题意:给出n个数,再给出q个询问,求L到R的逆序对的个数 先自己写的时候,是每次询问都重新插入来求sum(r)-sum(l) 果断T 后来还是看了别人的代码---- 预处理一下,把所有可能的区间的询问 ...
- HDU 5273 Dylans loves numbers(水题)
题意:给出一个0≤N≤1018,求其二进制中有几处是具有1的,假设相连的1只算1处,比如1101011就是3处. 思路:一个个数,当遇到第一个1时就将flag置为1:当遇到0就将flag置为0.当遇到 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)
Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...
- HDU 2085 核反应堆 --- 简单递推
HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...
随机推荐
- IRequiresSessionState和IReadOnlySessionState应用上的一些差异
在调用ashx时,如果需要应用Session,则必须继承接口 IRequiresSessionState,IReadOnlySessionState,但根据字面,可以知道 IRequiresSessi ...
- LoadRunner中常见参数和变量
1.参数和字符串变量的交换 ①lr_save_string(“hello world”,“param”) 将hello world 保存在参数 param中 ②lr_eval_stri ...
- Mac OS 10.8 中的 OpenCV 开发环境设置
一.编译OpenCV 要在Mac OS上使用OpenCV,需要自己编译源代码.操作过程如下: 1)从http://www.cmake.org下载cmake 2.8安装包. 2)安装cmake 2.8. ...
- 文本框的onchange事件,如何兼容各大浏览器
在项目中经常会遇到对用户输入的数据进行实时校验,而不是等文本框失去焦点或用户手动点击校验. 首先分析下在哪些情况下文本框会产生change事件. 1.用户通过键盘入正常字符时: 2.用户通过键盘输入非 ...
- HW7.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HDU1243:反恐训练营
题目链接:反恐训练营 题意:本质上是求最大公共子序列,然后加上一个权值 分析:见代码 //公共子序列问题 //dp[i][j]表示前s1的前i个与s的前j个匹配得到的最大公共子序列 #include& ...
- 验证dictionary重复键
if (dict.ContainsKey("sadsa")) { }
- JVM系列一:JVM内存组成及分配
java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...
- Rdlc报表出现空白页解决方法
在使用RDLC报表时,碰到这种情况:当只有一页数据时,报表确显示两页,第二页除了报表头之外数据为空.然后,当有多页数据时,最后一页为空. RDLC報表設計好後,在ReportViewer預覽報表時,頁 ...
- LoadRunner执行自动化以及报告自动化的方法
There are three major articles KB articles on Automating LR: 1. Command line arguments for the LoadR ...