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. Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp

    https://www.garron.me/en/linux/set-time-date-timezone-ntp-linux-shell-gnome-command-line.html Set ti ...

  2. HDU 4502

    直接贪心就好. #include <iostream> #include <cstdio> #include <algorithm> #include <cs ...

  3. Squares-暴力枚举或者二分

    B - Squares Time Limit:3500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  4. C#实体转换

    using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using S ...

  5. luogu2014 选课 背包类树形DP

    题目大意:有N门功课,每门课有个学分,每门课有一门或没有直接先修课(若课程a是课程b的先修课即只有学完了课程a,才能学习课程b).一个学生要从这些课程里选择M门课程学习,问他能获得的最大学分是多少? ...

  6. Ubuntu使用ssh方法连接不上

    查看远程机器是否安装ssh服务(ubuntu 没有默认安装ssh) 命令:ssh host  (图中没有安装) 安装ssh  命令 sudo apt-get install openssh-serve ...

  7. 获取android的SDK或者手机目录路径

    获取android的SDK或者手机目录路径 Google为我们提供了API来获取SDK或者手机目录路径: 1.获取SD卡目录 File file1 = Environment.getExternalS ...

  8. NET中的池

    NET中的各种池 在.NET中,常用到的池有四个:字符串拘留池.线程池 .应用程序池.数据库连接池. 字符串拘留池 在.NET中字符串是不可变对象,修改字符串变量的值会产生新的对象.为降低性能消耗及减 ...

  9. 关于Mybatis的几个问题

    今天在用mybatis开发的时候遇到两个问题,下面一一列出并给出解决方案. 问题一 最开始我设置的实体类中有个字段如isParent为boolean类型,set和get方法是eclispe自动生成的. ...

  10. HTML中href、src区别

    href是Hypertext Reference的缩写,表示超文本引用.用来建立当前元素和文档之间的链接.常用的有:link.a.例如: <link href="reset.css&q ...