您的位置:首页 > 其它

HDU 1754 I Hate It(基础线段树~)

2017-09-11 22:27 429 查看
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, (rt << 1) | 1
#define INF 0x3f3f3f
int tree[200005 * 8], maxn;

void pushup(int rt)
{
tree[rt] = max(tree[rt << 1], tree[(rt << 1) | 1]);
}

int Query(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
return tree[rt];

else
{
int m = (l + r) >> 1;
int ans = maxn;

if (L <= m)
{
ans = max(ans, Query(L, R, lson));
}
if (R > m)
{
ans = max(ans, Query(L, R, rson));
}

return ans;
}
}

void update(int pos, int val, int l, int r, int rt)
{
if (l == r)
tree[rt] = val;
else
{
int m = (l + r) >> 1;
if (pos <= m)
update(pos, val, lson);
else
update(pos, val, rson);

pushup(rt);
}
}

void build(int l, int r, int rt)
{
if (l == r)
{
scanf("%d", &tree[rt]);
return;
}

else
{
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
}

int main(void)
{
int n, m;

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

while (m--)
{
maxn = -INF;
char s[30];
int a, b;
scanf("%s", s);
scanf("%d%d", &a, &b);
if (s[0] == 'U')
update(a, b, 1, n, 1);
if (s[0] == 'Q')
printf("%d\n", Query(a, b, 1, n, 1));
}
}

return 0;
}


上码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: