1690 开关灯

 时间限制: 1 s
 空间限制: 128000 KB
 
题目描述 Description

YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人陆续按下开关,这些开关可以改变从第i盏灯到第j盏灯的状态,现在YYX想知道,从第x盏灯到第y盏灯中有多少是亮着的(1<=i,j,x,y<=N)

输入描述 Input Description
第 1 行: 用空格隔开的两个整数N和M
第 2..M+1 行: 每行表示一个操作, 有三个用空格分开的整数: 指令号(0代表按下开关,1代表询问状态), x 和 y 
输出描述 Output Description

第 1..询问总次数 行:对于每一次询问,输出询问的结果

样例输入 Sample Input

4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

样例输出 Sample Output
1
2
 
数据范围及提示 Data Size & Hint

一共4盏灯,5个操作,下面是每次操作的状态(X代表关上的,O代表开着的):

XXXX -> OOXX -> OXOO -> 询问1~3 -> OOXX -> 询问1~4

思路:线段树成段更新,每次一个区间改变为关灯的数量,每次模2即可;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=4e3+,M=1e6+,inf=1e9+;
int sum[M];
int flag[M];
void pushdown(int pos,int len)
{
int lson=pos*;
int rson=pos*+;
if(flag[pos])
{
flag[lson]+=flag[pos];
flag[lson]%=;
flag[rson]+=flag[pos];
flag[rson]%=;
flag[pos]=;
sum[lson]=(len-(len>>))-sum[lson];
sum[rson]=(len>>)-sum[rson];
}
}
void buildtree(int l,int r,int pos)
{
if(l==r)
{
sum[pos]=;
flag[pos]=;
return;
}
int mid=(l+r)>>;
buildtree(l,mid,pos*);
buildtree(mid+,r,pos*+);
}
int query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)
return sum[pos];
pushdown(pos,(r-l+));
int mid=(l+r)>>;
int ans=;
if(L<=mid)ans+=query(L,R,l,mid,pos*);
if(mid<R)ans+=query(L,R,mid+,r,pos*+);
return ans;
}
void update(int L,int R,int change,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
flag[pos]+=change;
flag[pos]%=;
sum[pos]=(r-l+)-sum[pos];
return;
}
pushdown(pos,r-l+);
int mid=(l+r)/;
if(L<=mid)update(L,R,change,l,mid,pos*);
if(R>mid)update(L,R,change,mid+,r,pos*+);
sum[pos]=sum[pos*]+sum[pos*+];
}
int main()
{
int x,y,z,i,t;
while(~scanf("%d%d",&x,&y))
{
for(i=;i<y;i++)
{
int flag,l,r;
scanf("%d%d%d",&flag,&l,&r);
if(flag)
{
printf("%d\n",query(l,r,,x,));
}
else
{
update(l,r,,,x,);
}
}
}
return ;
}

codevs 1690 开关灯 线段树+延迟标记的更多相关文章

  1. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  2. codevs 1690 开关灯 线段树水题

    没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...

  3. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  4. Tree(树链剖分+线段树延迟标记)

    Tree http://poj.org/problem?id=3237 Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12 ...

  5. codevs 2216 行星序列 线段树+延迟标记(BZOJ 1798)

    2216 行星序列  时间限制: 2 s  空间限制: 256000 KB     题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始, ...

  6. HDU4893--Wow! Such Sequence! (线段树 延迟标记)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)

    Jiu Yuan Wants to Eat https://nanti.jisuanke.com/t/31714 You ye Jiu yuan is the daughter of the Grea ...

  8. 线段树--codevs 1690 开关灯

    codevs 1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description YYX家门前的街上有N(2& ...

  9. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

随机推荐

  1. java jar命令及补丁方法

    用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...选项: -c 创建新档案 -t ...

  2. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  3. windows dos 常用命令行

    有关某个命令的详细信息,请键入 HELP 命令名 dir (directory) :列出当前目录下的文件以及文件夹 md (make directory): 创建目录 rd (remove direc ...

  4. VC编译选项 多线程(/MT)

    VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库                        库文件Single threa ...

  5. centos7.4yum错误

    1. 首先删除redhat原有的yum   rpm -aq | grep yum | xargs rpm -e --nodeps   rpm -aq | grep python-iniparse | ...

  6. python 类 __module__ __class__

    __module__ 和  __class__  __module__ 表示当前操作的对象在那个模块 __class__     表示当前操作的对象的类是什么 创建一个目录lib 在day7 目录下创 ...

  7. 关于RxJava背压

    http://flyou.ren/2017/04/05/%E5%85%B3%E4%BA%8ERxJava%E8%83%8C%E5%8E%8B/?utm_source=tuicool&utm_m ...

  8. 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)

    1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...

  9. 问题:ORA-28000: the account is locked 用户锁住了。

    打开cmd: 执行 sqlplus /nolog 执行conn / as sysdba 执行 ALTER USER  username  ACCOUNT UNLOCK;

  10. B树、B+树、二叉树、红黑树

    B树下面来具体介绍一下B-树(Balance Tree),一个m阶的B树具有如下几个特征:1.根结点至少有两个子女.2.每个中间节点都包含k-1个元素和k个孩子,其中 m/2 <= k < ...