题意:

给定一个长度为n的数组,有m次的查询,每次查询[a,b]区间里比H小的数有多少个?

由于n和m的取值范围为0到10的5次方,所以直接回答会超时,所以考虑先读入所有的查询操作,然后依次回答比H小的[a,b]区间里的数有多少个,

求和类似于求正序数的方法。

写法可以边插变查,也可以边查边插,边查边插简单些;

可是这道题从头wa到了尾,原因是add函数少打了等于号,扇自己一巴掌.

边插边查:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
#include <cstring>
#include<algorithm>
#define maxn 110000
#define LL int
using namespace std;
struct node
{
LL value,id;
};
struct querynode
{
LL L,R,H=-,cnt;
};
node a[maxn];
querynode que[maxn];
LL c[maxn];
LL N,M;
LL ans[maxn];
void init()
{
memset(a,,sizeof(a));
memset(c,,sizeof(c));
memset(ans,,sizeof(ans));
} bool cmp1(node a,node b)
{
return a.value<b.value;
}
bool cmp2(querynode a,querynode b)
{
return a.H<b.H;
}
int inline lowbit(int x){
return x & (-x);
}
void inline add(int x){
while(x <= N){
c[x]++;
x += lowbit(x);
}
}
int inline sum(int x){
int s = ;
while(x > ){
s += c[x];
x -= lowbit(x);
}
return s;
} void solve()
{
int t=;
for(int i=;i<=N;i++)
{
if(que[t].H==-)
{
break;
}
while(que[t].H<a[i].value && que[t].H!=-)
{
ans[ que[t].cnt ]=sum(que[t].R)-sum(que[t].L-);
t++;
}
add(a[i].id);
while(a[i].value==a[i+].value && i<=N)
{
i++;
add(a[i].id);
}
while(que[t].H==a[i].value && que[t].H!=-)
{
ans[ que[t].cnt ]=sum(que[t].R)-sum(que[t].L-);
t++;
}
}
for(int i=t;i<=M;i++)
{
ans[que[i].cnt]=sum(que[i].R)-sum(que[i].L-);
}
}
int main()
{
// freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
int Case=;
for(int Case=;Case<=t;Case++)
{
scanf("%d%d",&N,&M);
init();
for(int i=;i<=N;i++)
{
scanf("%d",&a[i].value);
a[i].id=i;
}
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&que[i].L,&que[i].R,&que[i].H);
que[i].L++;
que[i].R++;
que[i].cnt=i;
}
sort(a+,a+N+,cmp1);
sort(que+,que+M+,cmp2);
solve();
printf("Case %d:\n",Case);
for(int i = ; i <= M; ++i){
printf("%d\n",ans[i]);
}
} return ;
}

边查边插:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
#include <cstring>
#include<algorithm>
#define maxn 110000
#define LL int
using namespace std;
struct node
{
LL value,id;
};
struct querynode
{
LL L,R,H,cnt;
};
node a[maxn];
querynode que[maxn];
LL c[maxn];
LL N,M;
LL ans[maxn];
void init()
{
memset(a,,sizeof(a));
memset(c,,sizeof(c));
memset(ans,,sizeof(ans));
for(int i=;i<maxn;i++)
{
que[i].H=-;
}
} bool cmp1(node a,node b)
{
return a.value<b.value;
}
bool cmp2(querynode a,querynode b)
{
return a.H<b.H;
}
int inline lowbit(int x){
return x & (-x);
}
void inline add(int x){
while(x <= N ){
c[x]++;
x += lowbit(x);
}
}
int inline sum(int x){
int s = ;
while(x > ){
s += c[x];
x -= lowbit(x);
}
return s;
} void solve1()
{
for(int aski = ,ditj = ; aski < M; ++aski){
while(ditj < N && que[aski].H >= a[ditj].value){
add(a[ditj].id);
ditj++;
}
ans[que[aski].cnt] = sum(que[aski].R) - sum(que[aski].L - );
}
}
int main()
{
//freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
int Case=;
for(int Case=;Case<=t;Case++)
{
scanf("%d%d",&N,&M);
init();
for(int i=;i<N;i++)
{
scanf("%d",&a[i].value);
a[i].id=i+;
}
for(int i=;i<M;i++)
{
scanf("%d%d%d",&que[i].L,&que[i].R,&que[i].H);
que[i].L++;
que[i].R++;
que[i].cnt=i+;
}
sort(a,a+N,cmp1);
sort(que,que+M,cmp2);
solve1();
printf("Case %d:\n",Case);
for(int i = ; i <= M; ++i){
printf("%d\n",ans[i]);
}
} return ;
}

