BZOJ1230 [Usaco2008 Nov]lites 开关灯
区间not,求区间1的个数。。。线段树裸题
然而窝并不会线段树
我们可以对序列分块,每个块记录0/1的个数和tag表示又没有区间not过就好了
/**************************************************************
Problem: 1230
User: rausen
Language: C++
Result: Accepted
Time:796 ms
Memory:1608 kb
****************************************************************/ #include <cstdio>
#include <cmath>
#include <algorithm> using namespace std;
const int N = 1e5 + ;
const int SZ = ; int n, m;
int a[N];
int sz, b[N], st[SZ], cnt[SZ][], tag[SZ], cnt_block; inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
} inline void pre() {
int i;
sz = sqrt(n);
if (!sz) sz = ;
for (i = ; i <= n; ++i) {
if (i % sz == || sz == ) st[++cnt_block] = i;
b[i] = cnt_block, ++cnt[cnt_block][];
}
st[cnt_block + ] = n + ;
} inline void push_tag(int B) {
static int i;
if (!tag[B]) return;
for (i = st[B]; i < st[B + ]; ++i)
a[i] = !a[i];
swap(cnt[B][], cnt[B][]);
tag[B] = ;
} inline void change(int x, int y) {
static int i, t1, t2;
t1 = b[x], t2 = b[y];
for (i = t1 + ; i < t2; ++i) tag[i] = !tag[i];
if (t1 == t2)
for (push_tag(t1), i = x; i <= y; ++i)
--cnt[t1][a[i]], a[i] = !a[i], ++cnt[t1][a[i]];
else {
for (push_tag(t1), i = x; i < st[t1 + ]; ++i)
--cnt[t1][a[i]], a[i] = !a[i], ++cnt[t1][a[i]];
for (push_tag(t2), i = st[t2]; i <= y; ++i)
--cnt[t2][a[i]], a[i] = !a[i], ++cnt[t2][a[i]];
}
} inline int query(int x, int y) {
static int i, t1, t2, res;
t1 = b[x], t2 = b[y], res = ;
for (i = t1 + ; i < t2; ++i)
res += cnt[i][!tag[i]];
if (t1 == t2)
for (push_tag(t1), i = x; i <= y; ++i) res += a[i];
else {
for (push_tag(t1), i = x; i < st[t1 + ]; ++i) res += a[i];
for (push_tag(t2), i = st[t2]; i <= y; ++i) res += a[i];
}
return res;
} int main() {
int i, oper, x, y;
n = read(), m = read();
pre();
for (i = ; i <= m; ++i) {
oper = read(), x = read(), y = read();
if (oper == ) change(x, y);
else printf("%d\n", query(x, y));
}
return ;
}
BZOJ1230 [Usaco2008 Nov]lites 开关灯的更多相关文章
- 【线段树】Bzoj1230 [Usaco2008 Nov]lites 开关灯
Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...
- BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )
线段树.. --------------------------------------------------------------------------------- #include< ...
- 1230: [Usaco2008 Nov]lites 开关灯
1230: [Usaco2008 Nov]lites 开关灯 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1162 Solved: 589[Sub ...
- P1230: [Usaco2008 Nov]lites 开关灯
嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,upda ...
- bzoj:1230: [Usaco2008 Nov]lites 开关灯
Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...
- 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 开关灯【线段树】
在线段树上记录长度.区间01翻转标记.当前1个数.传递tag的时候用长度-1个数即可 #include<iostream> #include<cstdio> using nam ...
- B1230 [Usaco2008 Nov]lites 开关灯 线段树
就是线段树维护异或和.之前我线段树区间修改down都是修改当前区间,结果debug出不来,改成每次向下了. 题干: Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶 ...
- [Usaco2008 Nov]mixup2 混乱的奶牛 简单状压DP
1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 685 Solved: 383[S ...
随机推荐
- plot bar chart using python
Example import matplotlib.pyplot as plt import plotly.plotly as py # Learn about API authentication ...
- 【BZOJ】4636: 蒟蒻的数列
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 145 Solved: 71[Submit][Status][Discuss] ...
- Windows Internals学习笔记(二)系统架构
参考资料: 1. <Windows Internals> 2. http://bestcbooks.com 3. Windows Drive Kit 4. Microsoft Window ...
- linux 查看是否安装perl模块
这里介绍两种linux中查看perl模块是否安装的方法,一种是对于单体的模块,一种是对于群体的. 单体验证: [root@root ~]# perl -MShell -e "print\&q ...
- git学习笔记02-创建一个仓库提交一个文件-原来就是这么简单
打开安装好的git bash,设置你的git信息 (这个随便写就行) 初始化一个Git仓库,分三步.(创建文件夹也可以手动创建,也可以命令行创建) 第一步,进到一个目录 cd e: 第二步,创建一 ...
- iOS - OC NSString 字符串
前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...
- ubuntu_ar命令(操作 ?.deb文件)
1. -x 从自备存文件中取出成员文件. 1.1. “root@zc33-desktop:/home# ar -x libstdc++6_4.7.2-5_i386.deb” 这样的话,它会把 lib ...
- Windows_CMD_临时环境变量
1. 以 path 为例: 1.1.查看: set path 1.2.添加: set path=%path%;"要添加的路径" 附录:下面贴上一些常用的环境变量及作用 %ALLUS ...
- (转)使用Jmeter进行http接口测试
前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...
- linux之echo命令
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个提示 ...