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. Centos7下PHP的卸载与安装nginx

    Centos7下PHP的卸载与安装nginx CentOS上PHP完全卸载,想把PHP卸载干净,直接用yum的remove命令是不行的,需要查看有多少rpm包,然后按照依赖顺序逐一卸载. 1.首先查看 ...

  2. Mysql 更改编码方式

    Mysql 更改编码方式 --查看编码方式 show variables like 'char%'; --设置编码方式 set character_set_server=utf8;

  3. 函数指针(pointer to function)——qsort函数应用实例

    一,举例应用 在ACM比赛中常使用 stdlib.h 中自带的 qsort 函数,是教科书式的函数指针应用示范. #include <stdio.h> #include <stdli ...

  4. 判断某个ImageView的背景图是否是已知的背景图相等

    if (ibtn.getDrawable().getConstantState().equals(getResources().getDrawable(R.mipmap.image).getConst ...

  5. fastjson 简单使用 及其JSONObject使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  6. 【Tomcat部署】Linux环境部署war包到tomcat

    以turbine为例. 一.部署 1.下载或者生成war包(从maven上下载war包,并改名字为turbine.war) 2.将turbine.war拷贝到$TOMCAT_HOME/webapps中 ...

  7. Luogu P1533 可怜的狗狗

    题目链接:https://www.luogu.org/problemnew/show/P1533 没人写$fhq\ treap$做法,那我就补一篇qwq 看到这题第一时间想主席树,然后发现我还没学主席 ...

  8. 如何每日增量加载数据到Hive分区表

    如何每日增量加载数据到Hive分区表 hadoop hive shell crontab 加载数据 数据加载到Hive分区表(两个分区,日期(20160316)和小时(10))中 每日加载前一天的日志 ...

  9. 圆点博士 陀螺仪和加速度计MPU6050的单位换算方法

    圆点博士陀螺仪和加速度计MPU6050的单位换算方法 陀螺仪和加速度计MPU6050的单位换算方法 对于四轴的初学者,可能无法理解四轴源代码里面陀螺仪和加速度数据的那些数学转换方法.下面我们来具体描述 ...

  10. BZOJ 1015: [JSOI2008]星球大战starwar(并查集求连通块+离线处理)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1015 题意: 思路:好题啊!!! 这道题目需要离线处理,先把所有要删的点给保存下来,然后逆序加点,这 ...