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

线段树模板

时间:2015-08-12 21:17:39      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 using namespace std;
  5 #define LL(x) (x<<1)
  6 #define RR(x) (x<<1|1)
  7 #define MID(a,b) (a+((b-a)>>2))
  8 #define INF (1<<30)
  9 const int N=200005;
 10 struct node
 11 {
 12     int lft,rht,mx,mi,sum;
 13     int mid()
 14     {
 15         return MID(lft,rht);
 16     }
 17 };
 18 int y[N],n,m;
 19 struct Segtree
 20 {
 21     node tree[N*4];
 22     void build(int lft,int rht,int ind )
 23     {
 24         tree[ind].lft=lft;
 25         tree[ind].rht=rht;
 26         tree[ind].mx=-INF;
 27         tree[ind].mi=INF;
 28         tree[ind].sum=0;
 29         if(lft==rht)
 30         {
 31             tree[ind].mi=y[lft];
 32             tree[ind].mx=y[lft];
 33             tree[ind].sum=y[lft];
 34         }
 35         else
 36         {
 37             int mid=tree[ind].mid();
 38             build(lft,mid,LL(ind));
 39             build(mid+1,rht,RR(ind));
 40             tree[ind].mx=max(tree[LL(ind)].mx,tree[RR(ind)].mx);
 41             tree[ind].mi=min(tree[LL(ind)].mi,tree[RR(ind)].mi);
 42             tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum;
 43         }
 44 
 45 
 46     }
 47     void updata(int pos,int ind,int valu)
 48     {
 49         int lft=tree[ind].lft,rht=tree[ind].rht;
 50         if(lft==rht)
 51         {
 52             tree[ind].mx=valu;
 53             tree[ind].sum=valu;
 54             tree[ind].mi=valu;
 55         }
 56         else
 57         {
 58             int mid=tree[ind].mid();
 59             if(pos<=mid)updata(pos,LL(ind),valu);
 60             else updata(pos,RR(ind),valu);
 61             tree[ind].mx=max(tree[LL(ind)].mx,tree[RR(ind)].mx);
 62             tree[ind].mi=min(tree[LL(ind)].mi,tree[RR(ind)].mi);
 63             tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum;
 64 
 65 
 66         }
 67     }
 68     int querymx(int st,int ed,int ind)
 69     {
 70         int lft=tree[ind].lft,rht=tree[ind].rht;
 71         if(st<=lft&&rht<=ed)return tree[ind].mx;
 72         else
 73         {
 74             int mid=tree[ind].mid();
 75             int mx1=-INF,mx2=-INF;
 76             if(st<=mid)mx1=querymx(st,ed,LL(ind));
 77             if(ed>mid)mx2=querymx(st,ed,RR(ind));
 78             return max(mx1,mx2);
 79         }
 80 
 81 
 82     }
 83     int querymi(int st,int ed,int ind)
 84     {
 85         int lft=tree[ind].lft,rht=tree[ind].rht;
 86         if(st<=lft&&rht<=ed)return tree[ind].mi;
 87         else
 88         {
 89             int mid=tree[ind].mid();
 90             int mi1=INF,mi2=INF;
 91             if(st<=mid)mi1=querymi(st,ed,LL(ind));
 92             if(ed>mid)mi2=querymi(st,ed,RR(ind));
 93             return min(mi1,mi2);
 94         }
 95 
 96 
 97     }
 98     int querysum(int st,int ed,int ind)
 99     {
100         int lft=tree[ind].lft,rht=tree[ind].rht;
101         if(st<=lft&&rht<=ed)return tree[ind].sum;
102         else
103         {
104             int mid=tree[ind].mid();
105             int sum1=0,sum2=0;
106             if(st<=mid)sum1=querysum(st,ed,LL(ind));
107             if(ed>mid)sum2=querysum(st,ed,RR(ind));
108             return sum1+sum2;
109         }
110 
111 
112     }
113 
114 
115 } seg;
116 
117 
118 int main()
119 {
120 
121 
122     int n,m;
123     while(cin>>n>>m)
124     {
125         for(int i=1; i<=n; i++)
126             cin>>y[i];
127         seg.build(1,n,1);
128         char s;
129         int st,ed;
130         for(int i=0; i<m; i++)
131         {
132             cin>>s>>st>>ed;
133 
134 
135             if(s==S)cout<<seg.querysum(st,ed,1)<<endl;
136             if(s==A)cout<<seg.querymx(st,ed,1)<<endl;
137             if(s==I)cout<<seg.querymi(st,ed,1)<<endl;
138 
139 
140         }
141 
142 
143     }
144 
145 
146     return 0;
147 }

 

线段树模板

标签:

原文地址:http://www.cnblogs.com/lengxia/p/4725416.html

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