Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 200000 + 4;
int lazy[maxn << 2], sumv[maxn << 2];
inline void pushdown(int o, int l,int r)
{
int ls = (o << 1), rs = (o << 1)|1, mid = (l + r) >> 1;
if(lazy[o])
{
lazy[ls] ^= 1, lazy[rs] ^= 1;
if(ls) sumv[ls] = (mid - l + 1) - sumv[ls];
if(rs) sumv[rs] = (r - mid) - sumv[rs];
lazy[o] = 0;
}
}
inline void pushup(int o){ sumv[o] = sumv[(o << 1)] + sumv[(o << 1)|1]; }
void update(int l,int r,int L,int R,int o)
{
if(r < L || l > R || l > r) return ;
if(l >= L && r <= R)
{
lazy[o] ^= 1, sumv[o] = (r - l + 1) - sumv[o];
return ;
}
int mid = (l + r) >> 1, ls = (o<<1), rs = (o<<1)|1;
pushdown(o, l, r);
update(l,mid, L,R,ls);
update(mid + 1, r, L, R ,rs);
pushup(o);
}
int query(int l,int r, int L,int R,int o)
{
if(l > r || r < L || l > R) return 0;
if(l >= L && r <= R) return sumv[o];
int mid = (l + r) >> 1, ls = (o << 1), rs = (o << 1)|1;
pushdown(o, l, r);
int tmp = 0;
tmp += query(l, mid, L, R, ls);
tmp += query(mid + 1, r, L, R, rs);
return tmp;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= m; ++i)
{
int ops, a, b;
scanf("%d%d%d",&ops,&a,&b);
if(ops == 0)update(1, n, a, b, 1);
if(ops == 1)printf("%d\n", query(1, n, a, b, 1));
}
return 0;
}

USACO 2008 Nov Gold 3.Light Switching 线段树的更多相关文章

  1. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

  2. [USACO 2011 Nov Gold] Above the Median【逆序对】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...

  3. 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节

    意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O ...

  4. BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer

    [题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...

  5. USACO 2008 November Gold Cheering up the Cows /// MST oj24381

    题目大意: 输入n,p:n个点,p条路 接下来n行输入c[]:在各个点需要花费的时间 接下来p行输入u,v,w:u点到v点的路需要花费时间w 求经过所有点且最后回到起点的最少花费时间 https:// ...

  6. 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群

    题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...

  7. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  8. USACO 2017 FEB Platinum mincross 可持久化线段树

    题意 上下有两个位置分别对应的序列A.B,长度为n,两序列为n的一个排列.当Ai == Bj时,上下会连一条边.你可以选择序列A或者序列B进行旋转任意K步,如 3 4 1 5 2 旋转两步为 5 2 ...

  9. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

随机推荐

  1. ISAP 算法的学习

    http://www.renfei.org/blog/isap.html 算法与数学 网络流-最大流问题 ISAP 算法解释 2013-08-07Renfei Song 2 条评论 内容提要 [隐藏] ...

  2. asp.net常用容器

    autofac就是ioc的第三方的IOC容器 unity也是IOC容器 掌握这两个容器就可以了,非常简单

  3. Eclipse全局搜索

    按[Ctrl]+[H] 搜索时支持一些正则表达式. 参考: http://blog.csdn.net/huaweitman/article/details/38709323

  4. [React Native] Target both iPhone and iPad with React Native

    By default, React Native only targets iPhone - so if you run on an iPad, it will show up as a scaled ...

  5. 一个简单的演示用的Linux字符设备驱动程序

    实现如下的功能:--字符设备驱动程序的结构及驱动程序需要实现的系统调用--可以使用cat命令或者自编的readtest命令读出"设备"里的内容--以8139网卡为例,演示了I/O端 ...

  6. 0x53 区间DP

    石子合并 搞笑 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib& ...

  7. ClassLoader.getResourceAsStream(name);获取配置文件的方法

    ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+" ...

  8. [.Net] C# Excel操作类 ExcelHelper

    实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...

  9. putty和xshell使用和免密登录

    putty和xshell使用和免密登录 XSHELL的设置 事前:我们先去关闭防火墙和selinux 关闭防火墙:   ufw disable 再去看看selinux 一.查看SELinux状态命令: ...

  10. MySql数据库中乱码问题解决方案

    show variables like 'character%';    //查看当前各系统位置的字符编码格式 问题一: Incorrect string value: '\xB4\xF3\xB4\x ...