import java.util.Scanner;
public class TopOfScore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] score = new int[n];
String operation= null;
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
score[i] = sc.nextInt();
}
sc.nextLine();
for (int j = 0; j < m; j++) {
String[] strs = sc.nextLine().split(" ");
operation = strs[0];
a = Integer.parseInt(strs[1])-1;
b = Integer.parseInt(strs[2])-1;
int max = 0;
//Q操作和U操作是分开的不能一起判断a 和b的关系因为以下对换影响了U操作所以导致错误。
/*if(a>b) {
a =a^b;
b =a^b;
a = a^b;
}*/
if(operation.equals("Q")) {
for(int k = Math.min(a, b);k<=Math.max(a, b);k++) {
max = Math.max(max, score[k]);
}
System.out.println(max);
}
if(operation.equals("U")) {
score[a] =b+1;
}
}
}
}
}
有两点需要注意。
1.query查询的时候有可能a 和b的大小关系会导致程序的错误。因为数组查询的时候一定是a<b的。
2.对换a 和b是针对query操作的如果在步骤之中没有区分query操作和update操作会直接导致运算结果的错误。
思考:
用Scanner类是一并输入保存为字符串类型然后利用split()函数进行分割是不是比一个一个输入更加高效率呢?