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 ...
随机推荐
- Vue packages version mismatch
开发过程中,之前做的vue项目,一段时间后拿出来重新运行,报错: 打开vue-template-compiler/index.js查看错误提示,如下: 当安装的vue版本和package.json中的 ...
- 洛谷 P2813 母舰
题目描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普通的MA(Mobile Armor)无法比较的. 对于一艘母舰而言,它是由若干个攻击系统和若干个防御系统组 ...
- codeforces 257B
#include<stdio.h> #define mod 1000000007 int main() { __int64 n,m,i,j,k,x,y,z; while(scanf(&qu ...
- 服务器session,Tomcat有自己的session维护机制,apache有自己的session维护机制
1.SESSION一般不是你说的这种工作方式,你打开一个浏览器,再打开一个,请求同一个网址,然后其中一个登陆,另外一个绝对不会也登陆.SESSION和浏览器本身这个程序就是挂钩的,一般不通过IP和端口 ...
- [Cypress] Test React’s Controlled Input with Cypress Selector Playground
React based applications often use controlled inputs, meaning the input event leads to the applicati ...
- 开源项目 apk cfg and android app path profiling
暑假里面完毕的一个小项目,limitation还是挺多的. 期待未来有更大的motivation 去完好它.通过此次的项目设计,对于smali的语法更加了解了,对于进一步学习android app的安 ...
- 【待解决】An internal error occurred during: "Launching baiduTest1". java.lang.NullPointerException
编写的一个Java类,以junit运行可以正常执行,以testNG执行就报如下错误 解决方法:
- PHP自己定义函数及数组
个人原创博客:http://www.phpthinking.com/archives/350 一.自己定义函数 自己定义函数就是我们自己定义的函数.在PHP中自己定义函数格式例如以下: 1 funct ...
- jsp页面自动换行
其实只要在表格控制中添加一句 <div style="word-break:break-all">就搞定了.</div>其中可能对英文换行可能会分开一个单词 ...
- hdoj--3339--In Action(最短路+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...