题意:给出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. 洛谷P4014 分配问题(费用流)

    题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij​ .试设计一个将 nn 件工作分配给 nn 个人做的分配方案,使产生的总效益最大. ...

  2. ZBrush中的布料技巧分享

    今天主要给大家介绍一种在ZBrush®3D图形绘制软件中创建特定类型的布料的技巧,这种方法简单却非常强大. 这个想法源自下面这张图: 我们今天所要讲的技巧可能不是实现复杂的服装设计最有效的方法,但确实 ...

  3. Django框架详解之template

    模板简介 将页面的设计和python的代码分离开会更干净简洁更容易维护.我们可以使用Django的模板系统来实现这种模式 python的模板:HTML代码+模板语法 模板包括在使用时会被值替换掉的变量 ...

  4. Hibernate---criteria的具体使用列子

    方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge > ...

  5. 路飞学城Python-Day39(第四模块复习题)

    并发编程 一.简答题 1,简述计算机操作系统的中断的作用 由于cpu本身一次只能执行一个程序,操作系统提供的中断机制使得cpu能够实现不断的在各个程序间进行切换,给人的感觉就是多个程序同时执行 为什么 ...

  6. 七、利用frp 穿透到内网的http/https网站,实现对外开放

    有域名的话使用域名,没有域名的话使用IP注意80端口是否被已经安装使用的nginx占用,若被占用,可以换成其他端口,比如8080,,或者利用nginx的反向代理实现frp服务端与nginx共用80端口 ...

  7. pytorch 1 torch_numpy, 对比

    import torch import numpy as np details about math operation in torch can be found in: http://pytorc ...

  8. [WPF] 圆形等待效果

    原文:[WPF] 圆形等待效果 自己做着玩儿的,留着以后用,效果类似下面的 GIF 动画. <Grid Width="35" Height="35"> ...

  9. dubbo标签

    <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心. <dubbo:reference/&g ...

  10. 一个ibatis映射文件的例子(包含增删改单查,多查)

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...