1026 [USACO 2008 Nov G]Light Switching 区间翻转

链接:https://ac.nowcoder.com/acm/contest/26896/1026
来源:牛客网

题目描述

Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of the larger toys is the lights in the barn. Each of the N (2 <= N <= 100,000) cow stalls conveniently numbered 1..N has a colorful light above it.
At the beginning of the evening, all the lights are off. The cows control the lights with a set of N pushbutton switches that toggle the lights; pushing switch i changes the state of light i from off to on or from on to off.
The cows read and execute a list of M (1 <= M <= 100,000) operations expressed as one of two integers (0 <= operation <= 1).
The first kind of operation (denoted by a 0 command) includes two subsequent integers Si and Ei (1 <= Si <= Ei <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from Si through Ei inclusive exactly once.
The second kind of operation (denoted by a 1 command) asks the cows to count how many lights are on in the range given by two integers Si and Ei (1 <= Si <= Ei <= N) which specify the inclusive range in which the cows should count the number of lights that are on.
Help FJ ensure the cows are getting the correct answer by processing the list and producing the proper counts.

输入描述:

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line represents an operation with three space-separated integers: operation, Si, and Ei

输出描述:

* Lines 1..number of queries: For each output query, print the count as an integer by itself on a single line.
示例1

输入

复制
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

输出

复制
1
2

 分析

区间翻转

注意根节点翻转,push_down的时候,如果叶子的lazy 是翻转状态,要把它翻回来,如果是正常状态,要翻过去。这里^1 就可以了

翻转可以用0的数量和1的数量交换来维护,也可以像代码,直接用区间长度减去区间1的数量

//-------------------------代码----------------------------

#define int ll
const int N = 1e6+10;
int n,m;

struct node {
    int l,r,len,sum,lazy;
} tr[N<<2];

void push_up(int u) {
    tr[u].sum = tr[u<<1].sum + tr[u<<1|1].sum;
}

void push_down(int u) {
    if(tr[u].lazy)  {
        tr[u<<1].sum = tr_len(ul) - tr[u<<1].sum;
        tr[u<<1].lazy ^= 1;
        tr[u<<1|1].lazy = tr_len(ur) - tr[u<<1|1].sum;
        tr[u<<1|1].lazy ^= 1;
        tr[u].lazy ^= 1;
    }
}
void build(int u,int l,int r) {
    tr[u] = {l,r};
    tr[u].len = tr_len(u);
    tr[u].lazy = tr[u].sum = 0;
    if(l == r) {return;}
    int mid = l + r >> 1;
    build(u<<1,l,mid);build(u<<1|1,mid+1,r);
}


void modify(int u,int l,int r) {
    if(l <= tr[u].l && tr[u].r <= r) {
        tr[u].sum = tr_len(u) - tr[u].sum;
        tr[u].lazy ^= 1;rt;
    } 
    push_down(u);
    if(l <= tr_mid) modify(u<<1,l,r);
    if(tr_mid < r) modify(u<<1|1,l,r);
    push_up(u);
}

int query(int u,int l,int r) {
    if(l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
    if(l > tr[u].r || tr[u].l > r) return 0;
    push_down(u);
    return query(u<<1,l,r)+query(u<<1|1,l,r);
}

void solve()
{
    cin>>n>>m;
    build(1,1,n);
    fo(i,1,m) {
        int op;cin>>op;
        if(op == 0) {
            int s,e;cin>>s>>e;   
            modify(1,s,e);
        } else {
            int s,e;cin>>s>>e;
            cout<<query(1,s,e)<<endl;
        }
    }
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

posted @ 2022-08-11 22:30  er007  阅读(48)  评论(0编辑  收藏  举报