hdu 1754 I Hate It (单点更新,区间查询)

题意:

给出n个学生的分数,每次可以选择两个操作,1是选择将区间某个点学生的分数更新,2是询问区间学生的最大值。

题解:

入门题,直接搞。

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void cmax(int& a, int b){ if (b>a)a = b; }
void cmin(int& a, int b){ if (b<a)a = b; }
void cmax(ll& a, ll b){ if (b>a)a = b; }
void cmin(ll& a, ll b){ if (b<a)a = b; }
void add(int& a, int b, int mod){ a = (a + b) % mod; }
void add(ll& a, ll b, ll mod){ a = (a + b) % mod; }
#define lson rt<<1
#define rson rt<<1|1
const int oo = 0x3f3f3f3f;
const ll OO = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1000000007;
const double eps = 1e-9;
const int maxn = 2000005;
struct SegTree{
	int l, r, max;
	int mid(){
		return (l + r) >> 1;
	}
}tree[maxn << 1];

void push_up(int rt){
	tree[rt].max = max(tree[lson].max, tree[rson].max);
}

void build(int l, int r, int rt){
	tree[rt].l = l;
	tree[rt].r = r;
	if (l == r){
		scanf("%d", &tree[rt].max);
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, lson);
	build(mid + 1, r, rson);
	push_up(rt);
}

void update(int value, int pos, int rt){
	if (tree[rt].l == tree[rt].r && tree[rt].l == pos){
		tree[rt].max = value;
		return;
	}
	int mid = tree[rt].mid();
	if (pos <= mid) update(value, pos, lson);
	else update(value, pos, rson);
	push_up(rt);
}

int query(int l, int r, int rt){
	if (l <= tree[rt].l && tree[rt].r <= r)
		return tree[rt].max;
	int mid = tree[rt].mid();
	int res = 0;
	if (l <= mid)
		cmax(res, query(l, r, lson));
	if (mid + 1 <= r)
		cmax(res, query(l, r, rson));
	return res;
}

int main(){
	int n, q, a, b;
	char op;
	while (scanf("%d %d", &n, &q) != EOF){
		build(1, n, 1);
		while (q--){
			getchar();
			scanf("%c%d%d", &op, &a, &b);
			if (op == 'Q') printf("%d\n", query(a, b, 1));
			else update(b, a, 1);
		}
	}
	return 0;
}



你可能感兴趣的:(hdu 1754 I Hate It (单点更新,区间查询))