bzoj 1230: [Usaco2008 Nov]lites 开关灯

sb 线段树

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> >

using namespace std;

const int N = 1e5 + 10;
const int M = 10000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

int n, m, a[N << 2], lazy[N << 2];

void push_down(int rt, int l, int r) {
    if(!lazy[rt]) return;
    int mid = l + r >> 1;
    a[rt << 1] = mid - l + 1 - a[rt << 1];
    a[rt << 1 | 1] = r - mid - a[rt << 1 | 1];
    lazy[rt << 1] ^= lazy[rt];
    lazy[rt << 1 | 1] ^= lazy[rt];
    lazy[rt] = 0;
}
void update(int L, int R, int l, int r, int rt) {
    if(l >= L && r <= R) {
        a[rt] = r - l + 1 - a[rt];
        lazy[rt] ^= 1;
        return;
    }
    push_down(rt, l, r);
    int mid = l + r >> 1;
    if(L <= mid) update(L, R, l, mid, rt << 1);
    if(R > mid) update(L, R, mid + 1, r, rt << 1 | 1);
    a[rt] = a[rt << 1] + a[rt << 1 | 1];
}

int query(int L, int R, int l, int r, int rt) {
    if(l >= L && r <= R) return a[rt];
    push_down(rt, l, r);
    int mid = l + r >> 1, ans = 0;
    if(L <= mid) ans += query(L, R, l, mid, rt << 1);
    if(R > mid) ans += query(L, R, mid + 1, r, rt << 1 | 1);
    return ans;
}

int main() {
    scanf("%d%d", &n, &m);
    while(m--) {
        int op, l, r;
        scanf("%d%d%d", &op, &l, &r);
        if(!op) {
            update(l, r, 1, n, 1);
        } else {
            printf("%d\n", query(l, r, 1, n, 1));
        }
    }
    return 0;
}

/*
*/

 

posted @ 2018-05-31 19:07  NotNight  阅读(110)  评论(0编辑  收藏  举报