1166与1754相似(线段树)

1166讲的是对一段区间求和,而1754是对一段区间求最值。套用经典线段树模版

1166代码:

 

View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct node
 5 {
 6     int a,b,sum;
 7 };
 8 node re[140000];
 9 int s,p[50001];
10 void make(int x,int y,int z)
11 {
12     re[z].a=x;
13     re[z].b=y;
14     if(x==y)
15     {
16         re[z].sum=p[y];
17         return ;
18     }
19     make(x,(x+y)/2,z+z);
20     make((x+y)/2+1,y,z+z+1);
21     re[z].sum=re[z+z].sum+re[z+z+1].sum;
22 }
23 void que(int x,int y,int z)
24 {
25     if(x<=re[z].a&&re[z].b<=y)
26         s+=re[z].sum;
27     else
28     {
29         if(x>(re[z].a+re[z].b)/2)
30             que(x,y,z+z+1);
31         else if(y<=(re[z].a+re[z].b)/2)
32             que(x,y,z+z);
33         else
34         {
35             que(x,y,z+z);
36             que(x,y,z+z+1);        
37         }
38     }
39 }
40 void add(int x,int y,int z)
41 {
42     re[z].sum+=y;
43     if(re[z].a==x&&re[z].b==x)
44         return;
45     if(x>(re[z].a+re[z].b)/2)
46         add(x,y,z+z+1);
47     else
48         add(x,y,z+z);
49 }
50 void sub(int x,int y,int z)
51 {
52     re[z].sum-=y;
53     if(re[z].a==x&&re[z].b==x)
54         return;
55     if(x>(re[z].a+re[z].b)/2)
56         sub(x,y,z+z+1);
57     else
58         sub(x,y,z+z);
59 }
60 int main()
61 {
62     int t,n;
63     int i,j,x,y;
64     char c[10];
65     cin>>t;
66     for(j=1;j<=t;j++)
67     {
68         cout<<"Case "<<j<<':'<<endl;
69         cin>>n;
70         for(i=1;i<=n;i++)
71             cin>>p[i];
72         make(1,n,1);
73         while(cin>>c)
74         {
75             if(strcmp(c,"End")==0)
76                 break;
77             cin>>x>>y;
78             if(strcmp(c,"Add")==0)
79             {
80                 add(x,y,1);
81             }
82             if(strcmp(c,"Sub")==0)
83             {
84                 sub(x,y,1);
85             }
86             if(strcmp(c,"Query")==0)
87             {
88                 s=0;
89                 que(x,y,1);
90                 cout<<s<<endl;
91             }
92         }
93     }
94     return 0;
95 }

 

 

1754代码:(建议用c的输入输出 与 c++相比差了三倍的时间)

 

View Code
 1 #include<iostream>
 2 #include<stdlib.h>
 3 #include<algorithm>
 4 #define nsize 200001 
 5 using namespace std;
 6 
 7 int s[nsize];
 8 struct node
 9 {
10     int a,b,ma;
11 };
12 node re[nsize*3];
13 inline void make(int x,int y,int z)
14 {
15     re[z].a=x;
16     re[z].b=y;
17     if(x==y)
18         re[z].ma=s[x];
19     else 
20     {
21         make(x,(x+y)/2,z+z);
22         make((x+y)/2+1,y,z+z+1);
23         re[z].ma=max(re[z+z].ma,re[z+z+1].ma);
24     }
25 }
26 inline void updata(int x,int y,int z)
27 {
28     if(re[z].a==re[z].b)
29         re[z].ma=y;
30     else
31     {
32         if(x<=(re[z].a+re[z].b)/2)
33             updata(x,y,z+z);
34         else
35             updata(x,y,z+z+1);
36         re[z].ma=max(re[z+z].ma,re[z+z+1].ma);
37     }
38 }
39 inline int que(int x,int y,int z)
40 {
41     if(re[z].a==x&&re[z].b==y)
42     {
43         return re[z].ma;
44     }
45     else
46     {
47         if(x>(re[z].a+re[z].b)/2)
48          return   que(x,y,z+z+1);
49         if(y<=(re[z].a+re[z].b)/2)
50           return  que(x,y,z+z);
51         else
52         {
53          return   max(que(x,(re[z].a+re[z].b)/2,z+z),que((re[z].a+re[z].b)/2+1,y,z+z+1));
54         }
55     }
56 }
57 int main()
58 {
59     int n,m;
60     int i,j,x,y;
61     char c;
62     while(~scanf("%d%d",&n,&m))
63     {
64         
65         for(i=1;i<=n;i++)
66         {
67             scanf("%d",&s[i]);
68         }   
69         make(1,n,1);
70         for(j=1;j<=m;j++)
71         {   
72             scanf("%s %d%d",&c,&x,&y);
73             if(c=='Q')
74             {
75                 int zq=que(x,y,1);
76                 printf("%d\n",zq);
77             }    
78             else
79                 updata(x,y,1);
80         }
81     }
82     return 0;
83 }

 

posted on 2012-10-07 17:16  xinmenghuairi  阅读(344)  评论(0编辑  收藏  举报