题目大意:
输入N,M,表示有N个学生,M次操作,操作主要有两种查询[A,B]内分数最高的学生 (2) 更新 将A学生的分数修改为 B。
算法思想:
主要使用线段树的,单点更新,和区间查询操作。
代码如下:
复制代码
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#include <iostream> #include <cstdio> #include <algorithm> #define lson l,m,rt<<1 #define rson m+1,r,rt << 1|1 using namespace std; const int MAXN=222222; int Max[MAXN<<2]; void PushUp(int rt){ Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);//孩子节点中的最大值 } void Build(int l,int r,int rt){ if(l==r){ scanf("%d",&Max[rt]); return ; } int m=(l+r)>>1; Build(lson); Build(rson); PushUp(rt); } int Query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){//包含了所有区间 return Max[rt]; } int m=(l+r)>>1; int ret=0; if(L<=m) ret=max(ret,Query(L,R,lson)); if(R>m) ret=max(ret,Query(L,R,rson)); return ret; } void Update(int p,int sc,int l,int r,int rt){ if(l==r)//只有一个节点 { Max[rt]=sc; return ; } int m=(l+r)>>1; if(p<=m) Update(p,sc,lson); else Update(p,sc,rson); PushUp(rt); } int main(){ int N,M,A,B; char C; while(scanf("%d%d",&N,&M)!=EOF) { Build(1,N,1); while(M--){ char op[2]; scanf("%s%d%d",op,&A,&B); if(op[0]=='Q') printf("%dn",Query(A,B,1,N,1)); else Update(A,B,1,N,1); } } return 0; }
最后
以上就是矮小服饰最近收集整理的关于HDOJ 1754 ------线段树的全部内容,更多相关HDOJ内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。