简单线段树(模板)

A - I Hate It

 HDU - 1754 

模板,单点更新,区间查询

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 #define lson l,m,rt<<1
13 #define rson m+1,r,rt<<1|1
14 using namespace std;
15 const double Pi=3.14159265358979323846;
16 typedef long long ll;
17 const int dx[5]={0,0,0,1,-1};
18 const int dy[5]={1,-1,0,0,0};
19 const int INF = 0x3f3f3f3f;
20 const int NINF = 0xc0c0c0c0;
21 const int MAXN=200000+5;
22 const ll mod=1e9+7;
23 int tree[MAXN<<2];
24 //向上更新 
25 void PushUp(int rt){
26     tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
27 }
28 //建树 
29 void build(int l,int r,int rt){
30     if(l==r){
31         scanf("%d",&tree[rt]);
32         return;
33     }
34     int m=(l+r)>>1;
35     build(lson);build(rson);
36     PushUp(rt);
37 } 
38 //单点更新
39 void update(int p,int add,int l,int r,int rt){
40     if(l==r){
41         tree[rt]=add;
42         return;
43     }
44     int m=(l+r)>>1;
45     if(p<=m) update(p,add,lson);
46     else update(p,add,rson);
47     PushUp(rt);
48 } 
49 //区间查询
50 int query(int L,int R,int l,int r,int rt){
51     if(L<=l&&r<=R){
52         return tree[rt];
53     }
54     int m=(l+r)>>1;
55     int ret=NINF;
56     if(L<=m) ret=max(ret,query(L,R,lson));
57     if(R>m) ret=max(ret,query(L,R,rson));
58     return ret;
59 } 
60 
61 int main()
62 {
63     int n,m;
64     while(scanf("%d%d",&n,&m)!=EOF)
65     {
66         build(1,n,1);
67         while(m--)
68         {
69             char c;int x,y;cin>>c;scanf("%d%d",&x,&y);
70             if(c=='Q') 
71                 cout <<query(x,y,1,n,1)<<endl;
72             else update(x,y,1,n,1);
73         }
74     }
75     return 0;
76 }
View Code

B - A Simple Problem with Integers

 POJ - 3468 

这个题呀,错了个符号,调了半天。区间修改加区间查询

  1 #include <iostream>
  2 #include <cstring>
  3 #include <string>
  4 #include <map>
  5 #include <set>
  6 #include <algorithm>
  7 #include <fstream>
  8 #include <cstdio>
  9 #include <cmath>
 10 #include <stack>
 11 #include <queue>
 12 #define lson l,m,rt<<1
 13 #define rson m+1,r,rt<<1|1
 14 using namespace std;
 15 const double Pi=3.14159265358979323846;
 16 typedef long long ll;
 17 const int dx[5]={0,0,0,1,-1};
 18 const int dy[5]={1,-1,0,0,0};
 19 const int INF = 0x3f3f3f3f;
 20 const int NINF = 0xc0c0c0c0;
 21 const int MAXN=100000+5;
 22 const ll mod=1e9+7;
 23 
 24 ll tree[MAXN<<2];
 25 ll add[MAXN<<2]; 
 26 //向上更新 
 27 void PushUp(int rt){
 28     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
 29 }
 30 //向下更新
 31 void PushDown(int rt,int m){
 32     if(add[rt]){
 33         add[rt<<1]+=add[rt];
 34         add[rt<<1|1]+=add[rt];
 35         tree[rt<<1]+=add[rt]*(m-(m>>1));
 36         tree[rt<<1|1]+=add[rt]*(m>>1);
 37         add[rt]=0;
 38     }
 39 }
 40 //建树 
 41 void build(int l,int r,int rt){
 42     add[rt]=0;
 43     if(l==r){
 44         scanf("%lld",&tree[rt]);
 45         return;
 46     }
 47     int m=(l+r)>>1;
 48     build(lson);build(rson);
 49     PushUp(rt);
 50 } 
 51 //单点更新
 52 void updatePoint(int p,int add,int l,int r,int rt){
 53     if(l==r){
 54         tree[rt]=add;
 55         return;
 56     }
 57     PushDown(rt,r-l+1);
 58     int m=(l+r)>>1;
 59     if(p<=m) updatePoint(p,add,lson);
 60     else updatePoint(p,add,rson);
 61     PushUp(rt);
 62 } 
 63 //区间更新
 64 void updateQuery(int L,int R,int c,int l,int r,int rt){
 65     if(L<=l&&r<=R){
 66         add[rt]+=c;
 67         tree[rt]+=(ll)c*(r-l+1);
 68         return;
 69     }
 70     PushDown(rt,r-l+1);
 71     int m=(l+r)>>1;
 72     if(L<=m) updateQuery(L,R,c,lson);
 73     if(R>m) updateQuery(L,R,c,rson);
 74     PushUp(rt);
 75 } 
 76 //区间查询
 77 ll query(int L,int R,int l,int r,int rt){
 78     if(L<=l&&r<=R){
 79         return tree[rt];
 80     }
 81     PushDown(rt,r-l+1);
 82     int m=(l+r)>>1;
 83     ll ret=0;
 84     if(L<=m) ret+=query(L,R,lson);
 85     if(R>m) ret+=query(L,R,rson);
 86     return ret;
 87 }  
 88 int main()
 89 {
 90     int n,m;int cnt=0;
 91     //freopen("a.txt","r",stdin);
 92     cin>>n>>m;
 93         build(1,n,1);
 94         while(m--)
 95         {
 96             char c;ll x,y,z;cin>>c;
 97             if(c=='Q') 
 98             {
 99                 cnt++;
100                 scanf("%lld%lld",&x,&y);
101                 cout <<query(x,y,1,n,1)<<endl;
102             }else
103             {    cnt++;
104                 scanf("%lld%lld%lld",&x,&y,&z);
105                 updateQuery(x,y,z,1,n,1);
106             }
107         }
108     return 0;
109 }
View Code

 

posted @ 2019-05-13 22:35  Chuhanjing  阅读(176)  评论(0编辑  收藏  举报