HDU 5273 Dylans loves sequence【 树状数组 】
题意:给出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【 树状数组 】的更多相关文章
- 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 逆序数简单递推
Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- HDU 5273 Dylans loves sequence 暴力递推
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5273 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...
- HDU 4893 Wow! Such Sequence! (树状数组)
题意:给有三种操作,一种是 1 k d,把第 k 个数加d,第二种是2 l r,查询区间 l, r的和,第三种是 3 l r,把区间 l,r 的所有数都变成离它最近的Fib数, 并且是最小的那个. 析 ...
- HDU 5273 Dylans loves sequence (逆序对,暴力)
题意: 给定一个序列,对于q个询问:(L,R)之间有几个逆序对?序列元素个数上限1000,q上限10万.仅1测试例子. 思路: [L,R]的逆序对数量可以这么算,假设L<=K<R,将区间拆 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
随机推荐
- getElementsByClassName 方法兼容性封装方法二
var getElmsByClsName = function(className, results) { results = results || []; // 判断浏览器是否支持 getEleme ...
- js 基本基础知识回顾
js中的一切的变量.函数.操作符等等都是区分大小写的. js的基本的数据类型->包含下面的5种: 1.undefined 2.Null 3.Boolean 4.Number 5.String j ...
- Linux部署之NFS方式安装系统
1. 让客户端从网络启动并且选择第二项 2. 选择语言 3. 选择键盘布局 4. 选择安装方式为NFS 5. ...
- C/C++中的函数指针
C/C++中的函数指针 一.引子 今天无聊刷了leetcode上的一道题,如下: Median is the middle value in an ordered integer list. If t ...
- Day 02 - 02 编程语言的分类
编程语言的分类 机器语言分为: 1.机器语言 优点:执行代码效率非常快 缺点:开发效率低 2.汇编语言 优点(相对于机器语言):开发效率高 缺点(相对于机器语言):执行效率低 3.高级语言 解释型(同 ...
- Python_study_day_1_while_if
1.什么是计算机 cpu:大脑 3GHz 内存:缓冲硬盘和cpu,提高计算机运算速度 硬盘:存储数据 2.编程语言的简单分类 编译型,解释型,混合型 3.python是什么编程语言 解释类语言 //. ...
- PHP内置SOAP扩展客户端的使用例子
SOAP已经是属于OUT范畴的技术了,不过因为历史原因,时不时还是会用到它,以前都是用NuSOAP,现在准备试试PHP内置的SOAP扩展.由于文本只打算说说客户端的用法,所以得先找一些能直接用的服务端 ...
- [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 3 线性代数初步
向量和矩阵 什么是矩阵/向量? Vectors and matrix are just collections of ordered numbers that represent something: ...
- python_形参、实参
#参数:形参.实参'''def display_message(title): print("My favourite book is %s" %title) #return 0 ...
- centos7下搭建solr服务器
1.Solr的环境 Solr是java开发. 需要安装jdk. 安装环境Linux. 需要安装Tomcat. 1.2. 搭建步骤 第一步:把solr 的压缩包上传到Linux系统 第二步:解压solr ...