hdu1754(线段数维护区间最大值)
来源:互联网 发布:恢复系统数据 编辑:程序博客网 时间:2024/05/17 01:00
题意:给定1-n(n<=200000)个数,然后动态改变一些值,并动态询问区间最大值。
解法:裸的线段树,赛前默写模版回忆下线段树代码。仍然要注意:线段树节点数组一定要开到节点数的三倍长度。
代码:
/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8const double pi=acos(-1.0);typedef long long LL;const int Max=300000*3;const int INF=1000000007;struct node{ node* pleft,*pright; int l,r; int ma;} nodes[Max];int num[Max];int tot=0;void buildtree(node* p,int left,int right){ p->l=left; p->r=right; if(left==right) return; int mid=(left+right)/2; tot++; p->pleft=nodes+tot; buildtree(p->pleft,left,mid); tot++; p->pright=nodes+tot; buildtree(p->pright,mid+1,right);}int n,m;int update(node* p,int add,int value){ if(p->l==p->r) { p->ma=value; return value; } int mid=(p->l+p->r)/2; if(add<=mid) update(p->pleft,add,value); else update(p->pright,add,value); return p->ma=max(p->pleft->ma,p->pright->ma);}int query(node* p,int left,int right){ if(p->l==left&&p->r==right) return p->ma; int mid=(p->l+p->r)/2; if(right<=mid) return query(p->pleft,left,right); if(left>=mid+1) return query(p->pright,left,right); return max(query(p->pleft,left,mid),query(p->pright,mid+1,right));}int main(){ while(scanf("%d%d",&n,&m)==2) { tot=0; buildtree(nodes,1,n+1); for(int i=1; i<=n; i++) { scanf("%d",num+i); update(nodes,i,num[i]); } for(int i=0;i<m;i++) { char c;cin>>c; if(c=='Q') { int l,r;scanf("%d%d",&l,&r); printf("%d\n",query(nodes,l,r)); } else { int l,r;scanf("%d%d",&l,&r); update(nodes,l,r); } } } return 0;}
0 0
- hdu1754(线段数维护区间最大值)
- hdu1754线段树维护区间最大值
- 线段树维护区间最大值hdu1754
- hdu1754-线段树(求区间最大值)
- hdu1754 初探线段树之区间最大值、
- HDU1754线段树求取区间最大值
- hdu1754我讨厌他(线段树,区间最大值)
- 线段树入门单节点更新区间求最大值hdu1754
- HDU1754 Splay 区间维护
- spojGSS1 线段树维护区间和最大值
- HDU1754 I Hate It 线段树 区间更新 区间查找 最大值
- HDU1754 I Hate It(线段树单点更新,区间最大值,zkw线段树)
- 线段树区间修改 懒惰标记 维护和、最大值、最小值
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
- 51nod 1376【线段树维护区间最大值】
- hdu1754 I Hate It (线段树,求区间内的最大值)
- HDU1754 I hate it 线段树 最大值
- 树状数组维护区间最大值
- 创新实验室实习生每周工作总结【实习第七周】
- 获取 iOS crash log
- Ubuntu vim 配置以及快捷键使用
- Android -----基于回调机制的事件处理
- MC药水合成
- hdu1754(线段数维护区间最大值)
- eclipse断点调试
- JS 之document常用方法属性事件
- Debug Assertion Failed filecore.cpp line258
- 在spring security手动 自定义 用户认证 SecurityContextHolder
- 黑马程序员Java基础(1)
- 浮点数据(float/double)在计算机上的存储方式
- 无插件Vim编程技巧
- MinGW与Cygwin