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

来源:互联网 发布:淘客cms哪个好 编辑:程序博客网 时间:2024/05/08 02:36

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 lightsoff, and we use ‘1’ represented the light on, use ‘0’ represented the light off. As we know, there is a cat indremone’s house, and she is very naught. She always change the status continuously from Ath light to Bthone. (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 onfrom 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 soon. Then we assume the first operation that we change the status from 2nd to 4th, and then the status will be:1 1 0 0Then there are 2 lights on from 1st light to 4th one, 1 light on from 2nd to 4th one and so on. Then the secondoperation 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 Twill 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’. IfX=’Q’, you will be output the numbers of lights on from Cth to Dth, and if X=’C’, you will be change thestatus 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 ablank line after you have processed a test case.

Sample Input

24 31 0 1 1Q 2 4C 2 3Q 1 44 11 0 1 1Q 2 4

Sample Output

232
//题意:给出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|1struct 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);elseprintf("%d\n",query(1,l,r));}puts("");}return 0;}