A comparative habit is popular in many schools. Teachers like to ask, from so and so to so and so, what is the highest score.
Many students are disgusted by this.
Like it or not, what you need to do now is to write a program to simulate the teacher's inquiry according to the teacher's requirements. Of course, teachers sometimes need to update a student's grades.
Input
This topic contains more than one set of tests. Please process until the end of the document.
In the first line of each test, there are two positive integers N and m (0 < N < = 200000, 0 < m < 5000), representing the number of students and the number of operations respectively.
Student ID numbers range from 1 to N.
The second line contains N integers representing the initial scores of these N students, where the number i represents the scores of the students with ID i.
Then there's line M. Each line has A character C (take only 'Q' or 'U') and two positive integers A, B.
When C is' Q ', it means that this is A query operation. It asks the students with ID from A to B (including A and b) what is the highest score.
When C is' U ', it means that this is an update operation, which requires changing the score of the student with ID A to B.
Output
For each query operation, output the highest score in one line.
#include<cstdio> #include <iostream> #include <algorithm> #include<bits/stdc++.h> using namespace std; #define ll long long #define lson l, mid, rt<<1 #define rson mid + 1, r, rt<<1|1 const int inf = 0x3f3f3f3f; const int maxn = 2e5 + 10; int tree[maxn << 2]; void push_up(int rt) { tree[rt] = max(tree[rt << 1] , tree[rt << 1|1]); } void build(int l, int r, int rt) { if(l == r) { scanf("%d", &tree[rt]); return; } int mid = (l + r) >> 1; build(lson); build(rson); push_up(rt); } void update(int pos, int val, int l, int r, int rt) { if(l == r) { tree[rt] = val; return; } int mid = (l + r) >> 1; if(pos <= mid) update(pos, val, lson); else update(pos, val, rson); push_up(rt); } int query(int L, int R, int l, int r, int rt) { int ans = -inf; if(L <= l && r <= R) return tree[rt]; int mid = (l + r) >> 1; if(L <= mid) ans = max(ans, query(L, R, lson)); if(R > mid) ans = max(ans, query(L, R, rson)); return ans; } int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { build(1, n, 1); while(m--) { char s[6]; cin >> s; if(s[0] == 'U') { int i, j; scanf("%d%d", &i, &j); update(i, j, 1, n, 1); } else if(s[0] == 'Q') { int i, j; scanf("%d%d", &i, &j); printf("%d\n", query(i, j, 1, n, 1)); } } } return 0; }