Hdoj The Water problem 5443 (segment tree interval max-value problem)

Source: Internet
Author: User
The water problem Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 921 Accepted Submission (s): 731


Problem Description in land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with A1,a2,a3,..., an representing the size of the water source. Given a set of queries each containing 2 integers l and R, please find out the biggest water source between Al and Ar.
Input First you is given an integer T (t≤10) indicating the number of test cases. For each test case, there was a number n (0≤n≤1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,..., an, and each are in {1,..., 106}. On the next line, there is a number Q (0≤q≤1000) representing the number of queries. After that, there'll be Q lines with the integers l and R (1≤l≤r≤n) indicating the range of which you should find out the Biggest water source.
Output for each query, output an integer representing the size of the biggest water source.
Sample Input
3 1 100 1 1 1 5 1 2 3 4 5 5 1 2 1 3 2 4 3 4 3 5 3 1 999999 1 4 1 1 1 2 2 3 3 3
Sample Output
100 2 3 4 4 5 1 999999 999999 1
#include <stdio.h> #include <string.h> #include <algorithm> #define N 1010 using namespace std;
int a[n];
	struct ZZ {int l;
	int R;
int m;
}q[4*n];
	void build (int gen,int l,int r) {q[gen].l=l;
	Q[gen].r=r;
		if (l==r) {q[gen].m=a[l];
	return;
	} int mid= (L+R)/2;
	Build (Gen<<1,l,mid);
	Build (Gen<<1|1,mid+1,r);
Q[gen].m=max (Q[GEN&LT;&LT;1].M,Q[GEN&LT;&LT;1|1].M);
	} int query (int gen,int l,int R) {if (L&LT;=Q[GEN].L&AMP;&AMP;R&GT;=Q[GEN].R) return q[gen].m;
	int mid= (Q[GEN].L+Q[GEN].R)/2;
	int cnt;
	if (r<=mid) cnt=query (gen<<1,l,r);
	else if (l>=mid+1) cnt=query (gen<<1|1,l,r);
	else Cnt=max (query (gen<<1,l,mid), query (Gen<<1|1,mid+1,r));
return CNT;
	} int main () {int t,n,m,i;
	int x, y;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n);
		for (i=1;i<=n;i++) scanf ("%d", &a[i]);
		Build (1,1,n);
		scanf ("%d", &m);
			while (m--) {scanf ("%d%d", &x,&y);
		printf ("%d\n", Query (1,x,y));
	}} 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.