Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。 在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。 学生ID编号分别从1编到N。 第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。 接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。 当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。 当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cinAuthor
linle
Source
Recommend
这道题非常地简单,直接用线段树就行了。线段树的每一个节点就储存这段区间的最大值,
由于只有单点更新,连最令人讨厌的延时更新都没有,所以说非常地适合练线段树。
至于pushdown()就不用写了,pushup就是把两个子节点的最大值比较,将它们的最大
值赋给父节点
另外:1.尽量使用scanf而不是用cin,因为数据较多,cin读起来很慢
2.看清题目,多组数据!
3.使用指针的用户,请自行使用delete清理内存,小心MemoryLimitExceeded
附上用delete清理内存的函数:
1 void clear(TreeNode *now){ 2 if(now == NULL) return ; 3 clear(now->left); 4 clear(now->right); 5 delete now; 6 }
就是将树后续遍历一次,遍历完一个根节点的子树后,用delete关键字释放内存
Code:
1 /**
2 * hdoj
3 * Problem#1754
4 */
5 #include<iostream>
6 #include<cstdio>
7 using namespace std;
8 int *a; //原数组
9 typedef class TreeNode {
10 private:
11 void init(){
12 left = NULL;
13 right = NULL;
14 maxv = 0xffffffff;
15 state = 0;
16 }
17 public:
18 int from;
19 int end;
20 TreeNode* left;
21 TreeNode* right;
22 int maxv;
23 int state;
24 TreeNode(){ init(); }
25 TreeNode(int from,int end){
26 init();
27 this->from = from;
28 this->end = end;
29 }
30 }TreeNode;
31 typedef class Tree{
32 private:
33 static int _max(int a, int b){ return (a > b)?(a):(b); }
34 public:
35 TreeNode* root;
36 Tree():root(NULL){}
37 Tree(int size){
38 root = build(root, 1, size);
39 }
40 void pushUp(TreeNode* node){
41 if(node->left == NULL || node->right == NULL ) return ;
42 node->maxv = _max(node->right->maxv, node->left->maxv);
43 }
44 TreeNode* build(TreeNode *root, int from, int end){
45 root = new TreeNode(from, end);
46 if(from == end){
47 root->maxv = a[from];
48 return root;
49 }
50 int mid = (from + end)/2;
51 root->left = build(root->left, from, mid);
52 root->right= build(root->right, mid + 1, end);
53 pushUp(root);
54 return root;
55 }
56 void update(TreeNode *now, int index, int data){
57 if(index == now->from && index == now->end){
58 now->maxv = data;
59 return ;
60 }
61 int mid = (now->from + now->end)/2;
62 if( index <= mid ) update(now->left, index, data);
63 else update(now->right, index, data);
64 pushUp(now);
65 }
66 int query(TreeNode *now, int from, int end){
67 if(from <= now->from && end >= now->end){
68 return now->maxv;
69 }
70 int mid = (now->from + now->end)/2;
71 if( end <= mid ) return query(now->left, from, end);
72 else if( from > mid ) return query(now->right, from, end);
73 else{
74 return _max(query(now->left, from, mid),query(now->right, mid+1, end));
75 }
76 }
77 void clear(TreeNode *now){
78 if(now == NULL) return ;
79 clear(now->left);
80 clear(now->right);
81 delete[] now;
82 }
83 }Tree;
84 Tree MyTree;
85 int n,m;
86 char ch;
87 int c,d;
88 int main(){
89 while(~scanf("%d%d",&n,&m)){
90 a = new int[(const int)(n + 1)];
91 for(int i = 1;i <= n;i++){
92 scanf("%d",&a[i]);
93 }
94 MyTree = Tree(n);
95 for(int i = 1;i <= m;i++){
96 cin>>ch;
97 if(ch == 'Q'){
98 scanf("%d%d",&c,&d);
99 printf("%d
",MyTree.query(MyTree.root, c, d));
100 }else{
101 scanf("%d%d",&c,&d);
102 MyTree.update(MyTree.root, c, d);
103 }
104 }
105 MyTree.clear(MyTree.root);
106 delete[] a;
107 }
108 return 0;
109 }