题意:给出n个数,再给出q个询问,求L到R的逆序对的个数

先自己写的时候,是每次询问都重新插入来求sum(r)-sum(l)

果断T

后来还是看了别人的代码----

预处理一下,把所有可能的区间的询问都求出来(1000*1000), 然后询问就是O(1)了

然后想自己这样写超时,是因为询问太多了----

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int a[maxn];
int c[maxn];//树状数组
int n,m;
int ans[maxn][maxn]; struct node{
int x,id;
}p[maxn]; int cmp(node n1,node n2){
return n1.x < n2.x;
} int lowbit(int x){ return x &(-x);} int sum(int x){
int ret=;
while(x>){
ret += c[x];x-=lowbit(x);
}
return ret;
} void add(int x,int d){
while(x<=n){
c[x]+=d;x+=lowbit(x);
}
} int main(){
while(scanf("%d %d",&n,&m) != EOF){
memset(c,,sizeof(c));
memset(a,,sizeof(a));
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++) {
scanf("%d",&p[i].x);
p[i].id=i;
}
sort(p+,p+n+,cmp);
for(int i=,j=;i<=n;i++){
if(i==||p[i].x != p[i-].x) j++;
a[p[i].id]=j;
} // for(int i=1;i<=n;i++)
// printf("a[%d]=%d\n",i,a[i]); for(int i=;i<=n;i++){
memset(c,,sizeof(c));
int res=;
for(int j=i;j<=n;j++){
res+=(j-i)-sum(a[j]);
add(a[j],);
ans[i][j] = res;
}
} while(m--){
int l,r;
scanf("%d %d",&l,&r);
printf("%d\n",ans[l][r]);
}
}
return ;
}

HDU 5273 Dylans loves sequence【 树状数组 】的更多相关文章

  1. hdu 5273 Dylans loves sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...

  2. hdu 5273 Dylans loves sequence 逆序数简单递推

    Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  3. HDU 5273 Dylans loves sequence 暴力递推

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5273 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  4. HDU 5057 Argestes and Sequence --树状数组(卡内存)

    题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...

  5. HDU 4893 Wow! Such Sequence! (树状数组)

    题意:给有三种操作,一种是 1 k d,把第 k 个数加d,第二种是2 l r,查询区间 l, r的和,第三种是 3 l r,把区间 l,r 的所有数都变成离它最近的Fib数, 并且是最小的那个. 析 ...

  6. HDU 5273 Dylans loves sequence (逆序对,暴力)

    题意: 给定一个序列,对于q个询问:(L,R)之间有几个逆序对?序列元素个数上限1000,q上限10万.仅1测试例子. 思路: [L,R]的逆序对数量可以这么算,假设L<=K<R,将区间拆 ...

  7. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  8. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

随机推荐

  1. Web Api和Asp.Net mvc post请求区别

    这是mvc的,mvc的post请求可以这样写参数,但是web api的不行.而且content_type:"application/json" 必须要写 下面这是web api的: ...

  2. Android设计模式—— 观察者模式(以及EventBus的简单使用)

    1.概述 观察者模式最常用的地方是GUI系统.订阅-发布系统.观察者模式的一个重要作用就是解耦,将观察者和被观察者解耦,使得他们之间的依赖性更小. 2.定义 定义对象间的一种一对多依赖关系,使得每当一 ...

  3. [Linux C]系统调用(system call)和库函数调用(Library functions)

    Linux 下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions).系统调用实际上就是指最底层的一个调用,在 linux 程序设计里面就是底层 调 ...

  4. 安装node-sass及报错后解决方案

    使用npm install 命令安装node-sass时,经常出现安装失败的情况.原因在于npm服务器在美国,还有就是某强大的防火墙作用.导致模块无法下载. 1 npm install node-sa ...

  5. Lvs+heartbeat高可用高性能web站点的搭建

    这是我们公司在实际的生产环境当中使用的一套东西,希望对大家有所帮助(实际的公网ip,我已经做了相应的修改): 说明:每台服务器需要有两块网卡:eth0连接内网的交换机,用私网ip,实现服务器间内部访问 ...

  6. Spring MVC中 提交表单报错400

    背景: 在写SpringMVC表单提交的代码的时,在最后点击提交的时候总是会出现400的错误 原因: 主要原因就是表单提交的数据和对应实体类的属性无法完全匹配 解决方案: 查看我们提交的数据是否完全和 ...

  7. 记一次BootStrap的使用

    效果图如下: 一.简介: 什么是Bootstrap?  Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. 什么是响应式布局? 引用一句Bootstrap的标题语 “Boots ...

  8. ofbiz初级教程

    本教程是ofbiz 基本应用,它涵盖了OFBiz应用程序开发过程的基本原理.目标是使开发人员熟悉最佳实践,编码惯例,基本控制流程以及开发人员对OFBiz定制所需的所有其他方面. 本教程将帮助您在OFB ...

  9. ASP.NET-HttpPostedFileBase file为null的问题

    MVC使用Ajax.BeginForm上传图片时HttpPostedFileBase file为null,Request.Files获取不到文件,问题分析是页面中存在jquery.unobtrusiv ...

  10. Android socket 使用PrintWriter和BufferedReader发送和接收出现乱码问题解决

    项目中用到了Android和C++的通信.选择了用socket 发送字符的方式,一開始使用的代码是: socket=new Socket(); InetSocketAddress isa = new ...