• 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
    ", query(l, r, 1, n, 1));
            }
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    【原创】【Android New Features】—— 关于ADT 17的BuildConfig.DEBUG
    《jQuery、jQuery UI及jQuery Mobile技巧与示例》勘误收集
    《jQuery UI开发指南》勘误收集
    获取输入框中选中文本相对于页面的偏移
    html标签对应的英文原文
    CSS选择器解析
    Input File 表单上传按钮美化
    常用CSS代码片断
    web前端开发框架搜集
    Fiddler
  • 原文地址:https://www.cnblogs.com/CJLHY/p/9118434.html
Copyright © 2020-2023  润新知