HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)

Super Mario

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


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
 

 

Recommend
liuyiding
 
 
这题我是用的离线处理。
把所有的查询存下来。
按照高度加入求和就可以逐渐求出答案。
 
树状数组求:
//1008

#include<stdio.h>

#include<iostream>

#include<algorithm>

#include<map>

#include<vector>

#include<string.h>

#include<stdlib.h>

#include<math.h>

using namespace std;

const int MAXN=100010;

int c[MAXN];

int n;

int lowbit(int x)

{

    return x&(-x);

}

void add(int i,int val)

{

    while(i<=n)

    {

        c[i]+=val;

        i+=lowbit(i);

    }

}

int sum(int i)

{

    int s=0;

    while(i>0)

    {

        s+=c[i];

        i-=lowbit(i);

    }

    return s;

}



struct Node

{

    int s,t;

    int h;

    int index;

}node[MAXN];



int answer[MAXN];



struct SS

{

    int index;

    int v;

}nn[MAXN];



bool cmp(SS a,SS b)

{

    return a.v<b.v;

}

bool cmp2(Node a,Node b)

{

    return a.h<b.h;

}





int input()

{

    int ret=0;

    char ch;

    ch=getchar();

    while(ch<'0'||ch>'9')ch=getchar();

    while(ch>='0'&&ch<='9')

    {

        ret*=10;

        ret+=ch-'0';

        ch=getchar();

    }

    return ret;

}



int main()

{

   // freopen("H.in","r",stdin);

  //  freopen("H.out","w",stdout);

    int T;

    int m;

    int iCase=0;

    //scanf("%d",&T);

    T=input();

    while(T--)

    {

        iCase++;

        scanf("%d%d",&n,&m);

        for(int i=1;i<=n;i++)

        {

            //scanf("%d",&nn[i].v);

            nn[i].v=input();

            nn[i].index=i;

        }

        for(int i=0;i<m;i++)

        {

           // scanf("%d%d%d",&node[i].s,&node[i].t,&node[i].h);

           node[i].s=input();

           node[i].t=input();

           node[i].h=input();

            node[i].s++;

            node[i].t++;

            node[i].index=i;

        }

        memset(c,0,sizeof(c));



        sort(node,node+m,cmp2);





     /*   for(int i=0;i<m;i++)

          printf("%d  %d  %d  %d\n",node[i].s,node[i].t,node[i].index,node[i].h);

*/

        sort(nn+1,nn+1+n,cmp);



      /*  for(int i=1;i<=n;i++)

          printf("%d  %d\n",nn[i].index,nn[i].v);

*/

        int i,j;

        i=1;

        j=0;



        while(j<m)

        {

            while(i<=n)

            {

                if(nn[i].v>node[j].h)break;

               // printf("i:%d\n",i);

                add(nn[i].index,1);

                i++;



            }

            while(j<m)

            {

                //printf("%d  %d\n",node[j].h,nn[i].v);

                if(i<=n&&node[j].h>=nn[i].v)break;

               // printf("j:%d\n",j);

                answer[node[j].index]=sum(node[j].t)-sum(node[j].s-1);

                //printf("%d  %d\n",node[i].t,node[i].s-1);

               // printf("answer:%d   %d\n",node[j].index,answer[node[j].index]);

                j++;



            }



        }

        printf("Case %d:\n",iCase);

        for(int i=0;i<m;i++)

          printf("%d\n",answer[i]);

    }

    return 0;

}

 

 
线段树求:
 
//1008

#include<stdio.h>

#include<iostream>

#include<algorithm>

#include<map>

#include<vector>

#include<string.h>

#include<stdlib.h>

#include<math.h>

using namespace std;

const int MAXN=100010;





struct NN

{

    int l,r;

    int sum;

    int lazy;

    int t;

}segTree[MAXN*3];

int s[MAXN];

void Build(int i,int l,int r)

{

    segTree[i].l=l;

    segTree[i].r=r;

    segTree[i].sum=0;

    segTree[i].lazy=0;

    if(l==r)

    {

        s[l]=i;

        return;

    }

    int mid=(l+r)>>1;

    Build(i<<1,l,mid);

    Build((i<<1)|1,mid+1,r);

}

void update(int t)

{

    int m=s[t];

    while(m)

    {

        segTree[m].sum+=1;

        m>>=1;

    }

}

int query(int i,int l,int r)

{

    if(segTree[i].l==l&&segTree[i].r==r)return segTree[i].sum;

    int mid=(segTree[i].l+segTree[i].r)>>1;

    if(r<=mid)return query(i<<1,l,r);

    else if(l>mid) return query((i<<1)|1,l,r);

    else return query(i<<1,l,mid)+query((i<<1)|1,mid+1,r);

}

struct Node

{

    int s,t;

    int h;

    int index;

}node[MAXN];



int answer[MAXN];



struct SS

{

    int index;

    int v;

}nn[MAXN];



bool cmp(SS a,SS b)

{

    return a.v<b.v;

}

bool cmp2(Node a,Node b)

{

    return a.h<b.h;

}





int input()

{

    int ret=0;

    char ch;

    ch=getchar();

    while(ch<'0'||ch>'9')ch=getchar();

    while(ch>='0'&&ch<='9')

    {

        ret*=10;

        ret+=ch-'0';

        ch=getchar();

    }

    return ret;

}



int main()

{

    //freopen("H.in","r",stdin);

    //freopen("H.out","w",stdout);

    int T;

    int m;

    int n;

    int iCase=0;

    //scanf("%d",&T);

    T=input();

    while(T--)

    {

        iCase++;

        scanf("%d%d",&n,&m);

        Build(1,1,n);

        for(int i=1;i<=n;i++)

        {



            nn[i].v=input();

            nn[i].index=i;

        }

        for(int i=0;i<m;i++)

        {



           node[i].s=input();

           node[i].t=input();

           node[i].h=input();

            node[i].s++;

            node[i].t++;

            node[i].index=i;

        }



        sort(node,node+m,cmp2);







        sort(nn+1,nn+1+n,cmp);



        int i,j;

        i=1;

        j=0;

        while(j<m)

        {

            while(i<=n)

            {

                if(nn[i].v>node[j].h)break;

                update(nn[i].index);

                i++;

            }

            while(j<m)

            {

                if(i<=n&&node[j].h>=nn[i].v)break;

               answer[node[j].index]=query(1,node[j].s,node[j].t);

                j++;

            }

        }

        printf("Case %d:\n",iCase);

        for(int i=0;i<m;i++)

          printf("%d\n",answer[i]);

    }

    return 0;

}

 

你可能感兴趣的:(super)