HDU1754 I hate it 线段树

线段树,单点修改

这道题就是给你一些学生的初始成绩,然后有m个命令,

输入 “Q A B ”的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少

输入为 “ U A B ”的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B

 1 #include<cstdio>

 2 #include<algorithm>

 3 using namespace std;

 4 #define lson l,m,rt<<1

 5 #define rson m+1,r,rt<<1|1

 6 const int inf=0x3f3f3f3f;

 7 const int maxn=200000+10;

 8 int _max[maxn<<2];

 9 void pushup(int rt)

10 {

11     _max[rt]=max(_max[rt<<1],_max[rt<<1|1]);

12 }

13 void build(int l,int r,int rt)

14 {

15     if(l==r){

16         scanf("%d",&_max[rt]);

17         return ;

18     }

19     int m=(l+r)>>1;

20     build(lson);

21     build(rson);

22     pushup(rt);

23 }

24 void update(int p,int add,int l,int r,int rt)

25 {

26     if(l==r){

27         _max[rt]=add;

28         return ;

29     }

30     int m=(l+r)>>1;

31     if(p<=m)

32         update(p,add,lson);

33     else

34         update(p,add,rson);

35     pushup(rt);

36 }

37 int query(int L,int R,int l,int r,int rt)

38 {

39     if(L<=l&&R>=r)

40         return _max[rt];

41     int m=(l+r)>>1;

42     int ans=-inf;

43     if(L<=m)

44         ans=max(ans,query(L,R,lson));

45     if(R>m)

46         ans=max(ans,query(L,R,rson));

47     return ans;

48 }

49 int main()

50 {

51     int n,m;

52     while(scanf("%d%d",&n,&m)!=EOF){

53         build(1,n,1);

54         char str[5];

55         int i,j;

56         while(m--){

57             scanf("%s",&str);

58             scanf("%d%d",&i,&j);

59             if(str[0]=='U'){

60                 update(i,j,1,n,1);

61             }

62             else{

63                 printf("%d\n",query(i,j,1,n,1));

64             }

65 

66         }

67     }

68     return 0;

69 }
View Code

 

你可能感兴趣的:(HDU)