bzoj1230 [Usaco2008 Nov]lites 开关灯

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1230

【题解】

线段树裸题啊

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, m;

struct SMT {
    int w[M], n; bool tag[M];
    inline void set(int _n) {
        n = _n;
        memset(w, 0, sizeof w);
        memset(tag, 0, sizeof tag);
    }
    # define ls (x<<1)
    # define rs (x<<1|1)
    inline void up(int x) {
        if(!x) return ;
        w[x] = w[ls] + w[rs];
    }
    inline void pushtag(int x, int l, int r) {
        if(!x) return ;
        w[x] = r-l+1-w[x];
        tag[x] ^= 1;
    }
    inline void down(int x, int l, int r) {
        if(!x) return ;
        if(!tag[x]) return ;
        int mid = l+r>>1;
        pushtag(ls, l, mid);
        pushtag(rs, mid+1, r);
        tag[x] = 0;
    }
    inline void edt(int x, int l, int r, int L, int R) {
        if(L <= l && r <= R) {
            pushtag(x, l, r);
            return ;
        }
        down(x, l, r);
        int mid = l+r>>1;
        if(L <= mid) edt(ls, l, mid, L, R);
        if(R > mid) edt(rs, mid+1, r, L, R);
        up(x);
    }
    inline int query(int x, int l, int r, int L, int R) {
        if(L <= l && r <= R) return w[x];
        down(x, l, r);
        int mid = l+r>>1, ret = 0;
        if(L <= mid) ret += query(ls, l, mid, L, R);
        if(R > mid) ret += query(rs, mid+1, r, L, R);
        return ret;
    }
    inline void edt(int L, int R) {
        edt(1, 1, n, L, R);
    }
    inline int sum(int L, int R) {
        return query(1, 1, n, L, R);
    }
}T;

int main() {
    cin >> n >> m;
    T.set(n);
    int opt, S, E;    
    while(m--) {
        scanf("%d%d%d", &opt, &S, &E);
        if(opt == 0) T.edt(S, E);
        else printf("%d\n", T.sum(S, E));
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/galaxies/p/bzoj1230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值