题意:

给定一个长度为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. [luoguP2073] 送花(set)

    传送门 set #include <set> #include <cstdio> #include <iostream> #define LL long long ...

  2. python 快排,堆排,归并

    #归并排序 def mergeSort(a,L,R) :     if(L>=R) :         return     mid=((L+R)>>1)     mergeSort ...

  3. linux下库文件的编程

    编程到了一定的时候,总喜欢追求新的东西.将代码尽量模块化就是我的追求之一,原来只是满足于将代码从单文件中分离,通过头文件和实现文件实现模块化,后来发现最好的方法是打包成库文件,使用更加方便.尽管在li ...

  4. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  5. POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】

    题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...

  6. codeforces 892E(离散化+可撤销并查集)

    题意 给出一个n个点m条边的无向联通图(n,m<=5e5),有q(q<=5e5)个询问 每个询问询问一个边集{Ei},回答这些边能否在同一个最小生成树中 分析 要知道一个性质,就是权值不同 ...

  7. Windows下SVN服务器及客户端的使用

    原文地址:windows下配置VisualSVN Server服务器 作者:Deem_passion 下载安装文件: 服务端安装文件:VisualSVN-Server-1.6.2 客户端安装文件:To ...

  8. kvm虚拟化学习笔记(一)之kvm虚拟化环境安装

    平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...

  9. 【python】搜索引擎方面的资料

    http://blog.csdn.net/hguisu/article/category/1230933

  10. Linux进程IPC浅析[进程间通信SystemV共享内存]

    Linux进程IPC浅析[进程间通信SystemV共享内存] 共享内存概念,概述 共享内存的相关函数 共享内存概念,概述: 共享内存区域是被多个进程共享的一部分物理内存 多个进程都可把该共享内存映射到 ...