nyoj1185 最大最小值 (线段树求区间最大值和最小值)

对于不懂线段树的,先看为这篇文章理解下。点击打开链接

这道题普通方法 ,TLE。

最大最小值

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

给出N个整数,执行M次询问。

对于每次询问,首先输入三个整数C、L、R:

如果C等于1,输出第L个数到第R个数之间的最小值;

如果C等于2,输出第L个数到第R个数之间的最大值;

如果C等于3,输出第L个数到第R个数之间的最小值与最大值的和。

(包括第L个数和第R个数)。

输入
首先输入一个整数T(T≤100),表示有T组数据。

对于每组数据,先输入一个整数N(1≤N≤10000),表示有N个整数;

接下来一行有N个整数a(1≤a≤10000);

然后输入一个整数M,表示有M次询问;

接下来有M行(1≤M≤10000),每行有3个整数C、L、R(1≤C≤3,1≤L≤R≤N)。

输出
按照题意描述输出。每个输出占一行。
样例输入
2
4
1 3 2 4
2
1 1 4
2 2 3
5
1 2 3 4 5
1
3 1 5
样例输出
1
3
6
来源
原创
#include <stdio.h>
#include <algorithm>
#define N 10005
using 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;
}

时间: 2024-08-27 19:30:46

nyoj1185 最大最小值 (线段树求区间最大值和最小值)的相关文章

hdoj 5443 The Water Problem【线段树求区间最大值】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 刷道水题助助兴 #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> #define MAX 2100 #define INF 0x7fffff using namespace std; int n,m; int Max[MAX]; void pushup(int

2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-th question is whether P remains balanced after pai and pbi  swapped. Note that questions ar

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

HDU6447 YJJ&#39;s Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 ?原题目描述在最下面. ?1e5个点,问从(0,0)走到(1e9,1e9)的最大收益. ?当你从(u-1,v-1)走到(u,v)时,你可以获得点(u,v)的权值. Solution: ?十分详细了. ?直接线段树区间最值.当然也可以树状数组,不能st表. ?\(dp[i] = max(query\_max(0,dp[i]-1,1)+val[i] ,

poj 3264 线段树 求区间最大最小值

Description For 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 will take a contiguous rang

【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps

https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意考虑一个月内装满多个房间和装满所有房间后不用再购买的情况. 代码: #include <iostream> #include <cstdio> #include <algorithm> const int maxn = 100005; const int INF = 0x3

LightOJ Array Queries 1082【线段树求区间最值】

1082 - Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index

HDU 2795 Billboard 【线段树维护区间最大值&amp;&amp;查询变形】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 28743    Accepted Submission(s): 11651 Problem Description At the entrance to the un