HDU 4417 Super Mario (offline tree array | tree Division)

Source: Internet
Author: User
Super Mario Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2584 accepted submission (s): 1252


Problem descriptionmario 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.
Inputthe 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 .)
Outputfor 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
 
Source2012 ACM/ICPC Asia Regional Hangzhou online
Recommendliuyiding | we have carefully selected several similar problems for you: 5041 5040 5039 5038 question: Give You A series with a length of N (1e5. Then M (1e5) queries. L, R, and H ask the number of numbers in the [L. R] sequence not greater than H. Idea: it is easy to think of if we know that H is the number of numbers in the [L, R] series. You can see that the number of the intervals is no greater than that of the interval. The K-number in the interval is obviously the strength of tree division. However, we do not know the maximum number of H values. So we can do this in binary mode. Then the problem is solved. Time complexity. O (N * log (n) ^ 2 ). It is acceptable. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;int seg[20][maxn],lnum[20][maxn],sa[maxn];int n,m;void btree(int L,int R,int d){    int i,ls,rs,lm,mid;    if(L==R)        return ;    mid=(L+R)>>1;    ls=L,rs=mid+1;    lm=mid-L+1;    for(i=L;i<=R;i++)        if(seg[d][i]<sa[mid])            lm--;    for(i=L;i<=R;i++)    {        lnum[d][i]=(i==L)?0:lnum[d][i-1];        if(seg[d][i]==sa[mid])        {            if(lm>0)            {                lm--;                lnum[d][i]++;                seg[d+1][ls++]=seg[d][i];            }            else                seg[d+1][rs++]=seg[d][i];        }        else if(seg[d][i]<sa[mid])        {            lnum[d][i]++;            seg[d+1][ls++]=seg[d][i];        }        else            seg[d+1][rs++]=seg[d][i];    }    btree(L,mid,d+1);    btree(mid+1,R,d+1);}int qu(int L,int R,int l,int r,int d,int k){    int ss,s,bb,b,mid;    if(L==R)        return seg[d][L];    ss=(l==L)?0:lnum[d][l-1];    s=lnum[d][r]-ss;    mid=(L+R)>>1;    if(s>=k)        return qu(L,mid,L+ss,L+ss+s-1,d+1,k);    else    {        bb=l-L-ss;        b=r-l+1-s;        return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s);    }}void init(){    int i;    for(i=1;i<=n;i++)    {        scanf("%d",&seg[0][i]);        sa[i]=seg[0][i];    }    sort(sa+1,sa+n+1);    btree(1,n,0);}int bin(int L,int R,int h){    int low=1,hi=R-L+1,mid,ans=0;    while(low<=hi)    {        mid=(low+hi)>>1;        if(qu(1,n,L,R,0,mid)<=h)            ans=mid,low=mid+1;        else            hi=mid-1;    }    return ans;}int main(){    int L,R,h,t,cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        init();        printf("Case %d:\n",cas++);        while(m--)        {            scanf("%d%d%d",&L,&R,&h);            L++,R++;            printf("%d\n",bin(L,R,h));        }    }    return 0;}

Another clever method is the offline tree array. Sometimes offline can make the original complex problem much simpler. Imagine if we insert numbers not greater than H into the corresponding position of the tree array for each query, such as l, R, and H. Then, each time you ask [L, R] How many numbers are inserted. So we sort the series by H. Sort queries by H. Then, for each query, insert the series of H not greater than the query H into the tree array. In this way, the time complexity only reduces the O (N * log (N) code. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;int C[maxn],ans[maxn],n,m;struct qnode{    int l,r,h,id;    inline bool operator <(const qnode &tt) const    {        return h<tt.h;    }} qu[maxn];struct node{    int p,h;    inline bool operator <(const node &tt) const    {        return h<tt.h;    }} bk[maxn];void update(int x,int d){    for(int i=x;i<=n;i+=i&-i)        C[i]+=d;}int getsum(int x){    int sum=0;    for(int i=x;i>0;i-=i&-i)        sum+=C[i];    return sum;}int main(){    int t,cas=1,i,j;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        C[n]=0;        for(i=0;i<n;i++)        {            C[i]=0,bk[i].p=i+1;            scanf("%d",&bk[i].h);        }        for(i=0;i<m;i++)        {            scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].h);            qu[i].l++,qu[i].r++,qu[i].id=i;        }        sort(bk,bk+n);        sort(qu,qu+m);        for(i=j=0;i<m;i++)        {            for(;j<n&&bk[j].h<=qu[i].h;j++)                update(bk[j].p,1);            ans[qu[i].id]=getsum(qu[i].r)-getsum(qu[i].l-1);        }        printf("Case %d:\n",cas++);        for(i=0;i<m;i++)            printf("%d\n",ans[i]);    }    return 0;}


HDU 4417 Super Mario (offline tree array | tree Division)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.