hdu1754 I Hate It

题目链接:hdu1754 I Hate It

树状数组学习参考博客:http://blog.csdn.net/u010598215/article/details/48206959

树状数组之前没看懂就放着一直没看了,现在抽空学下...

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 #define CLR(a,b) memset((a),(b),sizeof((a)))
 6 using namespace std;
 7 typedef long long ll;
 8 const int N = 200001;
 9 int n;
10 int a[N];
11 int tree[N];
12 int lowbit(int x){
13     return x&(-x);
14 }
15 void update(int pos){
16     int lx, i;
17     while(pos <= n){
18         tree[pos] = a[pos];
19         lx = lowbit(pos);
20         for(i = 1; i < lx; i <<= 1)
21             tree[pos] = max(tree[pos], tree[pos-i]);
22         pos += lowbit(i);
23     }
24 }
25 int query_ma(int x, int y){
26     int ma = 0;
27     while(y >= x){
28         ma = max(ma, a[y]);
29         y--;
30         for(; y - lowbit(y) >= x; y -= lowbit(y))
31             ma = max(tree[y], ma);
32     }
33     return ma;
34 }
35 int main(){
36     int m, i;
37     while(~scanf("%d%d", &n, &m)){
38         CLR(tree, 0);
39         for(i = 1; i <= n; ++i){
40             scanf("%d", &a[i]);
41             update(i);
42         }
43         int l, r;
44         char c;
45         while(m--){
46             scanf("%*c%c%d%d", &c, &l, &r);
47             if(c == 'Q')
48                 printf("%d\n", query_ma(l,r));
49             else {
50                 a[l] = r;
51                 update(l);
52             }
53         }
54     }
55     return 0;
56 }
View Code

 

posted @ 2016-11-05 00:20  GraceSkyer  阅读(305)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……