hdu-1754 I Hate It 线段树
来源:互联网 发布:图像语义分割 知乎 编辑:程序博客网 时间:2024/04/29 04:53
http://acm.hdu.edu.cn/showproblem.php?pid=1754
求区间最大数,并且有单点操作
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;#define MAX 200010typedef struct{ int l; int r; int max;} Tree;Tree tree[MAX*4];int build(int i,int l,int r) //建树{ int mid; tree[i].l=l; tree[i].r=r; tree[i].max=0; if(l==r) { return 0; } mid=(l+r)/2; build(i*2,l,mid); build(i*2+1,mid+1,r);}int insert(int i,int k,int v) //插入{ tree[i].max=v >tree[i].max?v:tree[i].max; if(tree[i].l==tree[i].r) return 0; else { int mid=(tree[i].l+tree[i].r)/2; if(mid<k) insert(i*2+1,k,v); else insert(i*2,k,v); }}int del(int i,int k,int v) //先删除{ if(tree[i].l==tree[i].r) { tree[i].max=v; return i; } else { int mid=(tree[i].l+tree[i].r)/2; if(mid<k) del(i*2+1,k,v); else del(i*2,k,v); }}void change(int i) //再倒着更新{ if(i==1) return ; if(i%2==1) { tree[(i-1)/2].max=max(tree[i-1].max,tree[i].max); change((i-1)/2); } else { tree[i/2].max=max(tree[i].max,tree[i+1].max); change(i/2); }}int findmax(int i,int x,int y) //找区间的最大{ int ans=0,mid; if((x<=tree[i].l)&&(y>=tree[i].r)) return tree[i].max; ans=0; mid=(tree[i].l+tree[i].r)/2; if (x<=mid) ans= max(ans,findmax(i*2,x,y)); if (y>mid) ans= max(ans,findmax(i*2+1,x,y)); return ans;}int N,M;int main(){ while(~scanf("%d%d",&N,&M)) { build(1,1,N); int a; char Q[3]; for(int i=1; i<=N; i++) { scanf("%d",&a); insert(1,i,a); } for(int i=1; i<=M; i++) { scanf("%s",Q); if(Q[0]=='Q') { int x,y; scanf("%d%d",&x,&y); printf("%d\n",findmax(1,x,y)); } else { int i,v; scanf("%d%d",&i,&v); int t=del(1,i,v); change(t); } } }}
- 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 线段树
- SIFT/SURF算法的深入剖析——谈SIFT的精妙与不足
- Python之美[从菜鸟到高手]--装饰器之使用情景分析
- 【字符串操作】08.将单词的首字母大写
- GO + ichartjs + jsonP实现数据图表分析后台
- 飘逸的python - 赛程表算法
- hdu-1754 I Hate It 线段树
- 快速旋转匹配、缺陷检测、目标定位、模板配准算法接口
- Linux MySql+Php详细配制
- Integrating jQuery UI and ASP.NET MVC
- SIFT算法详解
- poj 2756 Autumn is a Genius(高精度加法)
- 聚类算法初探(六)OPTICS
- ARM体系学习一
- 音频数字低通滤波与带进位除法优化