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; }
相关文章推荐
- android include 在父子layout可见性设置
- ICMP,ARP
- Linux入门笔记——文件操作命令1
- boost智能指针 与 内存分配
- JavaScript实现局部打印
- jsp page指令
- [编写高质量iOS代码的52个有效方法](七)内存管理(上)
- 51nod 编辑距离问题(动态规划)
- C++11 左值、右值、右值引用详解
- HDU 5734 Acperience(水~)
- (总结)关于Linux的缓存内存 Cache Memory详解
- 【Windows】 PE文件头学习
- [php学习十一]javaScript的基本练习2
- 用友数据库“可能发生了架构损坏。请运行 DBCC CHECKCATALOG。”错误修复
- @property的本质是什么?ivar、getter、setter是如何生成并添加到这个类中的
- Spring系列之Java代理机制实现AOP
- HDU 5753 Permutation Bo(期望)
- text
- 【AppGameKit】数独图形界面化
- 简单软件方法/算法/思想