1. 程式人生 > >洛谷 P2826 [USACO08NOV]光開關Light Switching

洛谷 P2826 [USACO08NOV]光開關Light Switching

producing iostream begin dash eno john clu play roc

題目鏈接:https://www.luogu.org/problem/show?pid=2826

題目描述

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 S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from S_i through E_i 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 S_i and E_i (1 <= S_i <= E_i <= 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.

燈是由高科技——外星人鼠標操控的。你只要左擊兩個燈所連的鼠標,

這兩個燈,以及之間的燈都會由暗變亮,或由亮變暗。右擊兩個燈所連的鼠

標,你就可以知道這兩個燈,以及之間的燈有多少燈是亮的。你的任務是在LZ

之前算出燈的亮滅。

輸入輸出格式

輸入格式:

第1 行: 用空格隔開的兩個整數N 和M,n 是燈數

第2..M+1 行: 每行表示一個操作, 有三個用空格分開的整數: 指令號, S_i 和E_i

第1 種指令(用0 表示)包含兩個數字S_i 和E_i (1 <= S_i <= E_i <= N), 它們表示起

始開關和終止開關. 表示左擊

第2 種指令(用1 表示)同樣包含兩個數字S_i 和E_i (1 <= S_i <= E_i <= N), 不過這

種指令是詢問從S_i 到E_i 之間的燈有多少是亮著的.

輸出格式:

輸入輸出樣例

輸入樣例#1:
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
輸出樣例#1:
1
2

說明

技術分享

原題時間限制為2s,內存限制為16M

這題的難度被評到了提高+/省選-,AC以後簡直興奮ヾ(?‘?`?)?

但是據說這個難度很虛...因為通過的人很少...

做法也是線段樹,涉及到燈的開關,有以下幾點需要註意:

1.開始時燈都開著,如果不用結構體來記線段樹,可以不必初始化,然後用0和1代表開和關

2.實現燈的開關可以用異或(^):1^1 = 0 , 0^1 = 1.

3.使用lazy標記;lazy也要用異或來處理

AC代碼:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>

using namespace std;

const int MAXN = 100000 + 5;
int stdata[MAXN << 2],stlazy[MAXN << 2],num[MAXN << 2];
int m,n;

void putdown(int o,int l,int r)//下放lazy標記 
{
    stlazy[o << 1] ^= stlazy[o];
    stlazy[o << 1 | 1] ^= stlazy[o];
    stlazy[o] = 0;
    int mid = (l + r) >> 1;
    stdata[o << 1] = (mid - l + 1) - stdata[o << 1];
    stdata[o <<1 | 1] = (r - mid) - stdata[o << 1 | 1];  
}

int ask(int ll,int rr,int o,int l,int r)
{//詢問開著的燈的數量 
    if(l >= ll && r <= rr)
    {
        return stdata[o];
    }
    int ans = 0;
    if(stlazy[o]) putdown(o,l,r);
    int mid = (l + r) >> 1;
    if(mid >= ll) ans += ask(ll,rr,o << 1,l,mid);
    if(mid < rr) ans += ask(ll,rr,o << 1 | 1,mid + 1,r);
    return ans;
}

void modify2(int ll,int rr,int o,int l,int r)//區間修改
{
    if(ll <= l && rr >= r)
    {
        stdata[o] = (r - l + 1) - stdata[o]; 
        stlazy[o] ^= 1;
        return;
    }
    int mid = (l + r) >> 1;
    if(stlazy[o]) putdown(o,l,r);
    if(mid >= ll) modify2(ll,rr,o << 1,l,mid);
    if(mid < rr) modify2(ll,rr,o <<1 | 1,mid + 1,r);
    stdata[o] = stdata[o << 1] + stdata[o << 1 | 1]; 
}

int main()
{
    scanf("%d%d",&n,&m);
    int op,l,r;
    for(int i = 0;i < m;i ++)
    {
        scanf("%d",&op);
        scanf("%d%d",&l,&r);
        if(op == 0)
        {
            modify2(l,r,1,1,n);    
        }
        if(op == 1)
        {
            int t = ask(l,r,1,1,n);
            printf("%d\n",t);
        }
    }
    return 0;
}

註意:有一些變量是在函數內聲明的,比如int ans、int mid,絕對不可以在函數外面聲明,因為函數內每一層遞歸調用都會生成新的同名變量,可以保證在回退時得到正確的變量值,否則可能會導致輸出為0.

洛谷 P2826 [USACO08NOV]光開關Light Switching