ACM Learning process-hdu4417 Super Mario (tree array && offline)

Source: Internet
Author: User

Problem Description

Mario 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.

Input

The 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-th E 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.)

Output

For each case, output ' Case x: ' (x is the case number starting from 1) followed by M lines, each line Containsan integer. The ith integer is the number of bricks Mario can hits for the ithquery.

Sampleinput

1

10 10

0 5 2 75 4 3 8 7 7

2 8 6

3 5 0

1 3 1

1 9 4

0 1 0

3 5 5

5 5 1

4 6 3

1 5 7

5 7 3

Sampleoutput

Case 1:

4

0

0

3

1

2

0

1

5

1

The main idea is to find the number of the sequence of Lt to RT in the range of smaller than H.

Although the sequence is fixed, there is no modification, but it is difficult to implement the online operation.

So consider the offline.

First, save all the queries. Then according to H from small to large to sort, of course, will record this query in the original is the first few.

And then from the smallest h to check, so that the small H query to ensure that the content of the elements, large h is also satisfied.

So just need to take the elements of the sequence first removed, and then from small to large and then put back, check h, to ensure that the elements are smaller than H are returned to the sequence.

The operation can then be described as follows:

1, from small to large query H.

2, with an array, 0 indicates that the element has no regression sequence, and 1 indicates that the sequence has been returned.

3, when the query H to ensure that the element is smaller than H regression sequence (first of all, the elements in the sequence must be sorted)

4, check the LT to RT than H small elements of the number, that is, the number of query interval 1, is also the interval and. Maintain this sequence with a tree-like array.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<Set>#include<queue>#include<vector>#defineLL Long Longusing namespacestd;Const intMAXN = 1e5+5;intN, M, ANS[MAXN];structnode{intVal; intID;} S[MAXN];BOOLCmpnode (node x, node Y) {returnX.val <Y.val;}structquery{intLT, RT; inth; intID;} Q[MAXN];BOOLcmpquery (query x, query y) {returnX.h <y.h;}//tree-like arrayintD[MAXN];intLowbit (intx) {    returnx& (-x);}voidAddintIdintpls) {     while(ID <= MAXN)//ID max is MAXN{D[id]+=pls; ID+=lowbit (ID); }}intSumintTo ) {    ints =0;  while(To >0) {s= S+D[to]; to-=Lowbit (to); }    returns;}intQueryint  from,intTo ) {    returnSUM (to)-sum ( from-1);}voidinput () {memset (d,0,sizeof(d)); memset (s),0,sizeof(s)); scanf ("%d%d", &n, &m);  for(inti =1; I <= N; ++i) {scanf ("%d", &s[i].val); S[i].id=i; } sort (S+1, s+1+N, Cmpnode);  for(inti =1; I <= m; ++i) {scanf ("%d%d%d", &q[i].lt, &q[i].rt, &q[i].h); Q[i].id=i; } sort (Q+1, q+1+m, cmpquery);}voidWork () {inttop =1;  for(inti =1; I <= m; ++i) { while(Top <= n && s[top].val <=q[i].h) {Add (s[top].id,1); Top++; } Ans[q[i].id]= Query (q[i].lt+1, q[i].rt+1); }}voidoutput () { for(inti =1; I <= m; ++i) printf ("%d\n", Ans[i]);}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times )        {input ();        Work (); printf ("Case %d:\n", times);    Output (); }    return 0;}

ACM Learning process-hdu4417 Super Mario (tree array && offline)

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.