HDU 1754——I Hate It【线段树 & 维护区间最大值】

题目传送门


Problem Description

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。


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


题解

  • 传统线段树模板题
  • 维护区间最大值即可

AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MOD = 1e4;
const int MAXN = 2e5 + 7;

struct node {
	int left;
	int right;
	int lazy;
	int data;
}tree[MAXN << 2];

void PushUp(int rt) {
	tree[rt].data = max(tree[rt << 1].data, tree[rt << 1 | 1].data);
}
void PushDown(int rt) {
	if (tree[rt].lazy) {
		//下传标记
		tree[rt << 1].lazy = tree[rt].lazy;
		tree[rt << 1 | 1].lazy = tree[rt].lazy;
		//更新子节点
		tree[rt << 1].data = max(tree[rt << 1].data, tree[rt << 1].lazy * (tree[rt << 1].right - tree[rt << 1].left + 1));
		tree[rt << 1 | 1].data = max(tree[rt << 1 | 1].data, tree[rt << 1 | 1].lazy * (tree[rt << 1 | 1].right - tree[rt << 1 | 1].left + 1));
		tree[rt].lazy = 0;//清空当前节点标记
	}
}
void build(int rt, int l, int r) {
	tree[rt].lazy = 0;				/* ---设置延迟标记--- */
	tree[rt].left = l;
	tree[rt].right = r;
	if (l == r) {
		scanf("%d", &tree[rt].data);
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	PushUp(rt);
}
void updata(int rt, int l, int r, int val) {
	if (tree[rt].left >= l && tree[rt].right <= r) {
		tree[rt].data = val * (tree[rt].right - tree[rt].left + 1);
		tree[rt].lazy = val;
		return;
	}
	PushDown(rt);
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (l <= mid)	updata(rt << 1, l, r, val);
	if (r > mid)	updata(rt << 1 | 1, l, r, val);
	PushUp(rt);
}
int Query(int rt, int l, int r) {
	if (tree[rt].left >= l && tree[rt].right <= r) {
		return tree[rt].data;
	}
	PushDown(rt);
	int ans = -INF;
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (l <= mid)	ans = max(ans, Query(rt << 1, l, r));
	if (r > mid)	ans = max(ans, Query(rt << 1 | 1, l, r));
	return ans;
}
int main() {
	int n, m;
	while (cin >> n >> m) {
		build(1, 1, n);
		while (m--) {
			char c;
			int a, b;
			cin >> c >> a >> b;
			if (c == 'Q') {
				cout << Query(1, a, b) << endl;
			}
			else if (c == 'U') {
				updata(1, a, a, b);
			}
		}
	}

	return 0;
}
发布了104 篇原创文章 · 获赞 60 · 访问量 5880

猜你喜欢

转载自blog.csdn.net/Q_1849805767/article/details/103141432