Nyoj1185 maximum and minimum values (the maximum and minimum values of a line segment tree)

Source: Internet
Author: User

If you do not understand the line segment tree, read this article first. Click Open Link

The general method for this question, TLE.

Maximum and minimum time limit: 1000 MS | memory limit: 65535 kb difficulty: 2
Description
Give n integers and execute M queries. For each query, enter three integers C, L, and R:

If C is equal to 1, the minimum value between the number of L and the number of R is output;

If C is equal to 2, the maximum value between the number of L and the number of R is output;

If C is equal to 3, the sum of the minimum and maximum values between the number of L and the number of R is output.

(Including numbers L and R ).
Input
First, enter an integer T (T ≤ 100), indicating that there is a T group of data.
For each group of data, enter an integer N (1 ≤ n ≤ 10000) to indicate that there are n integers;
The next row contains N integers A (1 ≤ A ≤ 10000 );
Then input an integer m, which indicates that there are m inquiries;
Next there are m rows (1 ≤ m ≤ 10000), each row has 3 integers C, L, R (1 ≤ C ≤ 3, 1 ≤ L ≤ r ≤ n ).
Output
Output according to the description of the question. Each output occupies one row.
Sample Input
241 3 2 421 1 42 2 351 2 3 4 513 1 5
Sample output
136
Source
Original

#include <stdio.h>#include <algorithm>#define N 10005using namespace std;struct node {int left,right,min,max;}c[N*4];int a[N];void buildtree(int l,int r,int root){c[root].left=l;c[root].right=r;if(l==r){c[root].min=c[root].max=a[l];return ;}int mid=(l+r)/2;buildtree(l,mid,root*2);buildtree(mid+1,r,root*2+1);c[root].min=min(c[root*2].min,c[root*2+1].min);c[root].max=max(c[root*2].max,c[root*2+1].max);}void findtree(int l,int r,int root,int &min1,int &max1){if(c[root].left==l&&c[root].right==r){min1=c[root].min;max1=c[root].max;return ;}int mid=(c[root].left+c[root].right)/2;if(mid<l)findtree(l,r,root*2+1,min1,max1);else if(mid>=r)findtree(l,r,root*2,min1,max1);else{int min2,max2;findtree(l,mid,root*2,min1,max1);findtree(mid+1,r,root*2+1,min2,max2);    min1=min(min1,min2);max1=max(max1,max2);}}int main(){int t,n,x,l,r,sum,m,min1,max1;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);buildtree(1,n,1);scanf("%d",&m);while(m--){scanf("%d %d %d",&x,&l,&r);findtree(l,r,1,min1,max1);if(x==1)printf("%d\n",min1);if(x==2)printf("%d\n",max1);if(x==3)printf("%d\n",min1+max1);}}return 0;}

 
 

Nyoj1185 maximum and minimum values (the maximum and minimum values of a line segment tree)

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.