[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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值