【题目链接】

https://www.lydsy.com/JudgeOnline/problem.php?id=1230

【算法】

线段树

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 int n,m,l,r,opt; struct Node
{
int l,r,sum;
bool tag;
}; class SegmentTree
{
private :
Node Tree[MAXN<<];
public :
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l; Tree[index].r = r;
Tree[index].sum = ;
Tree[index].tag = false;
if (l == r) return;
mid = (l + r) >> ;
build(index<<,l,mid);
build(index<<|,mid+,r);
}
inline void update(int index)
{
Tree[index].sum = Tree[index<<].sum + Tree[index<<|].sum;
}
inline void pushdown(int index)
{
int l = Tree[index].l,r = Tree[index].r;
int mid = (l + r) >> ;
if (Tree[index].tag)
{
Tree[index<<].sum = mid - l + - Tree[index<<].sum;
Tree[index<<|].sum = r - mid - Tree[index<<|].sum;
Tree[index<<].tag ^= ;
Tree[index<<|].tag ^= ;
Tree[index].tag = false;
}
}
inline void modify(int index,int l,int r)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].sum = r - l + - Tree[index].sum;
Tree[index].tag ^= ;
return;
} else
{
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index<<,l,r);
else if (mid + <= l) modify(index<<|,l,r);
else
{
modify(index<<,l,mid);
modify(index<<|,mid+,r);
}
update(index);
}
}
inline int query(int index,int l,int r)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum;
else
{
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index<<,l,r);
else if (mid + <= l) return query(index<<|,l,r);
else return query(index<<,l,mid) + query(index<<|,mid+,r);
}
}
} T; int main()
{ scanf("%d%d",&n,&m);
T.build(,,n);
while (m--)
{
scanf("%d%d%d",&opt,&l,&r);
if (opt == ) T.modify(,l,r);
else printf("%d\n",T.query(,l,r));
} return ; }

【BZOJ 1230】 开关灯的更多相关文章

  1. BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )

    线段树.. --------------------------------------------------------------------------------- #include< ...

  2. BZOJ 1230 [Usaco2008 Nov]lites 开关灯:线段树异或

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1230 题意: 有n盏灯,一开始全是关着的. 有m次操作(p,a,b).p为0,则将区间[a ...

  3. bzoj:1230: [Usaco2008 Nov]lites 开关灯

    Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...

  4. bzoj 1230: [Usaco2008 Nov]lites 开关灯【线段树】

    在线段树上记录长度.区间01翻转标记.当前1个数.传递tag的时候用长度-1个数即可 #include<iostream> #include<cstdio> using nam ...

  5. BZOJ 1230 Usaco2008 Nov 开关灯

    [题意概述] 给出一个01序列,初始时序列全为0,每次有修改操作或询问操作,修改操作要求把L~R区间中的0变成1,1变成0,查询操作要求输出L~R区间的1的个数 [题解] 线段树. 每次区间修改把区间 ...

  6. BZOJ 1230 Usaco2008 Nov 开关灯 线段树

    思路: 用线段树模拟题中的操作就好 (标记异或 长度=区间总长度-当前已开灯的长度) //By SiriusRen #include <cstdio> using namespace st ...

  7. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  8. USACO 刷题记录bzoj

    bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...

  9. 1230: [Usaco2008 Nov]lites 开关灯

    1230: [Usaco2008 Nov]lites 开关灯 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1162  Solved: 589[Sub ...

随机推荐

  1. ajax 实现输入提示效果

    网站主页 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  2. 10、scala面向对象编程之Trait

    1.  将trait作为接口使用 2.trait中定义具体方法 3.trait定义具体字段 4.trait中定义抽象字段 5.为实例对象混入trait 6.trait调用链 7.在trait中覆盖抽象 ...

  3. [问题记录]-技术学习-RocketMQ-全球集群部署问题

    一:问题场景 公司在部署全球的RocketMQ的时候,遇到亚洲区的服务器往欧洲区的RocketMQ发送消息失败的情况. 总共有出现两个问题 1:No Topic Route Info org.apac ...

  4. js的replace, 高亮

    ";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...

  5. day07补充-数据类型总结及拷贝

    目录 数据类型总结 按照存一个值 OR 多个值来分 按照有序 OR 无序来分 按照可变 OR 不可变来分 拷贝 && 浅拷贝 && 深拷贝&& .cop ...

  6. springboot 使用idea打包 遇到问题

    找了很久错误后来发现添加这三句话就可以了 需要在项目的pom.xml文件中加上第47-49行的3句话

  7. [luogu4054 JSOI2009] 计数问题(2D BIT)

    传送门 Solution 2D BIT模板 Code //By Menteur_Hxy #include <cmath> #include <cstdio> #include ...

  8. JavaScript学习笔记之对象

    目录 1.自定义对象 2.Array 3.Boolean 4.Date 5.Math 6.Number 7.String 8.RegExp 9.Function 10.Event 在 JavaScri ...

  9. proxy 跨域配置, 针对有axios的baseURL

    1.首先主要的config文件下的index.js中的proxytable配置 proxyTable:{ '/proxy': { target:'http://192.168.2.141:8080', ...

  10. 第三节:初识pandas之DataFrame(上)

    DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表.