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. Lombok让pojo变得更优雅

    Lombok 采取注解的形式,标记在pojo上面,在编译后,自动生成相应的方法,像get.set.构造方法等都可以注解一键生成. 引入jar包: <dependency> <grou ...

  2. html/jquery最实用功能与注意点

    获取某元素的父元素 通常用在根据被click的td获取tbody时. parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合.parents则是取得一个包含着所有匹配元素的祖先元素的元素集合 ...

  3. 通过一个例子了解Ajax

    Ajax指的Asyncronous JavaScript and XML Ajax并不是什么新的编程语言, 它是现有一些东西的应用.从它的名称中就可以看出来 假如我们设想, 浏览器展示了一个页面,但需 ...

  4. Android项目开发三

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

  5. loj 诗歌

    链接 链接 思路 好久之前的考试题了吧,之前貌似抄的题解 现在理解了怕忘了,就写个题解记录一下吧,题目还是不错的 枚举中间点j \[H_{i}-H_{j}=H_{j}-H_{k}\] \[H_{k}+ ...

  6. HDU 1285 确定比赛名次(拓扑排序)题解

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  7. hdu 3415 Max Sum of Max-K-sub-sequence 单调队列优化DP

    题目链接: https://www.cnblogs.com/Draymonder/p/9536681.html 同上一篇文章,只是 需要记录最大值的开始和结束的位置 #include <iost ...

  8. cent os下搭建简单的服务器

    作为常和网络打交道的程序员,经常会遇到需要服务器的场合,比如搭建一个web服务器,一个代理服务器,又或者一个小型的游戏服务器. 我时常和朋友一起玩一款叫我的世界的游戏,为了能够长期稳定地联机玩,所以特 ...

  9. Maven java.lang.OutOfMemeoryError 问题

    设置MAVEN_OPTS环境变量的值为:-Xms128m -Xmx512m Java默认的最大可用内存往往不能够满足Maven运行的需要 比如在项目较大时,使用Maven生成项目站点需要占用大量的内存 ...

  10. python 集合删除元素

    #Create a new set num_set = , , , , , ]) #Discard number num_set.discard() print(num_set)