Hdoj topic 4417 Super Mario (dividing tree to find interval ratio k small number + two points)

Source: Internet
Author: User

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


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

Source2012 ACM/ICPC Asia Regional Hangzhou Online
recommendliuyiding | We have carefully selected several similar problems for you:5363 5362 5361 5360 5359 AC Code
#include <stdio.h> #include <string.h> #include <stdlib.h> int tree[30][100100],toleft[30][100100];  int sorted[100100];  int cmp (const void *a,const void *b) {return * (int *) a-* (int *) b;      } void Build (int l,int r,int dep) {if (l==r) return;      int mid= (L+R) >>1;      int same=mid-l+1;      int i;      int lpos=l;      int rpos=mid+1;      for (i=l;i<=r;i++) {if (Tree[dep][i]<sorted[mid]) same--; } for (i=l;i<=r;i++) {if (Tree[dep][i]<sorted[mid]) {TREE[DEP+1][LPOS++]=TR          Ee[dep][i]; } else if (tree[dep][i]==sorted[mid]&&same>0) {tree[dep+1][                  Lpos++]=tree[dep][i];              same--;              } else tree[dep+1][rpos++]=tree[dep][i];      Toleft[dep][i]=toleft[dep][l-1]+lpos-l;      } build (L,mid,dep+1);  Build (mid+1,r,dep+1); } int Query (int l,int r,int l,int r,int dep,int k) {if (l==r) {return tree[dep][l];      } int mid= (L+R) >>1;      int CNT=TOLEFT[DEP][R]-TOLEFT[DEP][L-1];          if (cnt>=k) {int NEWL=L+TOLEFT[DEP][L-1]-TOLEFT[DEP][L-1];          int newr=newl+cnt-1;      return query (L,MID,NEWL,NEWR,DEP+1,K);          } else {int newr=r+toleft[dep][r]-toleft[dep][r];          int newl=newr-(R-L-CNT);      return query (MID+1,R,NEWL,NEWR,DEP+1,K-CNT); }}int Main () {int t,c=0;scanf ("%d", &t), while (t--) {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {int i;for (i=1 ; i<=n;i++) {scanf ("%d", &tree[0][i]); sorted[i]=tree[0][i];} Qsort (Sorted+1,n,sizeof (sorted[1]), CMP), build (1,n,0);p rintf ("Case%d:\n", ++c), while (m--) {int a,b,h;scanf ("%d%d%d ", &a,&b,&h); A++;b++;int l=1,r= (b-a) +1;int ans=0;while (l<=r) {int mid= (l+r) >>1;int temp=query (1 , n,a,b,0,mid); if (temp<=h) {ans=mid;l=mid+1;} Elser=mid-1;} printf ("%d\n", ans);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 4417 Super Mario (dividing tree to find interval ratio k small number + two points)

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.