hdu 1754 I Hate It solving report segment tree

hdu 1754 I Hate It solving report segment tree

Problem-solving ideas: tree line. Segment tree is an upgraded version of the tree array, can be query interval, and the array can only use the prefix tree, but the tree to write simple array.
Here Insert Picture Description

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<string.h>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<sstream>
#include<set>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 100005;
const int maxn = 1e9;
using namespace std;
int n, m;
int a[200005];
int tree[800020];//tree里放区间中的最大值
void build(int id, int l, int r)
{
	if (l == r)
	{
		tree[id] = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
}
int query(int id,int l,int r,int x,int y)
{
	if (x <= l && y >= r)
	{
		return tree[id];
	}
	int mid = (l + r) >> 1;
	int mx = -1 << 30;//初始为负无穷
	if (x <= mid)
		mx = max(mx, query(id << 1, l, mid, x, y));
	if (y > mid)
		mx = max(mx, query(id << 1 | 1, mid + 1, r, x, y));
	return mx;
}
void update(int id, int l, int r, int x, int y)
{
	if (l == r)
	{
		tree[id] = y;
		return;
	}
	int mid = (l + r) >> 1;
	if (x <= mid)
		update(id << 1, l, mid, x, y);
	else
		update(id << 1 | 1, mid + 1, r, x, y);
	tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
}
int main()
{
	while (scanf("%d%d",&n,&m)!=EOF)
	{
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
		}
		build(1, 1, n);
		while (m--)
		{
			char ch;
			int x, y;
			scanf(" %c%d%d", &ch, &x, &y);//注意%c前面要留空格将上一行键入的回车消除
			if (ch == 'Q')
				printf("%d\n", query(1, 1, n, x, y));
			else
				update(1, 1, n, x, y);
		}
	}
}


Published 64 original articles · won praise 0 · Views 1461

Guess you like

Origin blog.csdn.net/weixin_45566331/article/details/104411804