P1531 I Hate It, p1531hateit
Background
Many schools have a popular habit. Teachers like to ask, from xx to xx, what is the highest score. This made many students very disgusted.
Description
Whether you like it or not, what you need to do now is to write a program to simulate the instructor's inquiry according to the instructor's requirements. Of course, the teacher sometimes needs to update the score of a student.
Input/Output Format
Input Format:
The first line has 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 are separated from 1 to N. The second row contains N integers, indicating the initial score of the N students. The number of I represents the score of the students whose ID is I. Next there are M rows. Each line has A character C (only 'q' or 'U'), and two positive integers A and B. When C is 'Q', it indicates that this is A query operation. It asks the students whose ID ranges from A to B (including A and B) about the highest score. When C is 'U', it indicates that this is an update operation. If the current score of student A is lower than B, change the score of student A whose ID is A to B, otherwise, it will not be changed.
Output Format:
Output the highest score in one row for each query operation
Input and Output sample input sample #1:
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Output sample #1:
5659
Line Segment tree interval query + single point Modification
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 # define ls k <1 8 # define rs k <1 | 1 9 using namespace std; 10 const int MAXN = 2000001; 11 void read (int & n) 12 {13 char c = '+'; int x = 0; bool flag = 0; 14 while (c <'0' | c> '9') 15 {c = getchar (); if (c = '-') flag = 1 ;} 16 while (c> = '0' & c <= '9') 17 {x = x * 10 + c-48, c = getchar ();} 18 flag = 1? N =-x: n = x; 19} 20 int n, m; 21 int ans =-1; 22 struct node23 {24 int l, r, w; 25} tree [MAXN * 4]; 26 void update (int k) 27 {28 tree [k]. w = max (tree [ls]. w, tree [rs]. w); 29} 30 void build_tree (int ll, int rr, int k) 31 {32 tree [k]. l = ll; tree [k]. r = rr; 33 if (ll = rr) 34 {35 read (tree [k]. w); 36 return; 37} 38 int mid = (ll + rr)> 1; 39 build_tree (ll, mid, ls); 40 build_tree (mid + 1, rr, rs); 41 update (k); 42} 43 void query (int ll, int rr, int k) 44 {45 if (ll> tree [k]. r | rr <tree [k]. l) 46 return; 47 if (ll <= tree [k]. l & tree [k]. r <= rr) 48 {49 ans = max (ans, tree [k]. w); 50 return; 51} 52 int mid = (tree [k]. l + tree [k]. r)> 1; 53 query (ll, rr, ls); 54 query (ll, rr, rs); 55} 56 void change (int pos, int v, int k) 57 {58 if (pos> tree [k]. r | pos <tree [k]. l) 59 return; 60 if (tree [k]. l = tree [k]. r) 61 {62 tree [k]. w = v; 63 return; 64} 65 change (pos, v, ls); 66 change (pos, v, rs); 67 update (k ); 68} 69 int main () 70 {71 read (n); read (m); 72 build_tree (1, n, 1); 73 for (int I = 1; I <= m; I ++) 74 {75 string s; 76 cin> s; 77 if (s [0] = 'q ') // query 78 {79 int l, r; 80 read (l); read (r); 81 ans = 0; 82 query (l, r, 1 ); 83 printf ("% d \ n", ans); 84} 85 else 86 {87 int x, y; 88 read (x); read (y ); 89 ans = 0; 90 query (x, x, 1); 91 if (ans <y) 92 change (x, y, 1); 93} 94} 95 return 0; 96}