Hdu 4417 division tree plus the number of numbers smaller than num In the bipartite Interval

Source: Internet
Author: User

Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 771 Accepted Submission (s): 423


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

Input
The 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 .)
 

Output
For 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 Input
1
10 10
0 5 2 7 5 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
 

Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online
 
Question:

Enter cas
N m

Input n count

Each input m group indicates the number of numbers in the range of l r smaller than num.

Note that starting from l is 0

Idea: finding a number in a range can easily be thought of as a Division tree. The Division tree is used to find the k-largest number in a range.
So we can find 1st 2nd 3rd ....... Compare the value of a large number with num.

It is easy to find the number of digits smaller than and equal to num by using the bipartite method.

The following is almost just a template binary method.


[Cpp]
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
# Define M 100005
Int tree [40] [M], sorted [M];
Int toLeft [40] [M];
Void build (int level, int left, int right ){
If (left = right) return;
Int mid = (left + right)> 1;
Int I;
Int suppose; // assume that all the numbers on the left of the median sorted [mid] are smaller than sorted [mid].
Suppose = mid-left + 1;
For (I = left; I <= right; I ++ ){
If (tree [level] [I] <sorted [mid]) {
Suppose --;
}
}
// If suppose = 1, it indicates that the value of sorted [mid] in the array has only one number. For example, sequence: 1 3 4 5 6, sorted [mid] = 4
/* If suppose> 1, the left half edge value in the array is more than one number of sorted [mid], which is mid-suppose. For example, sequence: 1 4 4 4 6, sorted [mid] = 4
*/
Int lpos = left, rpos = mid + 1;
For (I = left; I <= right; I ++ ){
If (I = left)
{// Preprocessing, equivalent to Initialization
ToLeft [level] [I] = 0;
}
Else
{
ToLeft [level] [I] = toLeft [level] [I-1];
}
If (tree [level] [I] <sorted [mid]) {// divide it to the left of the median.
ToLeft [level] [I] ++;
Tree [level + 1] [lpos ++] = tree [level] [I];
} Else if (tree [level] [I]> sorted [mid]) {// divide it to the right of the median.
Tree [level + 1] [rpos ++] = tree [level] [I];
} Else {// here, suppose is divided into numbers greater than 0 to the left of the median
If (suppose! = 0) {// The processing here is too clever! Handsome!
Suppose --;
ToLeft [level] [I] ++;
Tree [level + 1] [lpos ++] = tree [level] [I];
} Else {// indicates
Tree [level + 1] [rpos ++] = tree [level] [I];
}
}
}
Build (level + 1, left, mid );
Build (level + 1, mid + 1, right );
}
// Query the k-th data in [left, right] data.
Int query (int level, int left, int right, int qleft, int qright, int k ){
If (qleft = qright)
Return tree [level] [qleft];
Int s; // indicates that multiple elements are allocated to the left of [left, qleft ).
Int ss; // number of elements to be divided into left subtree in [qleft, qright]
Int mid = (left + right)> 1;
If (left = qleft ){
S = 0;
Ss = toLeft [level] [qright];
} Else {
S = toLeft [level] [qleft-1];
Ss = toLeft [level] [qright]-s;
}
Int newl, newr;
If (k <= ss) {// query left side
Newl = left + s;
Newr = left + s + ss-1;
Return query (level + 1, left, mid, newl, newr, k );
} Else {// query the right side
Newl = mid-left + 1 + qleft-s;
Newr = mid-left + 1 + qright-s-ss;
Return query (level + 1, mid + 1, right, newl, newr, k-ss );
}
}
Int main ()
{
Int n, m, cas, ca = 0;
Scanf ("% d", & cas );
While (cas --)
{
Printf ("Case % d: \ n", ++ ca );
Scanf ("% d", & n, & m );
Int I;
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & tree [0] [I]);
Sorted [I] = tree [0] [I];
}
Sort (sorted + 1, sorted + n + 1 );
Build (0, 1, n );
Int ql, qr, num, ans = 0;
For (I = 0; I <m; I ++)
{
Scanf ("% d", & ql, & qr, & num );
Ql ++; qr ++; // note that here the meaning starts from 0.
Int mid, left = 1, right = qr-ql + 1, ans = 0;
Mid = (left + right)/2;
While (left <= right) // no equal sign is missing
{
Mid = (left + right)/2;
If (query (0, 1, n, ql, qr, mid)> num) right = mid-1;
Else {ans = mid; left = mid + 1;} // At the beginning, I did not know where it went wrong.
}
Printf ("% d \ n", ans );
}
} 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.