BZOJ 1230: [Usaco2008 Nov]lites 开关灯

http://www.zybbs.org/JudgeOnline/problem.php?id=1230

今天下午的多校联合赛就出类似这个题的那个题了 T T 。。都是在区间里进行异或操作。只不过下午那题更恶心,还要输出指定区间里连续的1 。

这个题只需要输出1的数目即可(开着灯的数目)。

下午那个题一直有问题,后来兴海帮我看了,基本知道是哪错了。我总是更新的时候有点迷茫,因为有点纠结什么时候更新自己,什么时候更新子节点。而且,我更新的时候都是在中间更新的,兴海说必须开始就更新,因为下面计算就要用到了。抛弃了以前做hotel看别人做的那种方法,不需要cover标记,直接计算,也很方便。


这个异或操作就是节点记录是否需要异或,然后利用 lazy 思想往子节点传递异或标记,并且更新子节点。


#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const int MAX = 100010;
struct Tnode{ int l,r,sum;bool cover;};
Tnode node[MAX << 4];

void init()
{
	memset(node,0,sizeof(node));
}

void Build(int t,int l,int r)
{
	node[t].l = l; node[t].r = r; node[t].cover = 0;
	if( node[t].l == node[t].r - 1 ) return ;
	int mid = MID(l,r);
	Build(L(t),l,mid);
	Build(R(t),mid,r);
}
void Updata_len(int t)
{
	node[t].sum = node[t].r - node[t].l - node[t].sum;
}
void Pushdown(int t)
{
	if( node[t].cover )
	{
		node[L(t)].cover = !node[L(t)].cover;
		node[R(t)].cover = !node[R(t)].cover;
		node[t].cover = !node[t].cover;
		Updata_len(L(t));
		Updata_len(R(t));
	}
}
void Updata(int t,int l,int r)
{
	Pushdown(t);
	if( node[t].l >= l && node[t].r <= r )
	{
		node[t].cover = !node[t].cover;
		node[t].sum = node[t].r - node[t].l - node[t].sum;
		return ;
	}
	if( node[t].l == node[t].r - 1 ) return ;
	
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		Updata(R(t),l,r);
	else
		if( r < mid )
			Updata(L(t),l,r);
		else
		{
			Updata(L(t),l,mid);
			Updata(R(t),mid,r);
		}
	node[t].sum = node[L(t)].sum + node[R(t)].sum;
}

int Query(int t,int l,int r)
{
	Pushdown(t);
	if( node[t].l >= l && node[t].r <= r )
		return node[t].sum;	
	if( node[t].l == node[t].r - 1 ) return 0;
	int mid = MID(node[t].l,node[t].r);
	int ans = 0;
	if( l >= mid )
		ans = Query(R(t),l,r);
	else
		if( r < mid )
			ans = Query(L(t),l,r);
		else
			ans = Query(L(t),l,mid) + Query(R(t),mid,r);
	return ans;
}

int main()
{
	int n,m,ind,x,y;
	while( ~scanf("%d%d",&n,&m) )
	{
		init();
		Build(1,0,n);
		while( m-- )
		{
			scanf("%d%d%d",&ind,&x,&y);
			if( ind == 0 )
				Updata(1,x-1,y);
			else
			{
				int ans = Query(1,x-1,y);
				printf("%d\n",ans);
			}
		}
	}

return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值