Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9118    Accepted Submission(s): 3840

Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
 
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
 
Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
 
Sample Input
1
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
 
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
 
Source
  询问静态数组中[L,R]之间<=H的数的个数,用第R棵树的query()减去第L-1棵树的query()就是答案了。
WA了几发是因为离散化的问题,我只是离散化了ai却忘记了hi,离线把询问的数值和ai一起离散化了,
还要记住这样的话根节点的区间就是[1,N+Q]了。
  

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
#define pb push_back
#define mid ((L+R)>>1)
const int maxn=;
int a[maxn],root[maxn],N,tot;
int l[maxn],r[maxn],h[maxn];
vector<int>vi;
struct node{int lc,rc,sum;}T[maxn*];
int getid(int x){return lower_bound(vi.begin(),vi.end(),x)-vi.begin()+;}
void update(int &x,int y,int L,int R,int d){
T[++tot]=T[y],T[tot].sum++,x=tot;
if(L==R)return;
if(d<=mid) update(T[x].lc,T[y].lc,L,mid,d);
else update(T[x].rc,T[y].rc,mid+,R,d);
}
int query(int x,int L,int R,int d){
if(L==R) return T[x].sum;
if(d<=mid) return query(T[x].lc,L,mid,d);
else return T[T[x].lc].sum+query(T[x].rc,mid+,R,d);
}
int main(){
int t,q,cas=;
cin>>t;
while(t--){
tot=;
vi.clear();
scanf("%d%d",&N,&q);
for(int i=;i<=N;++i) scanf("%d",a+i),vi.pb(a[i]);
printf("Case %d:\n",++cas);
for(int i=;i<=q;++i){
scanf("%d%d%d",l+i,r+i,h+i);
vi.pb(h[i]);
}
sort(vi.begin(),vi.end()),vi.erase(unique(vi.begin(), vi.end()),vi.end());
for(int i=;i<=N;++i) update(root[i],root[i-],,N+q,getid(a[i]));
for(int i=;i<=q;++i) h[i]=getid(h[i]),printf("%d\n",query(root[r[i]+],,N+q,h[i])-query(root[l[i]],,N+q,h[i]));
}
return ;
}
 

hdu-4417-主席树+离线的更多相关文章

  1. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  2. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  3. HDU 4417 主席树写法

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  5. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  6. HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...

  7. hdu 4605 Magic Ball Game (在线主席树/离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...

  8. HDU 3333 & 主席树

    题意: balabala SOL: 这题用主席树怎么做呢...貌似一模一样...一个一个建n棵的线段树.先把上一棵树复制下来,当a[i]出现过,就把这棵树里的那个位置去掉------一模一样的思维.. ...

  9. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

  10. SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)

    DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...

随机推荐

  1. 阿里云云主机swap功能设置实战案例

    阿里云云主机swap功能设置实战案例 阿里云提供的云服务器(Elastic Compute Service,简称 ECS),是云主机的一种,当前采用的虚拟化驱动是Xen(这一点可以通过bios ven ...

  2. java多线程----JUC集合”01之 框架

    java集合的架构.主体内容包括Collection集合和Map类:而Collection集合又可以划分为List(队列)和Set(集合). 1. List的实现类主要有: LinkedList, A ...

  3. Qt之美(一):D指针/私有实现

    The English version is available at: http://xizhizhu.blogspot.com/2010/11/beauty-of-qt-1-d-pointer-p ...

  4. c++第十五天

    <c++ primer, 5E> 第94页到第99页,笔记: 1.迭代器(iterator):一种比下标访问更通用的访问容器中元素的机制. (并不是所有标准库容器都支持下标访问,<运 ...

  5. P4878 [USACO05DEC]layout布局

    P4878 [USACO05DEC]layout布局 差分约束 最短路径最长路,最长路径最短路 本题求的是最长路径,所以跑最短路 根据题意连边,然后spfa即可 注意要判断图的连通性,所以新建一个虚拟 ...

  6. C++设计模式 之 “组件协作”模式:Template Method、Strategy、Observer

    “组件协作”模式: #现代软件专业分工之后的第一个结果是“框架与应用程序的划分”,“组件协作”模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的模式. #典型模式: Templ ...

  7. 设置redis访问密码

    在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redi ...

  8. 20145101《Java程序设计》第10周学习总结

    20145101<Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 计算机网络 路由器和交换机组成了核心的 ...

  9. Android项目开发三

    微博客户端开发 本周学习计划 运用OAuth相关知识,解决上周出现的微博验证问题. 看懂微博客户端登录.用户主页等功能代码. 将程序中存在的问题解决. 实际完成情况 本周继续研究了OAuth相关知识, ...

  10. c++builder ZIP文件解压与压缩(ZLIB DLL调用)(转载 )

    转载:http://blog.csdn.net/goodai007/article/details/7414512 头文件:ZipAndFile.h //----------------------- ...