HDU 1754 I Hate It 基础线段树
来源:互联网 发布:淘宝卖家推广方法 编辑:程序博客网 时间:2024/06/02 05:47
用区间值m表示这段区间的最大值,一直更新这个区间的最大值,很基础的线段树
#include<iostream>#include<stdio.h>using namespace std;#define N 200005struct node{ int l,r,m;}tree[N*4];int a[N];void build(int left,int right,int i){ tree[i].l=left; tree[i].r=right; if(tree[i].l==tree[i].r){ tree[i].m=a[tree[i].l]; return ; } int mid=(tree[i].l+tree[i].r)>>1; build(left,mid,i*2); build(mid+1,right,i*2+1); tree[i].m=max(tree[i*2].m,tree[i*2+1].m);}void updata(int left,int right,int i,int val){ // cout<<tree[i].l<<" "<<tree[i].r<<endl; if(left==tree[i].l&&right==tree[i].r){ tree[i].m=val; return ; } if(tree[i].l>left || tree[i].r<left) return ; int mid=(tree[i].r+tree[i].l)>>1; updata(left,right,i*2,val); updata(left,right,i*2+1,val); tree[i].m=max(tree[i*2].m,tree[i*2+1].m);}int ans=0;void query(int left,int right,int i){ // cout<<"Q 4 5 "<<left<<" "<<right<<" "<<tree[3].m<<endl; if(tree[i].l==left&&tree[i].r==right){ if(tree[i].m>ans) ans=tree[i].m; return ; } int mid=(tree[i].l+tree[i].r)>>1; if(mid>=right) query(left,right,i*2); else if(mid<left) query(left,right,i*2+1); else{ query(left,mid,i*2); query(mid+1,right,i*2+1); }}int main(){// freopen("in.txt","r",stdin); int m,n,x,y; char t[5]; while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,n,1); while(m--){ scanf("%s %d%d",t,&x,&y);// cout<<t<<" "<<x<<" "<<y<<endl; if(t[0]=='Q'){ ans=0; query(x,y,1); printf("%d\n",ans); } else{ updata(x,x,1,y); } } }}
0 0
- hdu 1754 I Hate It 线段树基础题
- HDU 1754 I Hate It 基础线段树
- HDU 1754 I Hate It(线段树基础)
- HDU 1754-I Hate It【基础线段树】
- hdu 1754 I Hate It 线段树基础题
- HDU 1754 I Hate It(基础线段树~)
- HDU 1754 I Hate It 线段树
- [hdu] 1754 I hate it -- 线段树
- hdu 1754 -- I Hate It (线段树)
- HDU 1754 I Hate It 线段树
- 线段树 hdu 1754 I Hate It
- HDU 1754 I Hate It(线段树)
- hdu 1754 (线段树)I Hate It
- hdu-1754 I Hate It 线段树
- HDU--1754 -- I Hate It [线段树]
- 【线段树】hdu 1754 I hate it
- hdu - 1754 I Hate It(线段树)
- HDU--1754--I Hate It--线段树
- 【数据结构和算法分析】循环链表及约瑟夫问题
- Linux系统编程(21)——信号的产生
- 重载一元运算符
- poj3723 Conscription(最大树问题)
- 中水处理设备:中水回用设备的供水标准及适用场所简析
- HDU 1754 I Hate It 基础线段树
- DSP28335中RAM空间不够的解决方法
- const 指针
- _c_int00 DSP
- K - Cube Stacking(并查集)
- 利用R文件根据按钮id来识别点击某一个按钮时的触摸响应
- OpenGL 编程指南笔记——第8章 OpenGL 绘制像素、位图、字体和图像
- C++ primer(第五版) 练习 3.39 个人code
- Ubuntu环境添加删除启动服务的命令