题解 | #涂颜料#
涂颜料
https://www.nowcoder.com/practice/4ef038ae1c5f4524b8a8a0c1e6b062a1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int q = in.nextInt();
char[] color = {'B', 'R', 'G'};
int[] diff = new int[n + 1];
while (q > 0) {
int start = in.nextInt();
int end = in.nextInt();
diff[start] += 1;
// 如果end在数组的结尾出,没必要将[end, -1]区间的元素减一
if(end + 1 < n + 1) diff[end + 1] -= 1;
q--;
}
for (int i = 1; i <= n; i++) {
diff[i] = diff[i - 1] + diff[i];
}
for (int i = 1; i <= n ; i++) {
if(diff[i] == 0){
System.out.print('O');
}else if(diff[i] % 3 == 0){
System.out.print('B');
}else if(diff[i] % 3 == 1){
System.out.print('R');
}else{
System.out.print('G');
}
}
}
}
#差分数组#
查看1道真题和解析