HDU 4417 Super Mario(离线线段树)

来源:互联网 发布:u盘怎么装linux系统 编辑:程序博客网 时间:2024/04/28 22:33

http://acm.hdu.edu.cn/showproblem.php?pid=4417

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2206    Accepted Submission(s): 1068


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
110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
 

Sample Output
Case 1:4003120151
 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online


题意:

给出路上砖头的高度,给出区间[l,r],问区间内马里奥跳跃能力是H时能撞到砖头的数量。


看了cxlove大牛的博客才知道线段树还能这样玩。

记录砖头的位置和高度, 然后按高度升序排列;记录每次询问的数据,按跳跃能力升序排列。每次询问前将<=跳跃能力的砖头加进去(维护线段树记录区间的和值)。这样就能愉快地查询了。

划分树的方法等我长大了再写吧。


#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<set>#define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#define mm 100007using namespace std;int _sum[mm<<2],ans[mm];struct brick{    int p,h;    bool operator < (const brick &b)const    {        return h<b.h;    }}b[mm];struct _query{    int l,r,h,id;    bool operator < (const _query &q)const    {        return h<q.h;    }}q[mm];void update(int p,int k,int l ,int r){    //if (p<l && p>=r)    return;    _sum[k]++;    if (r-l==1) return ;    itn m=l+r>>1;    if (p<m)    update(p,k*2+1,l,m);    else    update(p,k*2+2,m,r);}int query(itn a,int b,int k,int l,int r){    if (b<=l || r<=a)   return 0;    if (a<=l && r<=b)   return _sum[k];    else    {        return query(a,b,k*2+1,l,l+r>>1)+query(a,b,k*2+2,l+r>>1,r);    }}int main(){    #ifndef ONLINE_JUDGE        freopen("in.txt","r",stdin);//        freopen("/home/fcbruce/文档/code/t1","w",stdout);    #endif // ONLINE_JUDGE    int T_T,n,m,x,_a,_b,h;    scanf("%d",&T_T);    for (itn t=1;t<=T_T;t++)    {        memset(_sum,0,sizeof _sum);        printf("Case %d:\n",t);        scanf("%d %d",&n,&m);        for (int i=0;i<n;i++)        {            scanf("%d",&x);            b[i].p=i;            b[i].h=x;        }        for (itn i=0;i<m;i++)        {            scanf("%d %d %d",&_a,&_b,&h);_b++;            q[i].l=_a;            q[i].r=_b;            q[i].h=h;            q[i].id=i;        }        sort(b,b+n);        sort(q,q+m);        for (int i=0,j=0;i<m;i++)        {            while (j<n && b[j].h<=q[i].h)            {                update(b[j].p,0,0,n);                j++;            }            ans[q[i].id]=query(q[i].l,q[i].r,0,0,n);        }        for (itn i=0;i<m;i++)        {            printf ("%d\n",ans[i]);        }    }    return 0;}


1 0