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. oracle中如何判断blob类型字段是否为空

    eg.假如有表T_GA_GRJBXX  ,字段zp是blob类型 查询blob非空的记录 SELECT * FROM u_rs_sjgx.T_GA_GRJBXX TB WHERE TB.zp IS n ...

  2. Python Web学习笔记之进程与线程

    要了解二者的区别与联系,首先得对进程与线程有一个宏观上的了解. 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竟争计算机系统资源的基本单位.每一个进程都有一个自己的地址空 ...

  3. django 项目中使用多数据库 multiple databases

    假如在一个django项目中使用到了不只一个数据库, 其实这在大一点的工程中很常见,比如主从库 那么会涉及到如下一些东西 1, 定义 在settings中的DATABASE中定义会使用到的数据,比如除 ...

  4. Unix/Linux系统编程

    一,开发工具 编译器 GCC 调试工具 GDB 代码编辑 Vim 1. 编译命令 gcc hello.c -o hello # 第二个hello为新生成的可执行文件名 -o 为生成的可执行文件指定名称 ...

  5. 集成利用tesseract.exe进行ocr

    ocr是一个宽泛的概念.市场上面ocr将一直是一个不断发展.需求强烈的方向. 我认为,从难度上区分,中文ocr难于英文ocr;手写ocr难于印刷ocr.所以两两组合,中文手写体最难(比如毛体,有一些人 ...

  6. 20145327 《网络对抗》MSF基础应用

    20145327 <网络对抗>MSF基础应用 主动攻击ms08_067 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版) kali ip地址:192.168.4 ...

  7. Timer,TimerTask,Handler

    新建一个定时器线程,通过此线程每一秒发送数据到Handler,然后通过Handler来修改UI. 1.获得Handler,Timer,TimerTask对象. Handler handler=new ...

  8. Python3基础 while 斐波那契数列

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. HDU 3974 Assign the task(DFS序)题解

    题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值.给出m个询问. 思路:DFS序,将树改为线性结构,用线段树维护.start[ ]记录每个节点的编号,End[ ]为该节点的最 ...

  10. [Java] - MySQL数据库的时间设置问题.

    之前有朋友做的项目时间格式设置为String,我感觉很不好,随后自己试了试. 首先在设置数据库类型时,选择的是timestamp, 而Java的实体中设置时间的属性类型为Date, (java.uti ...