HDU1754 I Hate It,线段树
2012-01-17 20:32
531 查看
线段树的简单题,整整1000+ms!!!看来要优化一下
/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2012-01-17 20:35 # Filename: HDU1754 I Hate It.cpp # Description : ******************************************************************************/ #ifdef _MSC_VER #define DEBUG #endif #include <fstream> #include <stdio.h> #include <iostream> #include <string.h> #include <string> #include <limits.h> #include <algorithm> #include <math.h> #include <numeric> #include <functional> #include <ctype.h> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MAX 200010 using namespace std; struct NODE_T { int l,r,score; }node[MAX*4]; void init() { memset(node,'\0',sizeof(node)); } void build(const int &t,const int &l,const int &r) { node[t].l = l; node[t].r = r; node[t].score = -1; if(l==r-1) return; int mid=(l+r)>>1; build(L(t),l,mid); build(R(t),mid,r); } void update(const int &t,const int &l,const int &r,const int score) { if(node[t].l==l && node[t].r==r) { node[t].score = score; return; } int mid = (node[t].l+node[t].r)>>1; if( l>=mid) update(R(t),l,r,score); else if(mid>=r) update(L(t),l,r,score); node[t].score = max(node[L(t)].score,node[R(t)].score); } int query(const int &t,const int &l,const int &r) { if(node[t].l>=l && node[t].r<=r) return node[t].score; int mid=(node[t].l+node[t].r)>>1; if(l>=mid) return query(R(t),l,r); else if(r<=mid) return query(L(t),l,r); else { int a = query(L(t),l,mid); int b = query(R(t),mid,r); return max(a,b); } } int main(void) { #ifdef DEBUG freopen("../stdin.txt","r",stdin); freopen("../stdout.txt","w",stdout); #endif int n,m; int x,y; char ch; while(~scanf("%d%d",&n,&m)) { init(); build(1,0,n+1); for(int i=1;i<=n;++i) { scanf("%d",&x); update(1,i,i+1,x); } while(m--) { getchar(); scanf("%c %d %d",&ch,&x,&y); if(ch=='Q') printf("%d\n",query(1,x,y+1)); else update(1,x,x+1,y); } } return 0; }
相关文章推荐
- hdu 1754 I Hate It (模板线段树)
- HDU 1754 B I Hate It 线段树 单点更新 区间最大值 模板
- 线段树单点更新 - HDU 1166 敌兵布阵 1754 I Hate it
- HDU-1754-I hate it (线段树RMQ)
- hdu 1754 I Hate It 线段树单点更新区间查询
- [ACM] hdu 1754 I Hate It (线段树,单点更新)
- hdu1754——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 (线段树--求区间最大值)(基础)
- HDU1754-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 B I Hate It 线段树 单点更新 区间最大值 模板
- HDU 1754 I Hate It 线段树
- 线段树系列-hdu-1754-I Hate It-单点修改求区间最值