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

线段树专题(持续更新中...)

时间:2015-07-20 12:24:53      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

单点更新:最最基础的线段树,只更新叶子节点,然后把信息用PushUP(int r)这个函数更新上来

 

技术分享
 1 #include <iostream>
 2 #include<cstring>
 3 #include<stdio.h>
 4 using namespace std;
 5 #define M 50005
 6 int sum[M<<2];
 7 void pushup(int rt)
 8 {
 9     sum[rt] = sum[rt<<1] + sum[rt<<1|1];
10 }
11 void build(int l,int r,int rt)
12 {
13     if(l==r)
14     {
15         scanf("%d",&sum[rt]);
16         return ;
17     }
18     int m=(l+r)/2;
19     build(l,m,rt*2);
20     build(m+1,r,rt*2+1);
21     pushup(rt);
22 }
23 void update(int p,int add,int l,int r,int rt)
24 {
25     if(l==r)
26     {
27         sum[rt]+=add;
28         return ;
29     }
30     int m=(l+r)/2;
31     if(p<=m)
32     {
33         update(p,add,l,m,rt*2);
34     }
35     else
36         update(p,add,m+1,r,rt*2+1);
37     pushup(rt);
38 }
39 int query(int L,int R,int l,int r,int rt)
40 {
41     if(l>=L&&r<=R)
42     {
43         return sum[rt];
44     }
45     int m=(l+r)/2;
46     int ans=0;
47     if(L<=m)
48         ans+=query(L,R,l,m,rt*2);
49     if(R>m)
50         ans+=query(L,R,m+1,r,rt*2+1);
51     return ans;
52 }
53 int main()
54 {
55     int T,n,a,b;
56     scanf("%d",&T);
57     for(int i=1;i<=T;i++)
58     {
59         printf("Case %d:\n",i);
60         scanf("%d",&n);
61         build(1,n,1);
62         char op[10];
63         while(scanf("%s",op)&&op[0]!=E)
64         {
65             scanf("%d %d",&a,&b);
66             if(op[0]==Q)
67             {
68                 printf("%d\n",query(a,b,1,n,1));
69             }
70             else if(op[0]==A)
71             {
72                 update(a,b,1,n,1);
73             }
74             else
75                 update(a,-b,1,n,1);
76         }
77     }
78     return 0;
79 }
View Code

 

 

 

 hdu1754 I Hate It
线段树功能:update:单点替换 query:区间最值

 

技术分享
 1 #include <iostream>
 2 #include<cstring>
 3 #include<stdio.h>
 4 using namespace std;
 5 #define M 222222
 6 int sum[M<<2];
 7 int max(int a,int b)
 8 {
 9     return a>b?a:b;
10 }
11 void pushup(int rt)
12 {
13     sum[rt] =max(sum[rt<<1] ,sum[rt<<1|1]);
14 }
15 void build(int l,int r,int rt)
16 {
17     if(l==r)
18     {
19         scanf("%d",&sum[rt]);
20         return ;
21     }
22     int m=(l+r)/2;
23     build(l,m,rt*2);
24     build(m+1,r,rt*2+1);
25     pushup(rt);
26 }
27 void update(int p,int add,int l,int r,int rt)
28 {
29     if(l==r)
30     {
31         sum[rt]=add;
32         return ;
33     }
34     int m=(l+r)/2;
35     if(p<=m)
36     {
37         update(p,add,l,m,rt*2);
38     }
39     else
40         update(p,add,m+1,r,rt*2+1);
41     pushup(rt);
42 }
43 int query(int L,int R,int l,int r,int rt)
44 {
45     if(l>=L&&r<=R)
46     {
47         return sum[rt];
48     }
49     int m=(l+r)/2;
50     int ans=0;
51     if(L<=m)
52         ans=max(ans,query(L,R,l,m,rt*2));
53     if(R>m)
54         ans=max(ans,query(L,R,m+1,r,rt*2+1));
55     return ans;
56 }
57 int main()
58 {
59     int n,m,a,b;
60     while(scanf("%d%d",&n,&m)!=EOF)
61     {
62         build(1,n,1);
63         char op[2];
64         for(int i=0; i<m; i++)
65         {
66             scanf("%s",op);
67             scanf("%d%d",&a,&b);
68             if(op[0]==Q)
69                 printf("%d\n",query(a,b,1,n,1));
70             else
71                 update(a,b,1,n,1);
72         }
73     }
74     return 0;
75 }
View Code

 

线段树专题(持续更新中...)

标签:

原文地址:http://www.cnblogs.com/PJQOOO/p/4660854.html

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