HDU 4417 Super Mario (tree array/segment tree)

Source: Internet
Author: User

Super Mario

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)

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 integer . The ith integer is the number of bricks Mario can hits for the ith query.

Sample Input110 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 OutputCase 1:4,003,120,151 Test instructions: give you a sequence, and M inquiry, each query [L,r] in the number of less than equals H Analysis: Save sequences and Queries offline, sort the sequence from small to large, and then for queries, use a tree-like array or segment tree to query the interval and on the line.
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN = 1e5+ -;structquery{intL,r; inth; intID;} Q[MAXN];p Air<int,int>P[MAXN];intF[MAXN];intn,m;voidUpdateintXintval) {     while(x<=N) {f[x]+=Val; X+=x&-x; }}intQueryintx) {    intres=0;  while(x>0) {res+=F[x]; X-=x&-x; }    returnRes;}BOOLcmpConstquery& AA,Constquery&BB) {    returnaa.h<bb.h;}intANS[MAXN];intMain () {intT; intIcase=0; scanf ("%d",&T);  while(t--) {icase++; scanf ("%d%d",&n,&m); memset (F,0,sizeof(F));  for(intI=1; i<=n;i++) {scanf ("%d",&P[i].first); P[i].second=i; }         for(intI=1; i<=m;i++) {scanf ("%d%d%d",&q[i].l,&q[i].r,&q[i].h); Q[i].id=i,q[i].l++,q[i].r++; } sort (Q+1, q+m+1, CMP); Sort (P+1, p+n+1); intI=1, j=1;  while(j<=m) { while(i<=N) {if(p[i].first>q[j].h) Break; Update (P[i].second,1); I++; }             while(j<=m) {if(I<=n&&q[j].h>=p[i].first) Break; Ans[q[j].id]=query (Q[J].R)-query (q[j].l-1); J++; }} printf ("Case %d:\n", icase);  for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); }    return 0;}

HDU 4417 Super Mario (tree array/segment tree)

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.