HDU——1754 I hate it(线段树入门模板题 ——区间最大值问题)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。


Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取’Q’或’U’) ,和两个正整数A,B。
当C为’Q’的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为’U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。


Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output

5
6
5
9

Hint
Huge input,the C function scanf() will work better than cin
题意:给你一个学生成绩序列,接下来会进行很多查询和修改操作,请修改和输出查询的结果。

解题思路:线段树的模板题,结点数据为区间最值,若对线段树还是不太会的朋友我指路一篇博客:https://blog.csdn.net/hzf0701/article/details/107859659
PS:提交一定要使用G++编译器,C++会TLE,尽管你是使用scanf输入也一样,如果使用cin流一定要加:ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。

AC代码

/*
*邮箱:[email protected]
*blog:https://blog.csdn.net/hzf0701
*注:代码如有问题请私信我或在评论区留言,谢谢支持。
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>
#include<memory.h>//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉
#include<iomanip>
#include<vector>
#include<cstring>
#define scd(n) scanf("%d",&n)
#define scf(n) scanf("%f",&n)
#define scc(n) scanf("%c",&n)
#define scs(n) scanf("%s",n)
#define prd(n) printf("%d",n)
#define prf(n) printf("%f",n)
#define prc(n) printf("%c",n)
#define prs(n) printf("%s",n)
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 2e5+2;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为代码自定义代码模板***************************************//

int father[maxn];//存储在线段树中的下标。
struct Node{
	int left;
	int right;
	int value;
}node[maxn<<2];
void BuildTree(int i,int l,int r){
	node[i].left=l;node[i].right=r;
	node[i].value=0;
	if(l==r){
		father[l]=i;       //保存叶子结点在线段树中的下标
		return;//到了叶子结点自然要返回咯。
	}
	BuildTree(i<<1,l,(l+r)/2);  //初始化左子树
	BuildTree(i<<1|1,(l+r)/2+1,r); //初始化右子树。
}
void UpdateTree(int i){
	if(i==1)return;//说明已到达根节点。
	int fi=i>>1;//获取父结点的下标
	node[fi].value=max(node[fi<<1].value,node[fi<<1|1].value);
	UpdateTree(fi);
}
int maxx;
//查询操作,调用函数为QueryTree(1,l,r)。
void QueryTree(int i,int l,int r){
	if(node[i].left==l&&node[i].right==r){
		 maxx=max(maxx,node[i].value);
		 return;
	}
	i=i<<1;
	if(l<=node[i].right){
		//说明在左子树中
		if(r<=node[i].right){
			//说明全部在左子树中。
			QueryTree(i,l,r);
		}
		else{
			//说明部分在左子树中
			QueryTree(i,l,node[i].right);
		}
	}
	i+=1;
	if(r>=node[i].left){
		//说明在右子树中
		if(l>=node[i].left){
			//说明全部在右子树中。
			QueryTree(i,l,r);
		}
		else{
			//说明部分在右子树中。
			QueryTree(i,node[i].left,r);
		}
	}
}
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。
	cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
	int n,m,temp;
	while(cin>>n>>m){
		BuildTree(1,1,n);
		rep(i,1,n){
			cin>>temp;
			node[father[i]].value=temp;
			UpdateTree(father[i]);
		}
		char ch;
		int a,b;
		while(m--){
			cin>>ch>>a>>b;
			if(ch=='Q'){
				maxx=0;
				QueryTree(1,a,b);
				cout<<maxx<<endl;
			}
			else{
				node[father[a]].value=b;
				UpdateTree(father[a]);
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/107860410
今日推荐