Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7230 Accepted Submission(s): 3116
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.)
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
思路:那么对于这种东西,我们会想到用线段树去维护区间,可是询问有很多组,如果每次询问都去找到小于h的点插入到线段树上再查询的话,时间铁定也不够。
所以用到一个离线手法:
1.先把所有点按照高度从小到大排好,得记好下标。
2.把所有的询问按照h从小到大排好,记好下标。
做好处理之后,我们从第一个询问开始,然后从前往后把所有比 h 小的点插入到线段树上,然后进行查询,通过询问的下标记录答案,然后到下一次询问,再把小于h 的点插入,以此反复。这样子就能只做一轮插入完成所有的询问
最后全部打印就可以了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100005
#define mem(a,x) memset(a,x,sizeof(a))
struct node{
int h,pos;
bool operator < (const node tmp) const { //每个点按照高度从小到大排
return h < tmp.h;
}
}a[maxn];
struct question{
int l,r,h;
int id;
bool operator < (const question tmp) const{
return h < tmp.h; //对于询问也从小到大排
}
}q[maxn];
struct tire{
int l,r,v;
}tree[maxn * 4];
int ans[maxn];
void build(int i,int l,int r){
tree[i].l = l;
tree[i].r = r;
tree[i].v = 0;
if(l == r)
return ;
int mid = (l + r) >> 1;
build(i << 1,l,mid);
build(i << 1 | 1,mid + 1,r);
}
void update(int pos,int i){
if(tree[i].l == tree[i].r){
tree[i].v++;
return;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if(pos > mid)
update(pos,i << 1 | 1);
else
update(pos,i << 1);
tree[i].v = tree[i << 1].v + tree[i << 1 | 1].v;
}
int query(int l,int r,int i){
if(l <= tree[i].l && r >= tree[i].r)
return tree[i].v;
int mid = (tree[i].l + tree[i].r) >> 1;
if(l > mid){
return query(l,r,i << 1 | 1);
}else if(r <= mid){
return query(l,r,i << 1);
}else{
return query(l,r,i << 1) + query(l,r,i << 1 | 1);
}
}
int main(){
int t,n,m,Case = 1;
scanf("%d",&t);
while(t--){
printf("Case %d:\n" ,Case++);
scanf("%d %d",&n,&m);
build(1,1,n);
for(int i = 1;i <= n;i++){
scanf("%d",&a[i].h);
a[i].pos = i;
}
for(int i = 1;i <= m;i++){
scanf("%d %d %d",&q[i].l,&q[i].r,&q[i].h);
q[i].id = i;
}
sort(a + 1,a + 1 + n);
sort(q + 1,q + 1 + m);
int i,j;
for(i = 1,j = 1;i <= m;i++){
while(a[j].h <= q[i].h && j <= n){ // 对于小于这个询问询问高度的点就把它更新到线段树上
update(a[j++].pos,1);
}//更新完后就去查询 这个询问要求的区间 以此继续,直到询问结束
ans[q[i].id] = query(q[i].l + 1,q[i].r + 1,1);
}
for(int i = 1;i <= m;i++)
printf("%d\n",ans[i]);
}
return 0;
}