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

简单线段树(模板)

时间:2019-05-13 22:49:29      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:char   net   push   include   get   http   查询   play   string   

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

 

简单线段树(模板)

标签:char   net   push   include   get   http   查询   play   string   

原文地址:https://www.cnblogs.com/Msmw/p/10859489.html

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