[poj]3264 Balanced Lineup

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/StartoverX/p/4618041.html

Topic:

Balanced lineuptime limit:5000ms Memory limit:65536ktotal submissions:38271 accepted:17936case time Limi T:2000msdescriptionfor The daily milking, Farmer John ' s N cows (1≤n≤50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate frisbee with some of the cows. To keep things simple, he'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height. Farmer John has made a list of Q (1≤q≤200,000) potential groups of cows and their heights (1≤height≤1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the G Roup.  Inputline 1:two space-separated integers, N and Q. Lines 2..n+1:line i+1 contains a single integer this is the height of Cow i Lines n+2..n+q+1:two integers A and B (1≤a≤b≤n), representing the range of cows from A to B inclusive. OutPutlines 1..q:each line contains a single integer that's a response to a reply and indicates the difference in height being Tween the tallest and shortest cow in the range. Sample Input6 31734251 2Sample Output630

Topic Analysis:

The input is N and Q,n is the number of data 1<=n<=50000, and the size of each data is H (1<=h<=1000000). Q is the number of queries, 1<=q<=200000, and asks for the difference between the maximum and minimum values within each query range.

Requires the maximum and minimum values in the array, the first thought is to iterate over the array, however, in this case, if each query is traversed over a given range, the time complexity is O (n*q), too large. Further analysis found that the size of the array is given in the subject, a large number of query operations on the same number of groups, it is a matter of course to think of the line tree, so that each query operation to reduce the complexity of O (LgN), the total complexity of O (lgn*q).

Solution:

#include <stdio.h>intcow[50010];typedefstructtree{intMin//The maximum minimum value information is saved with the segment tree.     intMax; intRight ; intLeft ; Tree*Right_node; Tree*Left_node;} Tree;//The sequential traversal of the recursive build is the maximum minimum value within the node range of each segment tree. tree* Build (intAintb) {Tree* Tree =NewTree; Tree->right =b; Tree->left =A; if(b = =a) {tree->right_node =NULL; Tree->left_node =NULL; Tree->min = cow[tree->Left ]; Tree->max = cow[tree->Right ]; }    Else if(B >a) {intMid = (a+b)/2; Tree->right_node = Build (mid+1, B); Tree->left_node =build (A,mid); Tree->min = (Tree->right_node->min < tree->left_node->min)? Tree->right_node->min:tree->left_node->min; Tree->max = (Tree->right_node->max > Tree->left_node->max)? Tree->right_node->max:tree->left_node->Max; }    returnTree;}//query operation, if the query scope and node range consistent, directly return the maximum minimum value, or continue to recursive query. voidQuery (tree* Tree,intAintBint& Min,int&max) {    if(A = = Tree->left && b = = tree->Right ) {min= tree->min; Max= tree->Max; return; }    intMid = (tree->right + tree->left)/2; if(A >mid) {query (tree-Right_node,a,b,min,max); }    Else if(b <=mid) {query (tree-Left_node,a,b,min,max); }    Else    {        intMIN_TEMP1,MAX_TEMP1; Query (Tree-LEFT_NODE,A,MID,MIN_TEMP1,MAX_TEMP1); intMIN_TEMP2,MAX_TEMP2; Query (Tree->right_node,mid+1, B,MIN_TEMP2,MAX_TEMP2); Min= Min_temp1 < MIN_TEMP2?MIN_TEMP1:MIN_TEMP2; Max= MAX_TEMP1 > MAX_TEMP2?MAX_TEMP1:MAX_TEMP2; }}intMain () {intn,q; scanf ("%d%d",&n,&q);  for(intI=1; i<=n;i++) {scanf ("%d",&Cow[i]); } Tree* Tree = Build (1, N);  for(intI=0; i<q;i++)    {        intb; scanf ("%d%d",&a,&b); intmin_in,max_in;        Query (tree,a,b,min_in,max_in); printf ("%d\n", max_in-min_in); }         return 0;}

A lesson: Handling input and output in C's Way stdio, do not use C + + iostream, too slow, this problem is stuck here time.

[poj]3264 Balanced Lineup

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.