hdu1754 I Hate It
来源:互联网 发布:java读取xml文件内容 编辑:程序博客网 时间:2024/06/16 13:26
题目链接:hdu1754 I Hate It
思路
单点更新。区间最大。
小结
又WA了一发。设查询区间为[QL, QR],当前节点为rt,对应区间为[L, R], mid=(L+R)/2。按照我的写法,当QL小于等于mid时,递归左区间,当QR大于mid时,递归右区间。这里应该是两个if,而我写成了if-else,含义就错了。
代码
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int maxn = 200000+5;int tree[maxn<<2];void build(int rt, int l, int r){ if(l==r){ scanf("%d", &tree[rt]); } else{ int mid = (l+r)>>1; build(rt<<1, l, mid); build(rt<<1|1, mid+1, r); tree[rt] = max(tree[rt<<1], tree[rt<<1|1]); }}void Set(int rt, int id, int l, int r, int x){ if(l==r && l==id){ tree[rt] = x; } else{ int mid = (l+r)>>1; if(id<=mid) Set(rt<<1, id, l, mid, x); else Set(rt<<1|1, id, mid+1, r, x); tree[rt] = max(tree[rt<<1], tree[rt<<1|1]); }}int query(int rt, int a, int b, int l, int r){ if(a<=l && r<=b){ return tree[rt]; } else{ int mid = (l+r)>>1; int ans = -1; if(a<=mid) ans = max(ans, query(rt<<1, a, b, l, mid)); if(mid<b) ans = max(ans, query(rt<<1|1, a, b, mid+1, r)); return ans; }}int main(){ int n, m, a, b; char opt[10]; while(scanf("%d%d", &n, &m)!=EOF){ build(1, 1, n); for(int i=0; i<m; ++i){ scanf("%s%d%d", opt, &a, &b); if(opt[0]=='U'){ Set(1, a, 1, n, b); } if(opt[0]=='Q'){ printf("%d\n", query(1, a, b, 1, n)); } } } return 0;}
阅读全文
0 0
- hdu1754 I Hate It
- HDU1754 I Hate It
- HDU1754:I Hate It
- HDU1754--I Hate It
- HDU1754 I hate it
- hdu1754 I Hate It
- HDU1754:I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate it
- HDU1754 I Hate It
- hdu1754 I Hate It
- hdu1754 I Hate It
- HDU1754 I Hate It
- HDU1754 I Hate It
- python 日期API
- [3-02]大型网站及其架构演进过程
- Validate Binary Search Tree
- 安装slowhttptest各种问题解决方案
- 微信js-sdk分享详解及demo实例
- hdu1754 I Hate It
- 数据加密---简单的int类型数据转化到一维数组中
- Python-文件输入和输出
- 傅立叶变换学习(一)初步认识傅立叶变换
- PHP函数stream_context_create()模拟POST/GET
- HDU-Kanade's sum-模拟
- 分享一款Android开源的仿IOS滚轮效果的控件
- HDU 6121 思维 + DFS
- nyoj 18 The Triangle