【BZOJ 1230】 开关灯
【题目链接】
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】 开关灯的更多相关文章
- BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )
线段树.. --------------------------------------------------------------------------------- #include< ...
- BZOJ 1230 [Usaco2008 Nov]lites 开关灯:线段树异或
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1230 题意: 有n盏灯,一开始全是关着的. 有m次操作(p,a,b).p为0,则将区间[a ...
- bzoj:1230: [Usaco2008 Nov]lites 开关灯
Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...
- bzoj 1230: [Usaco2008 Nov]lites 开关灯【线段树】
在线段树上记录长度.区间01翻转标记.当前1个数.传递tag的时候用长度-1个数即可 #include<iostream> #include<cstdio> using nam ...
- BZOJ 1230 Usaco2008 Nov 开关灯
[题意概述] 给出一个01序列,初始时序列全为0,每次有修改操作或询问操作,修改操作要求把L~R区间中的0变成1,1变成0,查询操作要求输出L~R区间的1的个数 [题解] 线段树. 每次区间修改把区间 ...
- BZOJ 1230 Usaco2008 Nov 开关灯 线段树
思路: 用线段树模拟题中的操作就好 (标记异或 长度=区间总长度-当前已开灯的长度) //By SiriusRen #include <cstdio> using namespace st ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- USACO 刷题记录bzoj
bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...
- 1230: [Usaco2008 Nov]lites 开关灯
1230: [Usaco2008 Nov]lites 开关灯 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1162 Solved: 589[Sub ...
随机推荐
- CSS——padding
padding是盒子内容与边框的距离. padding:10px;/*上下左右都是10px*/ padding:10px 20px;/*上下是10px 左右是20px*/ padding:10px 2 ...
- 使用xml实现的增删改查功能
实体类: package vo; public class Contact { private String id; private String name; private String gende ...
- 【sqli-labs】 less44 POST -Error based -String -Stacked Blind(POST型基于盲注的堆叠字符型注入)
盲注漏洞,登陆失败和注入失败显示的同一个页面 可以用sleep函数通过延时判断是否闭合引号成功 这个方法有一点不好的地方在于,并不能去控制延时,延时的时间取决于users表中的数据数量和sleep函数 ...
- Nginx 反向代理并缓存及缓存清除
Nginx 反向代理并缓存及缓存清除 原文地址:http://www.cnblogs.com/caoguo/p/5012447.html 一. Nginx 配置 #user nobody; worke ...
- Apache 在Linux上的安装
1.获取源码 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz 2.卸载centos自带的apache 3.解压apach ...
- table头部固定,内容滚动
可以设置两个table,th,td得设置宽度: <table> <thead> <tr><th></th&g ...
- cshtml中字符串中表示特殊字符@
用“@@”表示字符串中的特殊字符@
- Win32 线程同步
Win32 线程同步 ## Win32线程同步 ### 1. 原子锁 ### 2. 临界区 {全局变量} CRITICAL_SECTION CS = {0}; // 定义并初始化临界区结构体变量 {线 ...
- uva 1585 Score(Uva-1585)
vj:https://vjudge.net/problem/UVA-1585 不多说水题一个o一直加x就加的变为0 我的代码 #include <iostream> #include &l ...
- B树、B+树
when ? why ? how ? what ? 平衡二叉树其查找的时间复杂度是 O(log2N)与树的深度相关,那么降低树的深度自然会提高查找效率. 如果我们要操作的数据集非常大,大到内存已经没法 ...