您的位置:首页 > 其它

hud-1754-I Hate It(线段树)

2016-01-25 14:44 295 查看

Problem Description

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

这让很多学生很反感。

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

Input

本题目包含多组测试,请处理到文件结束。

在每个测试的第一行,有两个正整数 N 和 M ( 0

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

线段树,简单题

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#define N 200005
#define ll long long
const int mm = 0x3f3f3f3f;
using namespace std;
int ans[N<<2];
void build(int begin, int end, int root)
{
if (begin == end)
{
cin >> ans[root];
return ;
}
int m = (begin+end)>>1;
build(begin, m, root<<1);
build(m+1, end, root<<1|1);
ans[root] = max(ans[root<<1], ans[root<<1|1]);
}
void update(int pos, int result, int l, int r, int root)
{
if (l == r)
{
ans[root] = result;
return ;
}
int m = (l+r)>>1;
if (m >= pos)   update(pos, result, l, m, root<<1);
else    update(pos, result, m+1, r, root<<1|1);
ans[root] = max(ans[root<<1], ans[root<<1|1]);
}
int query(int L, int R, int l, int r, int root)
{
if (L <= l && R >= r)   return ans[root];
int m = (l+r)>>1;
int t = 0;
if (L <= m) t = max(t, query(L, R, l, m, root<<1));
if (R > m)  t = max(t, query(L, R, m+1, r, root<<1|1));
return t;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int i, j, a, b, n, m;
char c;
while(cin >> n >> m)
{
memset(ans, 0, sizeof(ans));
build(1, n, 1);
while(m--)
{
cin >> c >> a >> b;
if (c == 'U')
update(a, b, 1, n, 1);
else
cout << query(a, b, 1, n, 1) << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: