Problem 11: Switching Lights [LongFan, 2008]

Farmer John tries to keep the cows sharp by letting them play with
intellectual toys. One of the larger toys is the lights in the barn.
Each of the N (2 <= N <= 500) cow stalls conveniently numbered
1..N has a colorful light above it. At the beginning of the evening, all the lights are off. The cows
control the lights with a set of N pushbutton switches that toggle
the lights; pushing switch i changes the state of light i from off
to on or from on to off. The cows read and execute a list of M (1 <= M <= 2,000) operations
expressed as one of two integers (0 <= operation <= 1). The first operation (denoted by a 0 command) includes two subsequent
integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting
switch and ending switch. They execute the operation by pushing
each pushbutton from S_i through E_i inclusive exactly once. The second operation (denoted by a 1 command) asks the cows to count
how many lights are on in the range given by two integers S_i and
E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in
which the cows should count the number of lights that are on. Help FJ ensure the cows are getting the correct answer by processing
the list and producing the proper counts. PROBLEM NAME: swtch INPUT FORMAT: * Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line represents an operation with three
space-separated integers: operation, S_i, and E_i SAMPLE INPUT (file swtch.in): 4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4 INPUT DETAILS: Four lights; five commands. Here is the sequence that should
be processed:
Lights
1 2 3 4
Init: O O O O O = off * = on
0 1 2 -> * * O O toggle lights 1 and 2
0 2 4 -> * O * *
1 2 3 -> 1 count the number of lit lights in range 2..3
0 2 4 -> * * O O toggle lights 2, 3, and 4
1 1 4 -> 2 count the number of lit lights in the range 1..4 OUTPUT FORMAT: * Lines 1..number of queries: For each output query, print the count
as an integer by itself on a single line. SAMPLE OUTPUT (file swtch.out): 1
2
就是一开始所有的灯是灭着的,然后.... 可以用线段树解决
/* ***********************************************
Author :guanjun
Created Time :2015/10/4 15:30:49
File Name :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
#define ls i<<1
#define rs i<<1|1
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
struct node{
int l,r,c;
int sum;
int dist(){
return r-l+;
}
}nod[maxn*];
void push_up(int i){
nod[i].sum=nod[ls].sum+nod[rs].sum;
}
void push_down(int i){
if(nod[i].c){
nod[ls].c^=;
nod[rs].c^=;
nod[ls].sum=nod[ls].dist()-nod[ls].sum;
nod[rs].sum=nod[rs].dist()-nod[rs].sum;
nod[i].c=;
}
}
void build(int i,int l,int r){
nod[i].l=l;
nod[i].r=r;
nod[i].c=nod[i].sum=;
if(l==r){
return ;
}
int mid=(l+r)/;
build(ls,l,mid);
build(rs,mid+,r);
push_up(i);
}
void update(int i,int l,int r){
if(nod[i].l==l&&nod[i].r==r){
nod[i].c^=;
nod[i].sum=nod[i].dist()-nod[i].sum;
return ;
}
push_down(i);
int mid=(nod[i].l+nod[i].r)/;
if(r<=mid)update(ls,l,r);
else if(l>mid)update(rs,l,r);
else {
update(ls,l,mid);
update(rs,mid+,r);
}
push_up(i);
}
int query(int i,int l,int r){
if(nod[i].l==l&&nod[i].r==r){
return nod[i].sum;
}
push_down(i);
int mid=(nod[i].l+nod[i].r)/;
//int sum=0;
if(r<=mid)return query(ls,l,r);
else if(l>mid)return query(rs,l,r);
else return query(ls,l,mid)+query(rs,mid+,r);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,m,po,x,y;
while(cin>>n>>m){
build(,,n);
for(int j=;j<=m;j++){
scanf("%d%d%d",&po,&x,&y);
if(po==)update(,x,y);
else printf("%d\n",query(,x,y));
}
}
return ;
}

 数据 http://contest.usaco.org/TESTDATA/NOV08_1.htm

usaco2008 nov 区间异或求和的更多相关文章

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

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

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

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

  3. NOJ——1669xor的难题(详细的树状数组扩展—异或求和)

    [1669] xor的难题 时间限制: 1000 ms 内存限制: 65535 K 问题描述 最近Alex学长有个问题被困扰了很久,就是有同学给他n个数,然后给你m个查询,然后每个查询给你l和r(左下 ...

  4. bzoj 2819 Nim dfn序+树状数组维护区间异或值

    题目大意 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...

  5. BZOJ 4017 小 Q 的无敌异或 ( 树状数组、区间异或和、区间异或和之和、按位计贡献思想 )

    题目链接 题意 : 中文题 分析 : 首先引入两篇写的很好的题解 题解一.题解二 听说这种和异或相关区间求和的问题都尽量按位考虑 首先第一问.按二进制位计贡献的话.那么对于第 k 位而言 其贡献 = ...

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

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

  7. [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草

    [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...

  8. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

    二分一下答案就好了... --------------------------------------------------------------------------------------- ...

  9. BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛( dp )

    状压dp dp( x , S ) 表示最后一个是 x , 当前选的奶牛集合为 S , 则状态转移方程 : dp( x , S ) =  Σ dp( i , S - { i } )  ( i ∈ S , ...

随机推荐

  1. BZOJ2654 tree 【二分 + 最小生成树】

    题目 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 输入格式 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行, ...

  2. 算法复习——欧拉函数(poj3090)

    题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or e ...

  3. 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证)

    一.前言 在各种Tomcat相关书籍,书上都提到了其类加载器结构: 在Tomcat 7或者8中,共享类和Catalina类加载器在catalina.properties中都是没配置的,请看: 所以,c ...

  4. 洛谷P2677 超级书架 2

    题目描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N & ...

  5. 【调试】JS断点调试

    1.断点调试是啥?难不难? 断点调试其实并不是多么复杂的一件事,简单的理解无外呼就是打开浏览器,打开sources找到js文件,在行号上点一下罢了.操作起来似乎很简单,其实很多人纠结的是,是在哪里打断 ...

  6. Android SurfaceView与View

    SurfaceView介绍 SurfaceView是视图(View)的继承类,这个视图里面内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸,而SurfaceView控制 ...

  7. Day 4 Linux基础

    Linux基础(指令篇) 一.Linux命令 1.Linux命令行的语法格式: 命令+选项+参数 命令:告诉Linux(UNIX)操作系统做(执行)什么. 选项:说明命令运行的方式(可以改变命令的功能 ...

  8. elasticsearch入门使用(二) Mapping + field type字段类型

    Elasticsearch Reference [6.2] » Mapping 参考官方英文文档 https://www.elastic.co/guide/en/elasticsearch/refer ...

  9. Delphi使用进行post数据时超时设置

    因项目需要进行http的post提交数据,开始时用indy的idHttp组件,但是测试时发现当网络中断(如拔掉网线),idHttp的超时设置无效果,要等20秒才提示超时(参考网上的做法,将indy9升 ...

  10. mysql索引底层的数据结构和算法

    1.       为什么要用索引 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索 ...