USACO 2008 Nov Gold 3.Light Switching 线段树
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 线段树的更多相关文章
- [USACO 2011 Nov Gold] Cow Steeplechase【二分图】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...
- [USACO 2011 Nov Gold] Above the Median【逆序对】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...
- 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节
意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O ...
- BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer
[题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...
- USACO 2008 November Gold Cheering up the Cows /// MST oj24381
题目大意: 输入n,p:n个点,p条路 接下来n行输入c[]:在各个点需要花费的时间 接下来p行输入u,v,w:u点到v点的路需要花费时间w 求经过所有点且最后回到起点的最少花费时间 https:// ...
- 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群
题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- USACO 2017 FEB Platinum mincross 可持久化线段树
题意 上下有两个位置分别对应的序列A.B,长度为n,两序列为n的一个排列.当Ai == Bj时,上下会连一条边.你可以选择序列A或者序列B进行旋转任意K步,如 3 4 1 5 2 旋转两步为 5 2 ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
随机推荐
- ACDream - Crayon
题目: Description There are only one case in each input file, the first line is a integer N (N ≤ 1,000 ...
- 转载 - KMP算法
出处:http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html KMP算法 在介绍KMP算法之前,先介绍一下BF算法. 一. ...
- 【日常学习】【搜索/递归】codevs2802 二的幂次方题解
转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 题目描写叙述 Description 不论什么一个正整数都能够用2的幂次方表示. 比如:13 ...
- S 禁止F12和右键操作控制台,兼容各浏览器
document.oncontextmenu = function () { return false; }; document.onkeydown = function () { ...
- 【cl】maven新建web项目
我的环境:window32位 +jdk1.7.0_75+maven3.3.3 1.ctrl+N 新建maven project项目 2.选择maven-archetype-webapp [这里一开始用 ...
- 调试中的step into step over step out
step into/step out/step over的差别 step into就是单步运行,遇到子函数就进入而且继续单步运行: step over是在单步运行时,在函数内遇到子函数时不会进入子函数 ...
- python实现高速排序算法(两种不同实现方式)
# -*- coding: utf-8 -*- """ Created on Fri May 16 17:24:05 2014 @author: lifeix " ...
- MySQL调优 —— Using temporary
DBA发来一个线上慢查询问题. SQL例如以下(为突出重点省略部分内容): select distinct article0_.id, 等字段 from article_table article ...
- adb命令--之查看进程及Kill进程
adb shell kill [PID] //杀死进程 adb 命令查看程序进程方便简洁高效 adb shell ps //查看所有进程列表,Process Status ad ...
- 框架:Rureka
ylbtech-框架:Rureka Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.S ...