[kuangbin带你飞]专题七 线段树
Posted wangzhebufangqi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[kuangbin带你飞]专题七 线段树相关的知识,希望对你有一定的参考价值。
https://vjudge.net/contest/66989
A、敌兵布阵
单点修改、区间查询、维护区间和
1 //CSDN:https://blog.csdn.net/qq_40889820 2 #include<iostream> 3 #include<sstream> 4 #include<fstream> 5 #include<algorithm> 6 #include<cstring> 7 #include<iomanip> 8 #include<cstdlib> 9 #include<cctype> 10 #include<vector> 11 #include<string> 12 #include<cmath> 13 #include<ctime> 14 #include<stack> 15 #include<queue> 16 #include<map> 17 #include<set> 18 #define mem(a,b) memset(a,b,sizeof(a)) 19 #define random(a,b) (rand()%(b-a+1)+a) 20 #define ll long long 21 #define ull unsigned long long 22 #define e 2.71828182 23 #define Pi acos(-1.0) 24 #define ls(rt) (rt<<1) 25 #define rs(rt) (rt<<1|1) 26 #define lowbit(x) (x&(-x)) 27 using namespace std; 28 const int MAXN=5e4+5; 29 int a[MAXN]; 30 struct node 31 32 int sum; 33 T[MAXN<<2]; 34 int read() 35 36 int s=1,x=0; 37 char ch=getchar(); 38 while(!isdigit(ch)) if(ch==‘-‘) s=-1;ch=getchar(); 39 while(isdigit(ch)) x=10*x+ch-‘0‘;ch=getchar(); 40 return x*s; 41 42 void push_up(int p,int l,int r) 43 44 T[p].sum=T[ls(p)].sum+T[rs(p)].sum; 45 46 void build(int p,int l,int r) 47 48 if(l==r) 49 50 T[p].sum=a[l]; 51 return; 52 53 int mid=(l+r)>>1; 54 build(ls(p),l,mid); 55 build(rs(p),mid+1,r); 56 push_up(p,l,r); 57 58 void update(int p,int l,int r,int lr,int k) 59 60 if(l==r) 61 62 T[p].sum+=k; 63 return; 64 65 int mid=(l+r)>>1; 66 if(lr<=mid) update(ls(p),l,mid,lr,k); 67 else update(rs(p),mid+1,r,lr,k); 68 push_up(p,l,r); 69 70 int query(int p,int l,int r,int nl,int nr) 71 72 if(nl<=l&&nr>=r) return T[p].sum; 73 int mid=(l+r)>>1,ans=0; 74 if(nl<=mid) ans+=query(ls(p),l,mid,nl,nr); 75 if(nr>mid) ans+=query(rs(p),mid+1,r,nl,nr); 76 return ans; 77 78 int main() 79 80 int t=read(),nl,nr,n; 81 string ord; 82 for(int cas=1;cas<=t;++cas) 83 84 cout<<"Case "<<cas<<":\n"; 85 n=read(); 86 for(int i=1;i<=n;++i) 87 a[i]=read(); 88 build(1,1,n); 89 while(true) 90 91 cin>>ord; 92 if(ord=="End") break; 93 else if(ord=="Query") 94 95 nl=read(),nr=read(); 96 cout<<query(1,1,n,nl,nr)<<endl; 97 98 else if(ord=="Add") 99 100 nl=read(),nr=read(); 101 update(1,1,n,nl,nr); 102 103 else 104 105 nl=read(),nr=read(); 106 update(1,1,n,nl,-nr); 107 108 109 110 return 0; 111
单点修改、区间查询、维护区间最大值
1 //CSDN:https://blog.csdn.net/qq_40889820 2 #include<iostream> 3 #include<sstream> 4 #include<fstream> 5 #include<algorithm> 6 #include<cstring> 7 #include<iomanip> 8 #include<cstdlib> 9 #include<cctype> 10 #include<vector> 11 #include<string> 12 #include<cmath> 13 #include<ctime> 14 #include<stack> 15 #include<queue> 16 #include<map> 17 #include<set> 18 #define mem(a,b) memset(a,b,sizeof(a)) 19 #define random(a,b) (rand()%(b-a+1)+a) 20 #define ll long long 21 #define ull unsigned long long 22 #define e 2.71828182 23 #define Pi acos(-1.0) 24 #define ls(rt) (rt<<1) 25 #define rs(rt) (rt<<1|1) 26 #define lowbit(x) (x&(-x)) 27 using namespace std; 28 const int MAXN=2e5+5; 29 int a[MAXN]; 30 struct node 31 32 int maxx; 33 T[MAXN<<2]; 34 int read() 35 36 int s=1,x=0; 37 char ch=getchar(); 38 while(!isdigit(ch)) if(ch==‘-‘) s=-1;ch=getchar(); 39 while(isdigit(ch)) x=10*x+ch-‘0‘;ch=getchar(); 40 return x*s; 41 42 void push_up(int p,int l,int r) 43 44 T[p].maxx=max(T[ls(p)].maxx,T[rs(p)].maxx); 45 46 void build(int p,int l,int r) 47 48 if(l==r) 49 50 T[p].maxx=a[l]; 51 return; 52 53 int mid=(l+r)>>1; 54 build(ls(p),l,mid); 55 build(rs(p),mid+1,r); 56 push_up(p,l,r); 57 58 void update(int p,int l,int r,int lr,int k) 59 60 if(l==r) 61 62 T[p].maxx=k; 63 return; 64 65 int mid=(l+r)>>1; 66 if(lr<=mid) update(ls(p),l,mid,lr,k); 67 else update(rs(p),mid+1,r,lr,k); 68 push_up(p,l,r); 69 70 int query(int p,int l,int r,int nl,int nr) 71 72 if(nl<=l&&nr>=r) return T[p].maxx; 73 int mid=(l+r)>>1,ans=0; 74 if(nl<=mid) ans=max(query(ls(p),l,mid,nl,nr),ans); 75 if(nr>mid) ans=max(query(rs(p),mid+1,r,nl,nr),ans); 76 return ans; 77 78 int main() 79 80 int n,m; 81 while(~scanf("%d%d",&n,&m)) 82 83 for(int i=1;i<=n;++i) 84 a[i]=read(); 85 build(1,1,n); 86 char ord; 87 int A,B; 88 while(m--) 89 90 cin>>ord; 91 A=read(),B=read(); 92 if(ord==‘Q‘) 93 cout<<query(1,1,n,A,B)<<endl; 94 else 95 update(1,1,n,A,B); 96 97 98 return 0; 99
C、A Simple Problem with Integers
区间修改、区间查询、维护区间和、延迟标记
1 //CSDN:https://blog.csdn.net/qq_40889820 2 #include<iostream> 3 #include<sstream> 4 #include<fstream> 5 #include<algorithm> 6 #include<cstring> 7 #include<iomanip> 8 #include<cstdlib> 9 #include<cctype> 10 #include<vector> 11 #include<string> 12 #include<cmath> 13 #include<ctime> 14 #include<stack> 15 #include<queue> 16 #include<map> 17 #include<set> 18 #define mem(a,b) memset(a,b,sizeof(a)) 19 #define random(a,b) (rand()%(b-a+1)+a) 20 #define ll long long 21 #define ull unsigned long long 22 #define e 2.71828182 23 #define Pi acos(-1.0) 24 #define ls(rt) (rt<<1) 25 #define rs(rt) (rt<<1|1) 26 #define lowbit(x) (x&(-x)) 27 using namespace std; 28 const int MAXN=1e5+5; 29 ll a[MAXN]; 30 struct node 31 32 ll sum,tag; 33 T[MAXN<<2]; 34 ll read() 35 36 ll s=1,x=0; 37 char ch=getchar(); 38 while(!isdigit(ch)) if(ch==‘-‘) s=-1;ch=getchar(); 39 while(isdigit(ch)) x=10*x+ch-‘0‘;ch=getchar(); 40 return x*s; 41 42 void push_up(int p,int l,int r) 43 44 T[p].sum=T[ls(p)].sum+T[rs(p)].sum; 45 46 void build(int p,int l,int r) 47 48 if(l==r) 49 50 T[p].sum=a[l]; 51 return; 52 53 T[p].tag=0; 54 int mid=(l+r)>>1; 55 build(ls(p),l,mid); 56 build(rs(p),mid+1,r); 57 push_up(p,l,r); 58 59 void push_down(int p,int l,int r) 60 61 if(!T[p].tag) return; 62 T[ls(p)].tag+=T[p].tag,T[rs(p)].tag+=T[p].tag; 63 int mid=(l+r)>>1; 64 T[ls(p)].sum+=T[p].tag*(mid-l+1); 65 T[rs(p)].sum+=T[p].tag*(r-mid); 66 T[p].tag=0; 67 68 void update(int p,int l,int r,int nl,int nr,ll k) 69 70 if(nl<=l&&nr>=r) 71 72 T[p].sum+=k*(r-l+1); 73 T[p].tag+=k; 74 return ; 75 76 push_down(p,l,r); 77 int mid=(l+r)>>1; 78 if(nl<=mid) update(ls(p),l,mid,nl,nr,k); 79 if(nr>mid) update(rs(p),mid+1,r,nl,nr,k); 80 push_up(p,l,r); 81 82 ll query(int p,int l,int r,int nl,int nr) 83 84 if(nl<=l&&nr>=r) return T[p].sum; 85 push_down(p,l,r); 86 int mid=(l+r)>>1; 87 ll ans=0; 88 if(nl<=mid) ans+=query(ls(p),l,mid,nl,nr); 89 if(nr>mid) ans+=query(rs(p),mid+1,r,nl,nr); 90 return ans; 91 92 int main() 93 94 int n=read(),m=read(); 95 for(int i=1;i<=n;++i) 96 a[i]=read(); 97 build(1,1,n); 98 char ord; 99 int A,B; 100 ll C; 101 while(m--) 102 103 cin>>ord; 104 A=read(),B=read(); 105 if(ord==‘Q‘) 106 cout<<query(1,1,n,A,B)<<endl; 107 else 108 109 C=read(); 110 update(1,1,n,A,B,C); 111 112 113 return 0; 114
以上是关于[kuangbin带你飞]专题七 线段树的主要内容,如果未能解决你的问题,请参考以下文章
算法系列学习线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