HDU 4417 Super Mario (segment tree + dynamic Array)

Source: Internet
Author: User

Super MarioTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2685 Accepted Submission (s): 1306


Problem Descriptionmario is world-famous plumber. He "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess are in trouble again and Mario needs to save his lover. We regard the road to the boss's Castle as a line (the length are N), on every integer point I there are a brick on height h I. Now the question is what many bricks in [L, R] Mario can hits 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-integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, M-T He number of queries.
Next line contains n integers, the height of each brick, and 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 I Nteger. The ith integer is the number of bricks Mario can hits 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:4,003,120,151



#include <iostream> #include <algorithm> #include <cstdio>using namespace std;const int maxn = 100050;    struct node{int l,r,len; int *sub;}    A[maxn*4];int n,m,b[maxn];void Build (int l,int r,int k) {a[k].l=l,a[k].r=r;    A[k].sub = new Int[r-l+3];    int cnt=0;    for (int i=l; i<=r; i++) a[k].sub[cnt++]=b[i];    a[k].len=cnt;    Sort (a[k].sub,a[k].sub+cnt);        if (l!=r) {int mid= (L+R) >>1;        Build (L,mid,2*k);    Build (mid+1,r,2*k+1); }}int query (int l,int r,int h,int k) {if (a[k].l==l && a[k].r==r) {int Num=upper_bound (a[k].sub,a[k]         . sub+a[k].len,h)-a[k].sub;    return num;        } else {int mid= (A[K].L+A[K].R) >>1;        if (r<=mid) return query (L,R,H,2*K);        else if (l>mid) return query (l,r,h,2*k+1);    else return query (l,mid,h,2*k) +query (mid+1,r,h,2*k+1);    }}void Delete (int l,int r,int k) {delete[] a[k].sub;        if (l!=r) {int mid= (L+R) >>1; DeleTe (l,mid,2*k);    Delete (mid+1,r,2*k+1);    }}int Main () {int t,x,y,h;    scanf ("%d", &t);        for (int co=1; co<=t; co++) {scanf ("%d%d", &n,&m);        for (int i=1; i<=n; i++) scanf ("%d", &b[i]);        Build (1,n,1);        printf ("Case%d:\n", CO);            for (int i=1; i<=m; i++) {scanf ("%d%d%d", &x,&y,&h);        printf ("%d\n", Query (x+1,y+1,h,1));    } Delete (1,n,1); } return 0;}


HDU 4417 Super Mario (segment tree + dynamic Array)

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.