区间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 开关灯的更多相关文章

  1. 【线段树】Bzoj1230 [Usaco2008 Nov]lites 开关灯

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

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

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

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

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

  4. P1230: [Usaco2008 Nov]lites 开关灯

    嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,upda ...

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

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

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

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

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

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

  8. B1230 [Usaco2008 Nov]lites 开关灯 线段树

    就是线段树维护异或和.之前我线段树区间修改down都是修改当前区间,结果debug出不来,改成每次向下了. 题干: Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶 ...

  9. [Usaco2008 Nov]mixup2 混乱的奶牛 简单状压DP

    1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 685  Solved: 383[S ...

随机推荐

  1. Using SSH on Linux

    This document covers the SSH client on the Linux Operating System and other OSes that use OpenSSH. W ...

  2. 关于js运动的一些总结

    js运动实现,有两种.一种是速度版,另一种是时间版. 速度版是通过对速度的加减乘除,得出元素的运动数据.时间版是通过对时间进行Tween公式运算,得出元素的运动数据. 速度版运动优点:容易在运动过程中 ...

  3. mfc 可编辑 list control

    维护到一个古老的gm工具的时候 需要这个功能 在网上找到一份很好用的代码 贴到这里 再次感谢那位同僚 #pragma once //#include "OrangeMessage.h&quo ...

  4. HDU-4521 小明系列问题——小明序列 间隔限制最长上升子序列

    题意:给定一个长度为N的序列,现在要求给出一个最长的序列满足序列中的元素严格上升并且相邻两个数字的下标间隔要严格大于d. 分析: 1.线段树 由于给定的元素的取值范围为0-10^5,因此维护一棵线段树 ...

  5. Oracle一列的多行数据拼成一行显示字符

    Oracle一列的多行数据拼成一行显示字符   oracle 提供了两个函数WMSYS.WM_CONCAT 和 ListAgg函数.    www.2cto.com   先介绍:WMSYS.WM_CO ...

  6. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  7. iOS之Scanning的实现

    http://i.cnblogs.com/EditPosts.aspx?postid=5288517 //写在最前 /* AVFoundation原生框架的好处就是扫描特别快效率特别高,但是可能会遇到 ...

  8. [css] 【转载】 精简高效的CSS命名准则/方法

    原文链接:http://www.zhangxinxu.com/wordpress/2010/09/%E7%B2%BE%E7%AE%80%E9%AB%98%E6%95%88%E7%9A%84css%E5 ...

  9. [js] 非常好的面试题,学习了。。

    原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html

  10. neon汇编,C嵌入汇编

    1. Neon-intrics.pdf中的 void vst2q_u16(__transfersize(16) uint16_t * ptr, uint16x8x2_t val);// VST2.16 ...