洛谷 p1047 校门外的树 线段树做法
非常easy,
注意一下它是两端开始,也就是说0的位置也有一棵树就好了
我由于太弱了,一道红题交了4,5遍
由于树的砍了就没了,lazy标记最大就是1;
直接贴代码吧
#include<bits/stdc++.h>
using namespace std;
inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
inline void write(int x) {if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); }
int sum[400010],lazy[400010];
void build(int root,int l,int r){
if(l == r){
sum[root] = 1;
return ;
}
int mid = (l + r) >> 1;
int left = root << 1;
int right = left + 1;
build(left,l,mid);
build(right,mid + 1,r);
sum[root] = sum[left] + sum[right];
}
void add(int root,int l,int r,int k){
if(k == 0)
return ;
if(lazy[root])
return ;
lazy[root] = 1;
sum[root] = 0;
return ;
}
void putdown(int root,int l,int r){
if(lazy[root] == 0)
return ;
int mod = lazy[root] % 2; int mid = (l + r) >> 1;
add(root * 2,l,mid,mod);
add(root * 2 + 1,mid + 1,r,mod);
lazy[root] = 0;
return ;
}
void change(int root,int l,int r,int x,int y){
if(l > y || r < x)
return ;
if(l >= x && r <= y){
add(root,l,r,1);
return ;
}
int mid = (l + r) >> 1;
putdown(root,l,r);
int left = root << 1;
int right = left + 1;
if(x <= mid) change(left,l,mid,x,y);
if(mid < y) change(right,mid + 1,r,x,y);
sum[root] = sum[left] + sum[right];
}
int find(int root,int l,int r,int x,int y){
if(l >= x && r <= y){
return sum[root];
}
int mid = (l + r) >> 1,ans = 0;
int left = root << 1;
int right = left + 1;
putdown(root,l,r);
if(x <= mid) ans += find(left,l,mid,x,y);
if(mid < y) ans += find(right,mid + 1,r,x,y);
return ans;
}
int main(){
int n,m;
cin >> n >> m;
build(1,1,n + 1);
for(int i = 1; i <= m;++i){
int y,z;
scanf("%d%d",&y,&z);
change(1,1,n + 1,y + 1,z + 1);
}
write(find(1,1,n + 1,1,n + 1));
cout<<endl;
return 0;
}
洛谷 p1047 校门外的树 线段树做法的更多相关文章
- 洛谷——P1047 校门外的树
P1047 校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- 洛谷P1047 校门外的树
P1047 校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- 洛谷 P1047 校门外的树(待完善)
链接:https://www.luogu.org/problemnew/show/P1047 题目: 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是11米.我们可以把马路看 ...
- 洛谷 P1047 校门外的树 题解
Case 1. 本题其实不难,直接模拟就可以了.时间复杂度: \(O(L \times M)\) Case 2. 考虑一个简单的增强:把原来的: \[L \leq 10^4,M \leq 10^2 \ ...
- 洛谷 P1047 校门外的树
#include<iostream> #include<vector> #include<algorithm> using namespace std; ]; in ...
- 洛谷P1047校门外的树题解
题目 此题是一个模拟题,但需要注意的一点就是它的树是从数轴的0开始,所以我们也要从0开始,这样才能实现代码. 代码: #include<iostream> using namespace ...
- Java实现 洛谷 P1047 校门外的树
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = ...
- 洛谷 P1198 [JSOI2008]最大数——单调栈/线段树
先上一波题目 https://www.luogu.org/problem/P1198 题目要求维护后缀最大值 以及在数列的最后面添加一个数 这道题呢我们有两种做法 1.单调栈 因为只需要维护后缀最大值 ...
- 洛谷 P1276 校门外的树(增强版)
题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...
随机推荐
- SQLAlchemy基础
1.介绍 做个简单笔记,方便回顾. SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后 ...
- Eclipse/Idea 代码格式化部分忽略
有时候我们写一些代码的时候,格式化后,会使代码格式变得不美观.不易读,这里在看一些源码的时候发现有这么一种操作,随手记下 效果 那个代码像以下这样,@formatter:off开启关闭格式化,@for ...
- 【08】Jenkins:关于发布
写在前面的话 Jenkins 对于我们用户而言,可能中间会有不同的需求,比如自动构建,接口测试,代码质量检测.但其实我们的最终目的还是打包上线.当然,各个公司的项目开发语言会不一样,但是总体而言发布方 ...
- 利用kibana学习 elasticsearch restful api (DSL)
利用kibana学习 elasticsearch restful api (DSL) 1.了解elasticsearch基本概念Index: databaseType: tableDocument: ...
- windows系统mysql-5.7官方绿色版zip包安装教程
准备 下载页面:https://dev.mysql.com/downloads/mysql/ 点击 Download 按钮下载zip包到本地,解压(以我本地的解压路径是 D:\db\mysql-5.7 ...
- node、npm、gulp安装
1.先安装node.js ,官网下载地址:https://nodejs.org/en/ 2.安装完node之后,npm自动就安装了.可以直接在visual studio code 通过命令查看 nod ...
- python 练习题:请利用循环依次对list中的每个名字打印出Hello, xxx!
方法一: # -*- coding: utf-8 -*- # 请利用循环依次对list中的每个名字打印出Hello, xxx! L = ['Bart', 'Lisa', 'Adam'] n = 0 w ...
- # .NET Core下操作Git,自动提交代码到
.NET Core下操作Git,自动提交代码到 转自博客园(阿星Plus) .NET Core 3.0 预览版发布已经好些时日了,博客园也已将其用于生产环境中,可见 .NET Core 日趋成熟 回归 ...
- VS.NET(C#)--2.4_aspx默认页面模板代码
默认模板代码 客户端浏览器将忽视<script>块间任何字符,不在页面输出.通过<%=DataTime.Now.ToString() %> 将服务端代码放中间 < ...
- Linux C/C++编译过程中的各种not declared in this scope
Linux C/C++编译时经常会"XXX was not declared in this scope" 原因可能是以下几种: 变量名或函数名写错了; 忘记定义了 没有成功链接到 ...