学院OJ:1684:Light on or off __线段树 区间更新

http://220.166.52.162/oj/showproblem?problem_id=1684

Description

There is N lights on the wall of Dreamone’s house from left to right.Initially,some lights on and some lights
off, and we use ‘1’ represented the light on, use ‘0’ represented the light off. As we know, there is a cat in
dremone’s house, and she is very naught. She always change the status continuously from Ath light to Bth
one. (1<=A, B<=N).If the light is on, then the light will be off, If the light is off, then the light will be on.
Actually, she can do it K times like this. Then the cat puts forward another problem: How many lights on
from Cth light to Dth (1<=C, D<=N) one? For example:
When N=4 K=2, and the initial status is assumed as:
1 0 1 1
‘1’ represented on,’0’ represented off.
From the initial status we can get: There is 3 lights on from 1st light to 4th, 2 lights on from 2nd to 4th and so
on. Then we assume the first operation that we change the status from 2nd to 4th, and then the status will be:
1 1 0 0
Then there are 2 lights on from 1st light to 4th one, 1 light on from 2nd to 4th one and so on. Then the second
operation is assumed as from the 1st to 2nd .Then the status will be 0 0 0 0. And there will be no lights on.
Can you get the main idea? Can you help the naught cat?

Input

The first line of input will be a positive integer indicating how many test cases will be included (T) and T
will be less than 10. Each of the next T cases will contain two parts:
The first part: two integer N, K (1<=N<=100000, 1<=K<=100000)
The second part: N numbers (which is ‘0’ or ‘1’) represented the initial status from left to right.
Then third part: K lines. Each line will be X C D (1<=C, D<=N) X is a letter which is either ‘Q’ or ‘C’. If
X=’Q’, you will be output the numbers of lights on from Cth to Dth, and if X=’C’, you will be change the
status of lights as the rules described above.

Output

For each query,(when X=’Q’),you should output the numbers of lights on. What’s more, you must output a
blank line after you have processed a test case.

Sample Input

2

4 3

1 0 1 1

Q 2 4

C 2 3

Q 1 4

4 1

1 0 1 1

Q 2 4

Sample Output

2

3



2

//题意:给出n个灯的状态(1\0表示开\关),并给出m个操作

Q  x  y:询问[x,y]中亮着的等的个数;

C  x  y:将[x,y]内所有等状态取反。

//分析:典型的线段树区间更新操作。 对于题目中要求的亮着的灯的个数即1的个数其实也就是区间[x,y]所有数之和。 所以可以用num来记录区间数字总和,而更新操作中对给定区间所有的数取反(其实需要关心的是取反后亮着的灯的个数)操作与num值是否有关系? 分析知:区间长度-num=(取反后亮着的灯的个数) ,建立这样的关系后其实就是更新/求区间和的线段树操作了!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 100005
#define L(p) p<<1        //最近写线段树经常将p<<1和p<<1|1写反或写错掉,用宏定义后,L,R两个点亮的字母便容易区分多了,减少发生错误。
#define R(p) p<<1|1

struct node
{
	int l,r,num;
	bool lazy;
	int Len() { return r-l+1; }
	int Mid() { return (l+r)>>1; }
}tree[3*maxn];
int a[maxn];

void Lazy(int p)
{
	tree[L(p)].lazy=!tree[L(p)].lazy;
	tree[L(p)].num=tree[L(p)].Len()-tree[L(p)].num;
	
	tree[R(p)].lazy=!tree[R(p)].lazy;
	tree[R(p)].num=tree[R(p)].Len()-tree[R(p)].num;
	tree[p].lazy=false;
}



void BuildTree(int p,int l,int r)
{
	tree[p].l=l,tree[p].r=r,tree[p].lazy=false;
	if(l==r)
	{
		tree[p].num=a[l];
		return;
	}
	int mid=tree[p].Mid();
	BuildTree(L(p),l,mid);
	BuildTree(R(p),mid+1,r);
	tree[p].num=tree[L(p)].num+tree[R(p)].num;
}

void change(int p,int l,int r)
{
	if(tree[p].l==l&&tree[p].r==r)
	{
		tree[p].lazy=!tree[p].lazy;
		tree[p].num=tree[p].Len()-tree[p].num;  //更新区间灯亮的个数
		return;
	}
	if(tree[p].lazy)
		Lazy(p);
	int mid=tree[p].Mid();
	if(r<=mid)
		change(L(p),l,r);
	else if(l>mid)
		change(R(p),l,r);
	else
	{
		change(L(p),l,mid);
		change(R(p),mid+1,r);
	}
	tree[p].num=tree[L(p)].num+tree[R(p)].num;
}

int query(int p,int l,int r)
{
	if(tree[p].l==l&&tree[p].r==r)
		return tree[p].num;
	if(tree[p].lazy)
		Lazy(p);
	int mid=tree[p].Mid();
	if(r<=mid)
		return query(L(p),l,r);
	else if(l>mid)
		return query(R(p),l,r);
	else return query(L(p),l,mid)+query(R(p),mid+1,r);
}

int main()
{
	int t,n,m,i;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		BuildTree(1,1,n);
		char orde[3];
		int l,r;
		while(m--)
		{
			scanf("%s%d%d",orde,&l,&r);
			if(l>r)
				swap(l,r);
			if(orde[0]=='C')
				change(1,l,r);
			else
				printf("%d\n",query(1,l,r));
		}
		puts("");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值