您的位置:首页 > 其它

hdu1754 I Hate It--更新查询最大值

2016-07-27 13:28 253 查看
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754

一:分析

模板题,没什么难的,直接看代码就行。

二:AC代码

#define _CRT_SECURE_NO_DEPRECATE

#include<iostream>
#include<algorithm>
using namespace std;

struct Node
{
int left;
int right;
int mid;
int maxx;
};

Node node[1<<20];//2^20稍微大200,000

void build(int left, int right, int pos)
{
node[pos].left = left;
node[pos].right = right;
node[pos].mid = (left + right) / 2 + 1;
node[pos].maxx = 0;

if (left == right)
return;

build(left, (left + right) / 2, 2 * pos);
build((left + right) / 2 + 1, right, 2 * pos + 1);
}

void update(int k, int value, int pos)
{
node[pos].maxx = max(node[pos].maxx, value);

if (node[pos].left == k&&node[pos].right == k)
return;

if (k < node[pos].mid)
update(k, value, 2 * pos);
else
update(k, value, 2 * pos + 1);
}

int query(int left, int right, int pos)
{
if (node[pos].left == left&&node[pos].right == right)
return node[pos].maxx;

if (left < node[pos].mid)
{
if (right < node[pos].mid)
return query(left, right, 2 * pos);
else
return max(query(left, node[pos].mid - 1, 2 * pos), query(node[pos].mid, right, 2 * pos + 1));
}
else
return query(left, right, 2 * pos + 1);
}

int main()
{
int n, m;
int x;
char ch[2];
int a, b;

while (~scanf("%d%d", &n, &m))
{
build(1, n, 1);

for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
update(i, x, 1);
}

for (int i = 1; i <= m; i++)
{
scanf("%s%d%d", ch, &a, &b);

if (ch[0] == 'Q')
printf("%d\n", query(a, b, 1));
else
update(a, b, 1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: