codevs 1690 开关灯 线段树区间更新 区间查询Lazy
YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人陆续按下开关,这些开关可以改变从第i盏灯到第j盏灯的状态,现在YYX想知道,从第x盏灯到第y盏灯中有多少是亮着的(1<=i,j,x,y<=N)
第 1..询问总次数 行:对于每一次询问,输出询问的结果
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
2
一共4盏灯,5个操作,下面是每次操作的状态(X代表关上的,O代表开着的):
XXXX -> OOXX -> OXOO -> 询问1~3 -> OOXX -> 询问1~4
//code by drizzle
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n,m;
int flag,x,y;
struct node
{
int l;
int r;
int add;
int sum;
}tree[];
void buildtree(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
if(left==right)
{
tree[root].sum=;
return;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
void pushdown(int root,int m)
{
tree[root<<].add++;
tree[root<<].add=tree[root<<].add%;
tree[root<<|].add++;
tree[root<<|].add=tree[root<<|].add%;
tree[root<<].sum=(m-(m>>))-tree[root<<].sum;
tree[root<<|].sum=(m>>)-tree[root<<|].sum;
tree[root].add=;
}
void updata(int c,int left,int right,int root)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add++;
tree[root].add=tree[root].add%;
tree[root].sum=right-left+-tree[root].sum;
return ;
}
if(tree[root].l==tree[root].r)
return ;
if(tree[root].add)
pushdown(root,tree[root].r-tree[root].l+);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(c,left,right,root<<);
else
{
if(left>mid)
updata(c,left,right,root<<|);
else
{
updata(c,left,mid,root<<);
updata(c,mid+,right,root<<|);
}
}
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
int query(int root ,int left,int right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].sum;
}
if(tree[root].add)
pushdown(root,tree[root].r-tree[root].l+);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return (query(root<<,left,mid)+query(root<<|,mid+,right));
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
buildtree(,,n);
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&flag,&x,&y);
if(flag==)
{
updata(,x,y,);
}
else
{
printf("%d\n",query(,x,y));
}
}
}
return ;
}
codevs 1690 开关灯 线段树区间更新 区间查询Lazy的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- codevs 1690 开关灯 线段树+延迟标记
1690 开关灯 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- C# 文件操作 常用的类
File------实用类,提供许多静态方法,用于移动.删除.和复制文件. Directory------实用类,提供许多静态方法,用于移动.删除和复制目录. Path------ 实用类,用于处理路 ...
- 第十六篇、OC_按比例适配
// 屏幕高度 #define XMGHeight [UIScreen mainScreen].bounds.size.height // 屏幕宽度 #define XMGWidth [UIScree ...
- SummerVocation_Leaning--java动态绑定(多态)
概念: 动态绑定:在执行期间(非编译期间)判断所引用的对象的实际类型,根据实际类型调用其相应的方法.如下例程序中,根据person对象的成员变量pet所引用的不同的实际类型调用相应的方法. 具体实现好 ...
- 大数据的存储——HBase、HIVE、MYSQL数据库学习笔记
HBase 1.hbase为查询而生,它通过组织机器的内存,提供一个超大的内存hash表,它需要组织自己的数据结构,表在hbase中是物理表,而不是逻辑表,搜索引擎用它来存储索引,以满足实时查询的需求 ...
- mongodb安装,库操作,集合操作(表),文档操作(记录)
安装 1.下载地址 https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-4.0.8-signed.msi 2.如果报没 ...
- Mdrill集群安装
Mdrill集群安装 mdrill是阿里妈妈-adhoc-海量数据多维自助即席查询平台下的一个子项目.旨在帮助用户在几秒到几十秒的时间内,分析百亿级别的任意维度组合的数据.mdrill是一个分布式的在 ...
- Redis实现之字符串
简单动态字符串 Redis中的字符串并不是传统的C语言字符串(即字符数组,以下简称C字符串),而是自己构建了一种简单动态字符串(simple dynamic string,SDS),并将SDS作为Re ...
- day15 CSS JS DOM初探
居中 line-hight 是上下 text-line 是左右 实现一个返回顶部的功能: 1 先写好CSS 2 写动作JS 写一个悬浮菜单: <!DOCTYPE h ...
- SXCPC2018 nucoj2005 大闹上兰帝国
超 dark van♂全背包 ref1 ref2 #include <iostream> #include <cstring> #include <cstdio> ...
- 【Set Matrix Zeros】cpp
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...