fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int n = scanner.nextInt();
  7. int q = scanner.nextInt();
  8. int[] a = new int[n];
  9. for (int i = 0; i < n; i++) {
  10. a[i] = scanner.nextInt();
  11. }
  12.  
  13. Set<Integer> colorSet = new HashSet<>();
  14. for (int color : a) {
  15. colorSet.add(color);
  16. }
  17. int m = colorSet.size();
  18.  
  19. int pos = 0; // 当前起始位置(0-based)
  20.  
  21. for (int i = 0; i < q; i++) {
  22. String direction = scanner.next();
  23. int x = scanner.nextInt();
  24.  
  25. if (x >= n) {
  26. System.out.println(m);
  27. // 更新位置
  28. if (direction.equals("L")) {
  29. pos = (pos + x) % n;
  30. } else {
  31. pos = (pos - x) % n;
  32. if (pos < 0) pos += n;
  33. }
  34. continue;
  35. }
  36.  
  37. Set<Integer> currentColors = new HashSet<>();
  38. if (direction.equals("L")) {
  39. for (int j = 0; j < x; j++) {
  40. int idx = (pos + j) % n;
  41. currentColors.add(a[idx]);
  42. }
  43. pos = (pos + x) % n;
  44. } else {
  45. for (int j = 0; j < x; j++) {
  46. int idx = (pos - j) % n;
  47. if (idx < 0) idx += n;
  48. currentColors.add(a[idx]);
  49. }
  50. pos = (pos - x) % n;
  51. if (pos < 0) pos += n;
  52. }
  53. System.out.println(currentColors.size());
  54. }
  55. }
  56. }
Success #stdin #stdout 0.13s 56580KB
stdin
6 4
1 1 4 5 1 4
L 2
L 3
R 12
R 1
stdout
1
3
3
1