复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73#include<stdio.h> #include<stdlib.h> #include<string.h> #include<limits.h> #include<algorithm> using namespace std; int sum[200005]; void build(int l,int r,int rt)//线段树的创建 {//l和r表示该节点线段树的左端点和右端点,rt表示该节点的编号(线段树是完全二叉树,rt*2和rt*2+1既是该节点的两个子节点) int m; if(l==r)//如果一个节点的去见识[a,b],那么他的两个子节点就为[a,x]和[x,b](a<=x<=b) { scanf("%d",&sum[rt]); return ; } m=(l+r)/2; build(l,m,rt*2); build(m+1,r,rt*2+1); sum[rt]=sum[rt*2]+sum[rt*2+1]; } int query(int L,int R,int l,int r,int rt)//线段树的查询 {//L和R表示要查询的[L,R]区间的值 int m,ans; if(l>=L && r<=R) return sum[rt]; m=(l+r)/2; ans=0; if(L<=m) ans+=query(L,R,l,m,rt*2); if(R>=m+1) ans+=query(L,R,m+1,r,rt*2+1); return ans; } void updata(int p,int add,int l,int r,int rt) {//*p和add表示p点的值上升add*/ int m; if(l==r) { sum[rt]+=add; return; } m=(l+r)/2; if(p<=m) updata(p,add,l,m,rt*2); else updata(p,add,m+1,r,rt*2+1); sum[rt]=sum[rt*2]+sum[rt*2+1]; } int main() { int T,n,a,b,cas; char op[11]; cas=1; scanf("%d",&T); while(T--) { scanf("%d",&n); build(1,n,1); printf("Case %d:n",cas++); while(scanf("%s",op),op[0]!='E') { scanf("%d%d",&a,&b); if(op[0]=='Q') printf("%dn",query(a,b,1,n,1)); else if(op[0]=='S') updata(a,-b,1,n,1); else updata(a,b,1,n,1); } } return 0; }
最后
以上就是犹豫山水最近收集整理的关于HDU-1166 敌兵布阵(线段树模板题)的全部内容,更多相关HDU-1166内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复