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

[USACO08NOV]光開關Light Switching

題目描述

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



先簡化題面。

給出n個點An和m個操作,初始每個點Ai都為0

操作1:對於區間[x,y]中的每個點,將其值取反(A[i]=!A[i])

操作2:對於區間[x,y],統計其中1的個數。

顯而易見,樸素的暴力肯定是會TLE的。

我們很容易想到

1.統計一個區間中的1的個數,即為統計這個區間的和(tot(1)=sum[x,y])

2.而對於這個區間的每個值取反,即為用區間長度-區間和(sum[x,y]=(y-x+1)-sum[x,y])

3.若一個區間被取反的次數是2的倍數,相當於沒有進行操作。


統計區間和的方法有很多種。我們最常用的是樹狀陣列和線段樹。

但是樹狀陣列相對來說沒有線段樹好操作,所以在此我們用線段樹來維護區間和。

好訊息!我和百度已經簽訂了合同,如果不會線段樹的同學可以去百度搜索!

線上段樹的區間修改和區間求和中,我們常用的優化方法是引入tag點(也被稱作lazy點)。

在更新的時候先標記tag點,在下次需要經過此節點時再將tag點下放到其根節點的兩個子節點中。

這裡我們用tag來記錄節點被取反的次數,且每當tag增加時,對其mod2。

實現還是很簡單的。

下面放上程式碼。

#include<cstdio>
#include<iostream>
using namespace std;
struct tree
{
	int lc,rc,l,r,sum,tag;
}h[400001];
int n,m,tot=0;
void build(int l,int r)
{
	int now=++tot;
	h[now].l=l;
	h[now].r=r;
	int mid=(l+r)/2;
	if(l==r) return ;
	h[now].lc=tot+1;
	build(l,mid);
	h[now].rc=tot+1;
	build(mid+1,r);
}
void update(int k)
{
	h[h[k].lc].sum=(h[h[k].lc].r-h[h[k].lc].l+1)-h[h[k].lc].sum;
	h[h[k].rc].sum=(h[h[k].rc].r-h[h[k].rc].l+1)-h[h[k].rc].sum;
	h[h[k].lc].tag++;
	h[h[k].rc].tag++;
	h[h[k].lc].tag%=2;
	h[h[k].rc].tag%=2;
	h[k].tag=0;
}
void change(int k,int l,int r)
{
	if(h[k].l==l&&h[k].r==r)
	{
		h[k].sum=(h[k].r-h[k].l+1)-h[k].sum;
		h[k].tag++;
		h[k].tag%=2;
	}
	else
	{
		if(h[k].tag)
			update(k);
		int mid=(h[k].l+h[k].r)/2;
		if(l>mid) change(h[k].rc,l,r);
		else if(r<=mid) change(h[k].lc,l,r);
		else 
		{
			change(h[k].lc,l,mid);
			change(h[k].rc,mid+1,r);
		}
		h[k].sum=h[h[k].lc].sum+h[h[k].rc].sum;
	}
}
int query(int k,int l,int r)
{
	if(h[k].l==l&&h[k].r==r)
	return h[k].sum;
	if(h[k].tag) update(k);
	int mid=(h[k].l+h[k].r)/2;
	if(r<=mid) return query(h[k].lc,l,r);
	else if(l>mid) return query(h[k].rc,l,r);
	else return query(h[k].lc,l,mid)+query(h[k].rc,mid+1,r);
}
int main()
{
	cin>>n>>m;
	build(1,n);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		cin>>z;
		if(z==0)
		{
			cin>>x>>y;
			change(1,x,y);
		}
		else
		{
			cin>>x>>y;
			cout<<query(1,x,y)<<endl;
		}
	}
	return 0;
}