hud-1754-I Hate It(线段树)
2016-01-25 14:44
295 查看
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。在每个测试的第一行,有两个正整数 N 和 M ( 0
Output
对于每一次询问操作,在一行里面输出最高成绩。Sample Input
5 61 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
56
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; }
相关文章推荐
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
- loginUtil
- Visual Studio中的文件类型(sln vcproj suo user ncb)
- 八皇后问题及其推广
- 数字图像处理10--基于特征提取的拼接方法
- RHCE 学习笔记(12) 归档压缩和文件传递
- 周爱民:真正的架构师是没有title的(图灵访谈)
- 贪心算法,递归算法,动态规划算法比较与总结
- 机器学习算法之决策树(三)
- C#使用iTextSharp打印PDF
- Windows Server 2008 R2 RDS在线激活
- ubuntu 环境下开发android 程序,识别手机步骤。
- jquery ui dialog替代confirm实例分析
- leetcode之Remove Linked List Elements
- 开源控件库
- 语音识别书籍
- Android之数据持久化三(sqllite数据库)二
- 购买价格与商品价格和SKU价格的关系
- 编写松耦合的程序——迪米特原则
- POJ 3414 dfs 回溯