HDU 4417 Super Mario 37th ACM/ICPC Hangzhou division Network Competition 1,008th (Division tree)

Source: Internet
Author: User
Super Mario

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 120 accepted submission (s): 73

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 input1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 1 1 9 4 0 1 0 3 5 5 5 5 5 1 6 3 1 5 7 5 7 3

 

Sample outputcase 1: 4 0 0 3 1 2 0 1 5 1

 

Source2012 ACM/ICPC Asia Regional Hangzhou online

 

Recommendliuyiding This question takes a long time. The competition is processed offline. It is made of a tree array or line segment tree... Because at the beginning, the tree array is used to write a wrong place, and the endless loop has been down to TLE. So I wrote down the line segment tree, or TLE. Later, I immediately found the error and AC immediately after the correction. The speed is still relatively fast. For a tree array or line segment tree, see the following: Http://www.cnblogs.com/kuangbin/archive/2012/09/23/2699122.html When I came back in the evening, I suddenly thought of tree division. In fact, you only need to use the tree to list the K in the second part to obtain the answer .... Orz, it turns out that so many people are quickly passing through the Division tree... It is indeed simple to write the tree, and it is not easy to make mistakes... You are too weak... Please try the competition !!!
# Include <stdio. h> # Include <Iostream> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Const   Int Maxn = 100010  ;  Int Tree [ 30 ] [Maxn]; //  Indicates the value of each position on each layer. Int Sorted [maxn]; //  Number of sorted items  Int Toleft [ 30 ] [Maxn]; //  Toleft [p] [I] indicates the number of sub-partitions from layer I to the left  Void Build ( Int L, Int R, Int  Dep ){  If (L = r) Return ;  Int Mid = (L + r)> 1  ;  Int Same = mid-L + 1 ; //  Indicates the number equal to the median value and to the left of the input.      For ( Int I = L; I <= r; I ++ )  If (Tree [Dep] [I] < Sorted [Mid]) Same -- ; Int LPOS = L;  Int Rpos = Mid + 1  ;  For ( Int I = L; I <= r; I ++ ){  If (Tree [Dep] [I] <sorted [Mid]) //  Smaller than the number in the middle. Tree [Dep + 1 ] [LPOS ++] = Tree [Dep] [I];  Else  If (Tree [Dep] [I] = sorted [Mid] & same> 0  ) {Tree [Dep + 1 ] [LPOS ++] = Tree [Dep] [I]; same -- ;}  Else    //  Greater than the center value to the right Tree [Dep + 1 ] [Rpos ++] = Tree [Dep] [I]; toleft [Dep] [I] = Toleft [Dep] [l- 1 ] + LPOS-L;//  Number of places from 1 to I on the left  } Build (L, mid, Dep + 1  ); Build (Mid + 1 , R, DEP + 1  );}  //  The number of K in the query interval. [L, R] indicates a large interval, and [L, R] indicates the number of cells to be queried.  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 ]; //  Number on the left of [L, R]      If (CNT> = K ){  //  L + number of intervals to be queried placed on the left          Int Newl = L + toleft [Dep] [l- 1 ]-Toleft [Dep] [l- 1  ];  //  The left endpoint plus the query interval will be placed on the left.          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 Solve ( Int N,Int S, Int T, Int  H ){  Int Ans = 0  ;  Int L = 1  ;  Int R = (t-s) + 1  ;  Int  Mid;  While (L <=R) {mid = (L + r)> 1  ;  Int Temp = query ( 1 , N, S, T, 0  , Mid );  If (Temp <= H) {ans = Mid; L = Mid + 1  ;}  Else R = mid- 1 ;}  Return  Ans ;}  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  T;  Int  N, m;  Int  S, T, H; scanf (  " % D  " ,& T );  Int Icase = 0  ;  While (T -- ) {Icase ++ ; Scanf (  "  % D  " , & N ,& M); memset (tree,  0 , Sizeof (Tree )); //  This is required          For ( Int I = 1 ; I <= N; I ++) //  Starting from 1  {Scanf (  "  % D  " , & Tree [ 0  ] [I]); sorted [I] = Tree [ 0 ] [I];} Sort (sorted + 1 , Sorted + N + 1  ); Build (  1 , N, 0  ); Printf (  "  Case % d: \ n  "  , Icase );  While (M -- ) {Scanf (  "  % D " , & S, & T ,& H); s ++ ; T ++ ; Printf (  "  % D \ n  "  , Solve (N, S, T, H ));}}  Return   0  ;} 

 

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.