天天看点

LuoguP5057 [CQOI2006]简单题

/**
 * @Date:   2019-03-13T19:28:51+08:00
 * @Last modified time: 2019-03-13T19:28:52+08:00
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {
    int l, r, w, f;
}tree[A];
int n, m, opt, a, b, ans;
void build(int k, int l, int r) {
    tree[k].l = l; tree[k].r = r;
    if (l == r) {
        tree[k].w = 0; tree[k].f = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(k << 1, l, m);
    build(k << 1 | 1, m + 1, r);
}
void down(int k) {
    tree[k << 1].f ^= tree[k].f;
    tree[k << 1 | 1].f ^= tree[k].f;
    tree[k << 1].w = tree[k << 1].r - tree[k << 1].l + 1 - tree[k << 1].w;
    tree[k << 1 | 1].w = tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1 - tree[k << 1 | 1].w;
    tree[k].f = 0;
}
void change(int k) {
    if (tree[k].l >= a and tree[k].r <= b) {
        tree[k].w = tree[k].r - tree[k].l + 1 - tree[k].w;
        tree[k].f ^= 1;
        return;
    }
    if (tree[k].f) down(k);
    int m = (tree[k].l + tree[k].r) >> 1;
    if (a <= m) change(k << 1);
    if (b > m) change(k << 1 | 1);
    tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void ask(int k) {
    if (tree[k].l == tree[k].r) {
        ans = tree[k].w;
        return;
    }
    if (tree[k].f) down(k);
    int m = (tree[k].l + tree[k].r) >> 1;
    if (a <= m) ask(k << 1);
    else ask(k << 1 | 1);
}

int main(int argc, char const *argv[]) {
    // freopen("ss.in", "r", stdin);
    cin >> n >> m;
    build(1, 1, n);
    while (m--) {
        cin >> opt;
        if (opt == 1) {
            cin >> a >> b;
            change(1);
        }
        else {
            cin >> a;
            ask(1);
            cout << ans << endl;
        }
    }
    return 0;
}