码迷,mamicode.com
首页 > 其他好文 > 详细

线段树(待续)

时间:2016-07-23 19:43:47      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

推荐博客:http://blog.csdn.net/metalseed/article/details/8039326

接下来是,对应的线段树的题目解析。

hdu 1166 排兵布阵(构造线段树,单点更新,查询区间和)

 1 #include <cstdio>
 2 #include <cstring>
 3 typedef long long LL;
 4 LL sum[50000 << 2];
 5 #define lson l,m,rt << 1
 6 #define rson m+1,r,rt << 1 | 1
 7 void pushup(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}
 8 void build(int l,int r,int rt)
 9 {
10     if(l == r) {scanf("%d",&sum[rt]); return ;}
11     int m = (l + r) >> 1;
12     build(lson);
13     build(rson);
14     pushup(rt);
15 }
16 void update(int p,int add,int l,int r,int rt)
17 {
18     if(l == r) {sum[rt] += add;return ;}
19     int m = (l + r) >> 1;
20     if(p <= m)
21         update(p,add,l,m,rt << 1);
22     else
23         update(p,add,m+1,r,rt << 1 | 1);
24     pushup(rt);
25 }
26 LL query_sum(int ll,int rr,int l,int r,int rt)
27 {
28     if(ll <= l && rr >= r) return sum[rt];
29     LL tot = 0;
30     int m = (l + r) >> 1;
31     if(ll <= m)
32         tot += query_sum(ll,rr,l,m,rt << 1);
33     if(rr > m)
34         tot += query_sum(ll,rr,m+1,r,rt << 1 | 1);
35     return tot;
36 }
37 int main()
38 {
39     int T,n,x,y,p = 1;
40     char s[12];
41     scanf("%d",&T);
42     while(T--)
43     {
44         printf("Case %d:\n",p++);
45         scanf("%d",&n);
46         build(1,n,1);
47         while(~scanf("%s",s))
48         {
49             if(!strcmp(s,"End")) break;
50             scanf("%d%d",&x,&y);
51             if(!strcmp(s,"Query"))  {LL ans = query_sum(x,y,1,n,1);printf("%d\n",ans);}
52             if(!strcmp(s,"Add"))    update(x,y,1,n,1);
53             if(!strcmp(s,"Sub"))    update(x,-y,1,n,1);
54         }
55     }
56     return 0;
57 }

 

hdu 1754 I hate it(构造线段树,区间最大值查询,单点更新)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 LL MAX[200000 << 2];
 7 #define lson l,m,rt << 1
 8 #define rson m+1,r,rt << 1 | 1
 9 void pushup(int rt){MAX[rt] = max(MAX[rt << 1] , MAX[rt << 1 | 1]);}
10 void build(int l,int r,int rt)
11 {
12     if(l == r) {scanf("%d",&MAX[rt]); return ;}
13     int m = (l + r) >> 1;
14     build(lson);
15     build(rson);
16     pushup(rt);
17 }
18 void update(int p,int add,int l,int r,int rt)
19 {
20     if(l == r) {MAX[rt] = add;return ;}
21     int m = (l + r) >> 1;
22     if(p <= m)
23         update(p,add,l,m,rt << 1);
24     else
25         update(p,add,m+1,r,rt << 1 | 1);
26     pushup(rt);
27 }
28 LL query_max(int ll,int rr,int l,int r,int rt)
29 {
30     if(ll <= l && rr >= r) return MAX[rt];
31     LL ans = 0;
32     int m = (l + r) >> 1;
33     if(ll <= m)
34         ans = max(ans,query_max(ll,rr,l,m,rt << 1));
35     if(rr > m)
36         ans = max(ans,query_max(ll,rr,m+1,r,rt << 1 | 1));
37     return ans;
38 }
39 int main()
40 {
41     int n,m,x,y;
42     char ch;
43     while(~scanf("%d%d",&n,&m))
44     {
45         build(1,n,1);
46         for(int i=0;i<m;i++)
47         {
48             getchar();
49             scanf("%c%d%d",&ch,&x,&y);
50             if(ch == Q)
51             {
52                 int ans = query_max(x,y,1,n,1);
53                 printf("%d\n",ans);
54             }
55             else
56                 update(x,y,1,n,1);
57         }
58     }
59     return 0;
60 }

 

线段树(待续)

标签:

原文地址:http://www.cnblogs.com/luosuo10/p/5699231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!