hdu1754

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754

以前讲过一道相似的题,这里就直接扔代码了

那道题是 hdu1166

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1166

博客的传送门:http://www.cnblogs.com/Donnie-Darko/p/4762563.html

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=50000+10;
 7 int n,m,i,j,a[maxn],maxt[maxn*4];
 8 char s[20];
 9 int max(int a,int b) {return a>b?a:b;}
10 int min(int a,int b) {return a<b?a:b;}
11 
12 void build(int node,int s,int t)
13 {
14      if (s==t){maxt[node]=a[s]; return;}
15      int mid=(s+t)/2,lson=node*2,rson=lson+1;
16      build(lson,s,mid);
17      build(rson,mid+1,t);
18      maxt[node]=max(maxt[lson],maxt[rson]);
19 }
20      
21 void update(int node,int s,int t,int id,int add)
22 {
23      if (s==t){maxt[node]+=add; return;}
24      int mid=(s+t)/2,lson=node*2,rson=lson+1;
25      if (id<=mid) update(lson,s,mid,id,add);
26              else update(rson,mid+1,t,id,add);
27      maxt[node]=max(maxt[lson],maxt[rson]);
28 }
29 
30 int query(int node,int s,int t,int L,int R)
31 {
32     if (t<L||R<s) return -1;
33     if (L<=s&&t<=R) return maxt[node];
34     int mid=(s+t)/2,lson=node*2,rson=lson+1;
35     int ansl,ansr;
36     ansl=query(lson,s,mid,L,R);
37     ansr=query(rson,mid+1,t,L,R);
38     return max(ansl,ansr);
39 }
40 
41 int main()
42 {
43     while (scanf("%d%d",&n,&m)==2)
44     {
45           for (i=1;i<=n;i++) scanf("%d",&a[i]);
46           build(1,1,n);
47           while (m)
48           {
49                 m--;
50                 scanf("%s%d%d",s,&i,&j);
51                 if (s[0]=='U') update(1,1,n,i,j-a[i]),a[i]=j;
52                 if (s[0]=='Q') printf("%d\n",query(1,1,n,i,j));
53           } 
54     }
55 }

 

posted @ 2015-08-30 16:04  Donnie_Darko  阅读(190)  评论(0编辑  收藏  举报