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 < ...
随机推荐
- struts2学习之基础笔记3
第8章Struts 2类型转换 使用类型转换器 自定义类型转换器 步骤:1. Struts 2 构建流程 2.自定义类型转换器类(继承 DefaultTypeConverter /StrutsType ...
- struts2的DTD配置文件
新手可以看看,高手可以跳过…… 最近在学习struts2这个框架,自己也动手写过一些DTD文件,所以很好struts2这个DTD文件是怎么写的,接下来就一个一个的分析 根元素是struts,然后又4个 ...
- Node_进阶_1
第一天 1.1简介 Node.js简介 V8引擎本身就是用于Chrome浏览器的JS解释部分,Ryan Dahl把这个V8搬到了服务器上,用于做服务器的软件. Node.js是一个让Javascrip ...
- WordCount合作--自己部分
前言: (1)合作者:201631062127,201631062625 (2)合作代码地址:WordCount 一.结对的PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟 ...
- 用 JavaScript 实现简单拼图游戏
本篇主要讲解,如何利用原生的 JavaScript 来实现一个简单的拼图小游戏. 线上体验地址:拼图 一.游戏的基础逻辑 想用一门语言来开发游戏,必须先了解如何使用这门语言来实现一些基础逻辑,比如图像 ...
- 自备LocalDateTime工具类
package cn.zytao.taosir.common.utils; import java.time.Instant; import java.time.LocalDate; import j ...
- jpa自定义条件分页查询
主要依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- 题解 P1774 【最接近神的人_NOI导刊2010提高(02)】
这道题很明显是求逆序对. 所谓逆序对,就是逆序的数对. 譬如在下面这个数列中: 1 2 3 4 6 5 6 5就是一个逆序对. 求逆序对的方法比较多,常见的有归并排序和树状数组(线段树当然也行). 本 ...
- 【codeforces 757E】Bash Plays with Functions
[题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...
- 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem
Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...