hdu1754 单点更新
2015-08-04 15:25
253 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754
中文题 不解释题意
线段树 单点更新,维护区间的最大值
中文题 不解释题意
线段树 单点更新,维护区间的最大值
#include <set> #include <map> #include <queue> #include <stack> #include <deque> #include <math.h> #include <string> #include <vector> #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <functional> #define mem(a) memset(a,0,sizeof(a)); #define mem_1(a) memset(a,-1,sizeof(a)); #define sf(a) scanf("%d",&a) #define sff(a,b) scanf("%d%d",&a,&b) #define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c) #define LL long long #define lson l, mid, root<<1 #define rson mid+1, r, root<<1|1 const int INF = 0x7FFFFFFF; const int MAXN = 200100; const double PI = acos(-1.0); const double esp = 1e-10; using namespace std; struct node { int l,r,Max; }Tree[MAXN<<2]; void build_tree(int l,int r,int root) { Tree[root].l = l; Tree[root].r = r; if(Tree[root].l == Tree[root].r) { sf(Tree[root].Max); return ; } int mid = (l + r)>>1; build_tree(lson); build_tree(rson); Tree[root].Max = max(Tree[root << 1].Max , Tree[root << 1|1].Max); } void updata(int l,int r,int root,int k) { if(Tree[root].l == l && Tree[root].r == r) { Tree[root].Max = k; return ; } int mid = (Tree[root].l + Tree[root].r)>>1; if(r <= mid) updata(l,r,root<<1,k); else if(mid < l) updata(l,r,root<<1|1,k); else { updata(lson,k); updata(rson,k); } Tree[root].Max = max(Tree[root<<1].Max, Tree[root<<1|1].Max); } int Query(int l,int r,int root) { if(Tree[root].l == l && Tree[root].r == r) { return Tree[root].Max; } int ans = -1; int mid = (Tree[root].l + Tree[root].r) >> 1; if(r <= mid) ans = max(ans,Query(l,r,root<<1)); else if(mid < l) ans = max(ans,Query(l,r,root<<1|1)); else { ans = max(ans,Query(l,mid,root<<1)); ans = max(ans,Query(mid+1,r,root<<1|1)); } return ans; } int main() { int n,m,x,y; char c; while(~sff(n,m)) { build_tree(1,n,1); while(m--) { cin >> c; sff(x,y); if(c=='Q') { printf("%d\n",Query(x,y,1)); } else if(c=='U') { updata(x,x,1,y); } } } return 0; }
相关文章推荐
- leetcode Factorial Trailing Zeroes非负整数阶乘后尾0个数
- WordPress主题 Music Pro 高级音乐/电台自适应主题[更新至v1.0]
- android studio 工具
- 极光推送Jpush功能(具体参照官网说明文档,注意此文红色字体)
- python 多线程处理List
- dSploitzANTI渗透教程之修改MAC地址与Wifi监听器
- 开发的某条delete报错
- 9.3栈和队列(四)——堆盘子
- hdu1556 线段树更新区间
- ArcSDE10.2 for Oracle 11g 安装和创建企业数据库
- leetcode——Count Primes
- canvas 绘图基础
- unp.h及网络编程UNP所有代码的下载
- javascript运动框架多物体运动---1
- 常用的sql语句
- 献给北漂一族
- poj 2777 Count 线段树区间覆盖
- office2013 key
- Log4j
- Qt信号与槽机制(signal & slot)