hdu4417(离线操作 + 树状数组)的更多相关文章

  1. HDU3874Necklace(树状数组+离线操作)

    此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...

  2. HDU---4417Super Mario 树状数组 离线操作

    题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...

  3. Necklace(树状数组+离线操作)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 Necklace Time Limit: 15000/5000 MS (Java/Others) ...

  4. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  5. Codeforces 369E Valera and Queries --树状数组+离线操作

    题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...

  6. bzoj 1878 SDOI2009树状数组 离线操作

    本来想写2120的,结果想起来了这个 我们先对于询问左端点排序,用树状数组存区间字母个数,对于每种字母, 第一次出现的位置记录为1,剩下的记录为0,然后记录下,每种颜色 后面第一个和他相同颜色的位置 ...

  7. ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  8. hdu-4417 Super Mario(树状数组 + 划分树)

    题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu 3333(树状数组 + 离线操作)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 【BZOJ4475】子集选取(计数)

    题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...

  2. hdu3622:Bomb Game

    给n<=100对点,从每对点里面挑一个并以这些挑出的点为圆心画圆,并且这些圆不能互相覆盖,找出一种方案使得这些圆半径中最小的那个最大. “最小值最大”就是二分答案啦!考虑现在每个点都画出半径x的 ...

  3. Sigar 编译笔记

    https://blog.csdn.net/zw3413/article/details/79482438

  4. T1503 愚蠢的宠物 codevs

    http://codevs.cn/problem/1503/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 大家都知道,sh ...

  5. 初学总结--------Java修饰符与修饰关键字(且叫修饰关键字)

    Java中有类,有成员变量,有成员方法,有局部变量.他们分别能用什么来修饰? 目前学习到的类,有普通类和内部类. 一.修饰普通类: 1.public  每个文件中只有一个类能被public修饰,表示可 ...

  6. nexus3.1私服搭建

    原文:http://blog.csdn.net/qqqqq210/article/details/52993337 1.简介 近期公司需要搭建jenkins自动化构建部署,需要搭建nexus私服环境, ...

  7. Office EXCEL 如何为宏命令指定快捷键或者重新设置快捷键

    1 工具-宏-宏,打开宏窗口   2 鼠标单击任意宏将其选中,点击宏选项,即可修改或为他增加快捷键.   3 注意,你直接在Visual Basci编辑器里面改是不行的,因为加了'的只是注释而已  

  8. IIS 配置 FTP 网站 H5 音频标签自定义样式修改以及添加播放控制事件

    IIS 配置 FTP 网站   在 服务器管理器 的 Web服务器IIS 上安装 FTP 服务 在 IIS管理器 添加FTP网站 配置防火墙规则 说明:服务器环境是Windows Server 200 ...

  9. NSUserDefaults 保存自己定义对象

    项目里json返回的一个model须要保存下来,这个model是固定的没必须去创建表,想到了NSUserDefaults来存储,暂不考虑安全问题. NSUserDefaults没法直接存储一个对象.在 ...

  10. Redis实践系列丨Codis数据迁移原理与优化

    Codis介绍 Codis 是一种Redis集群的实现方案,与Redis社区的Redis cluster类似,基于slot的分片机制构建一个更大的Redis节点集群,对于连接到codis的Redis客 ...