Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import static java.lang.Integer.parseInt;
- FlippingCoins {
- static int tree[] = new int[400010];
- static int lazy[] = new int[400010];
- static final int UNDEFINED = -1;
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- FastReader reader = FastReader.SYSTEM_READER;
- int n, q;
- String s[];
- //s = br.readLine().split("\\s");
- n = reader.nextInt();
- q = reader.nextInt();
- Arrays.fill(lazy, 0, 200010, -1);
- for (int i = 0; i < q; i++) {
- //s = br.readLine().split("\\s");
- int type = reader.nextInt();
- int A = reader.nextInt();
- int B = reader.nextInt();
- if (type == 1) {
- System.out.println(query(1, 0, n - 1, A, B));
- } else {
- update(1, 0, n - 1, A, B);
- }
- }
- }
- private static int query(int id, int start, int end, int a, int b) {
- if (a > end || b < start) {
- return 0;
- }
- if (start >= a && end <= b) {
- return tree[id];
- }
- if (lazy[id] > 0) {
- shift(id, start, end);
- }
- int mid = (start + end) / 2;
- return query(id << 1, start, mid, a, b) + query((id << 1) + 1, mid + 1, end, a, b);
- }
- private static void update(int id, int start, int end, int a, int b) {
- if (a > end || b < start) {
- return;
- }
- if (start >= a && end <= b) {
- updateNode(id, start, end);
- return;
- }
- if (lazy[id] > 0) {
- shift(id, start, end);
- }
- int mid = (start + end) / 2;
- update(id << 1, start, mid, a, b);
- update((id << 1) + 1, mid + 1, end, a, b);
- tree[id] = tree[id << 1] + tree[(id << 1) + 1];
- }
- private static void updateNode(int id, int start, int end) {
- lazy[id] = 1-lazy[id];
- tree[id] = (end - start + 1) - tree[id];
- }
- private static void shift(int id, int start, int end) {
- int mid = (start + end) / 2;
- lazy[id << 1] = lazy[id];
- lazy[(id << 1) + 1] = lazy[id];
- tree[id << 1] = (mid - start + 1) - tree[id<<1];
- tree[(id << 1) + 1] = (end - (mid + 1) + 1) - tree[(id << 1) + 1];
- /*updateNode((id << 1), start, mid);
- updateNode((id << 1) + 1, start, mid);*/
- lazy[id] = 0;
- }
- static final class FastReader {
- public static final FastReader SYSTEM_READER = new FastReader(System.in);
- private final InputStream in;
- private final byte[] buffer = new byte[1 << 16];
- private int pos, count;
- public FastReader(InputStream in) {
- this.in = in;
- pos = count = 0;
- }
- public int nextInt() {
- int c;
- while ((c = read()) < '0');
- int result = c - '0';
- while ((c = read() - '0') >= 0)
- result = 10 * result + c;
- return result;
- }
- public long nextLong() {
- int c;
- while ((c = read()) < '0');
- long result = c - '0';
- while ((c = read() - '0') >= 0)
- result = 10 * result + c;
- return result;
- }
- public String nextString() {
- StringBuilder s = new StringBuilder();
- int c;
- while ((c = read()) >= 33)
- s.append((char) c);
- return s.toString();
- }
- private void fillBuffer() {
- try {
- count = in.read(buffer, pos = 0, buffer.length);
- } catch (Exception e) {
- }
- }
- public int read() {
- if (pos == count)
- fillBuffer();
- return buffer[pos++];
- }
- }
- }
Add Comment
Please, Sign In to add comment