Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1130 Accepted Submission(s): 574
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
Recommend
liuyiding
这是2012杭州网络赛的一道题 用线段树就可以,我本身是比较害怕线段树的,不知道如何优化,我想到的是在用线段树的时候,每个区间都取一下最大值,最小值,结果这样还是不行。 最后看的解题报告,他是先排序,动态更新,保证在查询的是啊,线段树中的高度都是满足要求的。然后在卡区间。 看区间内有多少个符合要求的。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct num
{
int val,id;
}a[1000000];
struct Num
{
int l,r,h;
int id;
}b[1000000];
struct tree
{
int l,r,sum;
}c[1000000];
int res[1000000];
int cmp1(const void *e,const void *f)
{
struct num *p1=(struct num *)e;
struct num *p2=(struct num *)f;
return (p1->val - p2->val);
}
int cmp2(const void *e,const void *f)
{
struct Num *p1=(struct Num *)e;
struct Num *p2=(struct Num *)f;
return p1->h - p2->h;
}
int main()
{
void build(int k,int l,int r);
void update(int k,int id);
int sum(int k,int l,int r);
int i,j,n,m,s,t;
int tem=1;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
build(1,1,n);
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i].val);
a[i].id=i+1;
}
qsort(a,n,sizeof(a[0]),cmp1);
for(i=0;i<=m-1;i++)
{
scanf("%d %d %d",&b[i].l,&b[i].r,&b[i].h);
b[i].l+=1;
b[i].r+=1;
b[i].id=i+1;
}
qsort(b,m,sizeof(b[0]),cmp2);
for(i=0,j=0;i<=m-1;i++)
{
while(j<=n-1&&a[j].val<=b[i].h)
{
update(1,a[j].id);
j+=1;
}
res[b[i].id]=sum(1,b[i].l,b[i].r);
}
printf("Case %d:\n",tem++);
for(i=1;i<=m;i++)
{
printf("%d\n",res[i]);
}
}
return 0;
}
void build(int k,int l,int r)
{
int mid;
c[k].sum=0;
c[k].l=l; c[k].r=r;
if(l==r)
{
return ;
}
mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
}
void update(int k,int id)
{
if(c[k].l<=id&&c[k].r>=id)
{
c[k].sum+=1;
update(k<<1,id);
update(k<<1|1,id);
}
}
int sum(int k,int l,int r)
{
int mid;
if(c[k].l==l&&c[k].r==r)
{
return (c[k].sum);
}
mid=(c[k].l+c[k].r)>>1;
if(mid<l)
{
return sum(k<<1|1,l,r);
}else if(mid>=r)
{
return sum(k<<1,l,r);
}else
{
return sum(k<<1,l,mid) + sum(k<<1|1,mid+1,r);
}
}