[TJOI2009]开关

题目描述

现有N(2 ≤ N ≤ 100000)盏灯排成一排,从左到右依次编号为:1,2,......,N。然后依次执行M(1 ≤ M ≤ 100000)项操作,操作分为两种:第一种操作指定一个区间[a, b],然后改变编号在这个区间内的灯的状态(把开着的灯关上,关着的灯打开),第二种操作是指定一个区间[a, b],要求你输出这个区间内有多少盏灯是打开的。灯在初始时都是关着的。

输入输出格式

输入格式:

第一行有两个整数N和M,分别表示灯的数目和操作的数目。接下来有M行,每行有三个整数,依次为:c, a, b。其中c表示操作的种类,当c的值为0时,表示是第一种操作。当c的值为1时表示是第二种操作。a和b则分别表示了操作区间的左右边界(1 ≤ a ≤ b ≤ N)。

输出格式:

每当遇到第二种操作时,输出一行,包含一个整数:此时在查询的区间中打开的灯的数目。

输入输出样例

输入样例#1: 
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
输出样例#1: 
 1
 2
 

Solution

很典型的线段树区间反转.

即用线段树维护每一个点的操作次数,如果%2==1 那么就是开着的.

否则,则为关着的.

不过这个版本的是用的结构体储存了一下每个线段树节点的关的点数和开得点数.

如果需要操作就把当前开的和关的数量交换.

然后就是基本的操作.

 1 #include<bits/stdc++.h>
 2 #define maxn 100005
 3 #define ll(x) x*2
 4 #define rr(x) x*2+1
 5 using namespace std;
 6 struct tree{ 
 7 int l,r,s0,s1,f; 
 8 }t[maxn*4];
 9 int n,m;
10 int read()
11 {
12     int x=0; char ch=getchar();
13     while (!isdigit(ch)) ch=getchar();
14     while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
15     return x;
16 }
17 void build(int x,int l,int r)
18 {
19     t[x]=(tree){l,r,r-l+1,0,0};
20     if (l==r) return; int mid=l+r>>1;
21     build(ll(x),l,mid),build(rr(x),mid+1,r);
22 }
23 void push_down(int x)
24 {
25     swap(t[ll(x)].s0,t[ll(x)].s1);
26     swap(t[rr(x)].s0,t[rr(x)].s1);
27     t[x].f^=1,t[ll(x)].f^=1,t[rr(x)].f^=1;
28 }
29 void mdfy(int x,int l,int r)
30 {
31     if (t[x].l>r||t[x].r<l) return;
32     if (t[x].l>=l&&t[x].r<=r){
33         t[x].f^=1,swap(t[x].s0,t[x].s1);
34         return;
35     }
36     if (t[x].f) push_down(x);
37     mdfy(ll(x),l,r),mdfy(rr(x),l,r);
38     t[x].s0=t[ll(x)].s0+t[rr(x)].s0;
39     t[x].s1=t[ll(x)].s1+t[rr(x)].s1;
40 }
41 int srch(int x,int l,int r){
42     if (t[x].l>r||t[x].r<l) return 0;
43     if (t[x].l>=l&&t[x].r<=r) return t[x].s1;
44     if (t[x].f) push_down(x);
45     return srch(ll(x),l,r)+srch(rr(x),l,r);
46 }
47 int main(){
48     n=read(),m=read(),build(1,1,n);
49     while (m--){
50         int f=read(),l=read(),r=read();
51         if (f) cout<<srch(1,l,r)<<endl;
52         else mdfy(1,l,r);
53     }
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/Kv-Stalin/p/8992689.html