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. Set<Integer> colorSet = new HashSet<>();
  13. for (int num : a) {
  14. colorSet.add(num);
  15. }
  16. int totalColors = colorSet.size();
  17. long pos = 0; // current position
  18.  
  19. for (int i = 0; i < q; i++) {
  20. char c = scanner.next().charAt(0);
  21. int x = scanner.nextInt();
  22. if (c == 'L') {
  23. pos += x;
  24. } else {
  25. pos -= x;
  26. }
  27. if (x >= n) {
  28. System.out.println(totalColors);
  29. continue;
  30. }
  31. long start, end;
  32. if (c == 'L') {
  33. start = pos - x;
  34. end = pos - 1;
  35. } else {
  36. start = pos - x + 1;
  37. end = pos;
  38. }
  39. start = (start % n + n) % n;
  40. end = (end % n + n) % n;
  41. Set<Integer> currentColors = new HashSet<>();
  42. if (start <= end) {
  43. for (int j = (int) start; j <= end; j++) {
  44. currentColors.add(a[j]);
  45. }
  46. } else {
  47. for (int j = (int) start; j < n; j++) {
  48. currentColors.add(a[j]);
  49. }
  50. for (int j = 0; j <= end; j++) {
  51. currentColors.add(a[j]);
  52. }
  53. }
  54. System.out.println(currentColors.size());
  55. }
  56. }
  57. }
Success #stdin #stdout 0.12s 56560KB
stdin
6 4
1 1 4 5 1 4
L 2
L 3
R 12
R 1
stdout
1
3
3
1