题目描述

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

Solution:

  本题比较水(是真的很水)。

  和以前做过的某道好像叫做开关的题目一模一样。

线段树维护区间和,区间修改时等价于区间取反,$sum$变为长度减去原来的和,打个异或懒惰标记(两次操作等于没有操作)。

  练手瞎搞一弃就好了。

代码:

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define il inline
using namespace std; const int N=;
int n,m,sum[N*],lazy[N*]; il int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
} il void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];} il void pushdown(int rt,int len){
if(lazy[rt]){
lazy[rt<<]^=;lazy[rt<<|]^=;
sum[rt<<]=(len-(len>>))-sum[rt<<];
sum[rt<<|]=(len>>)-sum[rt<<|];
lazy[rt]=;
}
} il void update(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){sum[rt]=(r-l+)-sum[rt];lazy[rt]^=;return;}
pushdown(rt,r-l+);
int m=l+r>>;
if(L<=m)update(L,R,lson);
if(R>m)update(L,R,rson);
pushup(rt);
} il int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r)return sum[rt];
pushdown(rt,r-l+);
int m=l+r>>,ret=;
if(L<=m)ret+=query(L,R,lson);
if(R>m)ret+=query(L,R,rson);
return ret;
} int main(){
n=gi(),m=gi();
int f,x,y;
while(m--){
f=gi(),x=gi(),y=gi();
if(!f)update(x,y,,n,);
else printf("%d\n",query(x,y,,n,));
}
return ;
}

P2846 [USACO08NOV]光开关Light Switching的更多相关文章

  1. 洛谷——P2846 [USACO08NOV]光开关Light Switching

    P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...

  2. 洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]

    P2826 [USACO08NOV]光开关Light Switching 题目描述 Farmer John tries to keep the cows sharp by letting them p ...

  3. LuoguP2846[USACO08NOV]光开关Light Switching【线段树维护区间异或】By cellur925

    题目传送门 题目大意,给你一串灯,按一下开关可以将灯的状态取反(开变成关,关变成开).维护这个序列的两种操作:询问区间内有多少灯是开着的,区间按灯. 开始想的是分别维护区间内0的数量,1的数量,两个懒 ...

  4. 洛谷P2846 光开关Light Switching

    题目描述 灯是由高科技--外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右击两个灯所连的鼠 标,你就可以知道这两个灯,以及之间的灯有多少灯是亮的.起初 ...

  5. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  6. POJ 3533 Light Switching Game(三维Nim积)题解

    思路:三维Nim积 代码: #include<set> #include<map> #include<stack> #include<cmath> #i ...

  7. POJ 3553 Light Switching Game 博弈论 nim积 sg函数

    http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...

  8. USACO 2008 Nov Gold 3.Light Switching 线段树

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...

  9. Light Switching(SPOJ LITE)—— 线段树成段更新异或值

    题目连接:http://www.spoj.com/problems/LITE/en/. 题意:有若干个灯泡,每次对一段操作,这一段原先是亮的,就关了:原先是关着的,就打开.询问某一段的打开的灯泡的个数 ...

随机推荐

  1. 2017.12.10 Java写一个杨辉三角(二维数组的应用)

    杨辉三角的定律 第n行m列元素通项公式为: C(n-1,m-1)=(n-1)!/[(m-1)!(n-m)!] 需要用到创建二维数组 package com.glut.demo; /** * 杨辉三角 ...

  2. python_77_json与pickle序列化3

    #此方法:dump多次,而不可以load多次,只能load一次,否则会出错 只有序列化,无反序列化 import json info={ 'name':'Xue Jingjie', 'age':22, ...

  3. python_28_dictionary补充

    #update:合并两个字典,如果有交叉就覆盖更新,没有交叉的就创建 info={ 'stu1101':'Liu Guannan', 'stu1102':'Wang Ruipu', 'stu1103' ...

  4. Spring学习记录(三)

    一.AOP的整理总结 aop面向切面编程 横向重复代码,纵向抽取 动态代理 1.通过动态代理可以体现aop思想 2.为什么要哦用动态代理:对目标对象中的方法进行增强 spring aop开发 spri ...

  5. C# 运用StreamReader类和StreamWriter类实现文件的读写操作

    对文件的读写操作应该是最重要的文件操作,System.IO命名空间为我们提供了诸多文件读写操作类,在这里我要向大家介绍最常用也是最基本的StreamReader类和StreamWriter类.从这两个 ...

  6. java基础面试题:switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

    package com.swift; public class Switch_Test { public static void main(String[] args) { /* * switch语句 ...

  7. JavaScript 交换两个变量的值

    方法一 let a = "a", b = "b"; console.log(a, b); let t = a; a = b; b = t; console.lo ...

  8. package.json字段分析

    分析1.必须在包的顶层目录下2.二进制文件应该在bin目录下3.javascipt在lib目录下4.文档在doc目录下 package.json字段分析 name:包的名称,必须是唯一的,由小写英文字 ...

  9. Qt之QThread随记

    这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...

  10. 【转载】SQLServer中char、varchar、nchar、nvarchar的区别:

    (1)       定义: char:    固定长度,存储ANSI字符,不足的补英文半角空格. nchar:   固定长度,存储Unicode字符,不足的补英文半角空格 varchar:  可变长度 ...