1. 程式人生 > >HDU 1754 I HATE IT

HDU 1754 I HATE IT

namespace ostream fin ace roo bsp scan fine set

線段樹板子。。無lazy。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 #define mem(a,b) memset(a,b,sizeof(a))
10 #define ll long long
11
#define inf 1000000000 12 #define maxn 40000 13 #define eps 1e-12 14 #define mod 1000000007 15 #define N 3005 16 inline int read() 17 { 18 int x=0,f=1;char ch=getchar(); 19 while(ch<0||ch>9) {if(ch==-) f=-1;ch=getchar();} 20 while(ch>=0&&ch<=9) {x=10*x+ch-0;ch=getchar();} 21
return x*f; 22 } 23 int tree[800005]; 24 void build(int left,int right,int root) 25 { 26 if(left==right) tree[root]=read(); 27 else{ 28 int mid=(left+right)>>1; 29 build(left,mid,root<<1); 30 build(mid+1,right,root<<1|1); 31 tree[root]=max(tree[root<<1
],tree[root<<1|1]); 32 } 33 } 34 void update(int p,int add,int left,int right,int root) 35 { 36 if(left==right) tree[root]=add; 37 else{ 38 int mid=(left+right)>>1; 39 if(p<=mid) update(p,add,left,mid,root<<1); 40 else update(p,add,mid+1,right,root<<1|1); 41 tree[root]=max(tree[root<<1],tree[root<<1|1]); 42 } 43 } 44 int query(int x,int y,int left,int right,int root) 45 { 46 if(x<=left&&right<=y) return tree[root]; 47 else{ 48 int sum=0,mid=(left+right)>>1; 49 if(x<=mid) sum=max(sum,query(x,y,left,mid,root<<1)); 50 if(y>mid) sum=max(sum,query(x,y,mid+1,right,root<<1|1)); 51 return sum; 52 } 53 } 54 int main() 55 { 56 int n,m; 57 while(~scanf("%d%d",&n,&m)) 58 { 59 build(1,n,1); 60 while(m--) 61 { 62 char op[3]; int x,y; 63 scanf("%s%d%d",op,&x,&y); 64 if(op[0]==U) update(x,y,1,n,1); 65 else printf("%d\n",query(x,y,1,n,1)); 66 } 67 } 68 return 0; 69 }

HDU 1754 I HATE IT