fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.Arrays;
  6.  
  7. import static java.lang.Integer.parseInt;
  8.  
  9. class FlippingCoins {
  10.  
  11. static int tree[] = new int[400010];
  12. static int lazy[] = new int[400010];
  13. static final int UNDEFINED = -1;
  14.  
  15. public static void main(String[] args) throws IOException {
  16. FastReader reader = FastReader.SYSTEM_READER;
  17. int n, q;
  18. String s[];
  19. //s = br.readLine().split("\\s");
  20. n = reader.nextInt();
  21. q = reader.nextInt();
  22. Arrays.fill(lazy, 0, 200010, -1);
  23. for (int i = 0; i < q; i++) {
  24. //s = br.readLine().split("\\s");
  25. int type = reader.nextInt();
  26. int A = reader.nextInt();
  27. int B = reader.nextInt();
  28. if (type == 1) {
  29. System.out.println(query(1, 0, n - 1, A, B));
  30. } else {
  31. update(1, 0, n - 1, A, B);
  32. }
  33. }
  34. }
  35.  
  36. private static int query(int id, int start, int end, int a, int b) {
  37. if (a > end || b < start) {
  38. return 0;
  39. }
  40. if (start >= a && end <= b) {
  41. return tree[id];
  42. }
  43. if (lazy[id] > 0) {
  44. shift(id, start, end);
  45. }
  46. int mid = (start + end) / 2;
  47. return query(id << 1, start, mid, a, b) + query((id << 1) + 1, mid + 1, end, a, b);
  48. }
  49.  
  50. private static void update(int id, int start, int end, int a, int b) {
  51. if (a > end || b < start) {
  52. return;
  53. }
  54. if (start >= a && end <= b) {
  55. updateNode(id, start, end);
  56. return;
  57. }
  58. if (lazy[id] > 0) {
  59. shift(id, start, end);
  60. }
  61. int mid = (start + end) / 2;
  62. update(id << 1, start, mid, a, b);
  63. update((id << 1) + 1, mid + 1, end, a, b);
  64. tree[id] = tree[id << 1] + tree[(id << 1) + 1];
  65. }
  66.  
  67. private static void updateNode(int id, int start, int end) {
  68. lazy[id] = 1-lazy[id];
  69. tree[id] = (end - start + 1) - tree[id];
  70. }
  71.  
  72. private static void shift(int id, int start, int end) {
  73. int mid = (start + end) / 2;
  74. lazy[id << 1] = lazy[id];
  75. lazy[(id << 1) + 1] = lazy[id];
  76. tree[id << 1] = (mid - start + 1) - tree[id<<1];
  77. tree[(id << 1) + 1] = (end - (mid + 1) + 1) - tree[(id << 1) + 1];
  78. /*updateNode((id << 1), start, mid);
  79.   updateNode((id << 1) + 1, start, mid);*/
  80. lazy[id] = 0;
  81. }
  82.  
  83. static final class FastReader {
  84. public static final FastReader SYSTEM_READER = new FastReader(System.in);
  85. private final InputStream in;
  86. private final byte[] buffer = new byte[1 << 16];
  87. private int pos, count;
  88.  
  89. public FastReader(InputStream in) {
  90. this.in = in;
  91. pos = count = 0;
  92. }
  93.  
  94. public int nextInt() {
  95. int c;
  96. while ((c = read()) < '0');
  97. int result = c - '0';
  98. while ((c = read() - '0') >= 0)
  99. result = 10 * result + c;
  100. return result;
  101. }
  102. public long nextLong() {
  103. int c;
  104. while ((c = read()) < '0');
  105. long result = c - '0';
  106. while ((c = read() - '0') >= 0)
  107. result = 10 * result + c;
  108. return result;
  109. }
  110.  
  111. public String nextString() {
  112. StringBuilder s = new StringBuilder();
  113. int c;
  114. while ((c = read()) >= 33)
  115. s.append((char) c);
  116. return s.toString();
  117. }
  118.  
  119. private void fillBuffer() {
  120. try {
  121. count = in.read(buffer, pos = 0, buffer.length);
  122. } catch (Exception e) {
  123. }
  124. }
  125.  
  126. public int read() {
  127. if (pos == count)
  128. fillBuffer();
  129. return buffer[pos++];
  130. }
  131. }
  132.  
  133.  
  134. }
Success #stdin #stdout 0.1s 31476KB
stdin
4 5
1 0 0
0 0 0 
1 0 0
0 0 3
1 0 0
stdout
0
1
0