洛谷 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 中凡是 ...
随机推荐
- C++:const
const const是C++提供的一个强大的关键字,const的用法非常多,但总的来说,const的作用只有一个:保证被修饰的内容不会被程序所修改. const基本用法 对一个类型的对象使用cons ...
- C#中$拼字符串
除了Format,StringBuilder,+方式拼字符串外,有一种比较直观的拼字符串方式,代码如下 static void Main(string[] args) { string name = ...
- Mysql load data infile 命令导入含中文csv源数据文件 【错误代码 1300】
[1]Load data infile 命令导入含中文csv源数据文件 报错:Invalid utf8 character string: '??֧' (1)问题现象 csv格式文件源数据: 导入SQ ...
- Why React Is Favored by Front-End Specialists
In this section, we will discuss some of the features that make React a superior choice for front-en ...
- checkbox实现单选
脚本: function bb(obj,name) { var aa = document.getElementsByName(name); for (var i = 0; i < aa.len ...
- OCaml (ML家族语言)很好很强大
理由如下: 1. 强类型,写着放心 2. 函数式. 且不是pure functional, 不用像 Haskell 那样极端.. 3. Algebric Data Types 的模式匹配 实在太爽了 ...
- 完美转发(perfect forwarding)、universal reference、引用折叠(collasping)
首先要分清: C++里的值只有两种值:左值.右值.—— 其本质应该是内存中存储的值/instance分两种:一种是持久的,一种是“短暂的” 也只有两种引用: 左值引用.右值引用. ——引用,就是这个内 ...
- C# Socket服务器及多客户端连接示例
服务端代码[控制台示例] static List<Socket> Sockets = new List<Socket>(); static void Main(string[] ...
- Springboot 项目源码 vue.js html 跨域 前后分离 shiro权限
官网:www.fhadmin.org 特别注意: Springboot 工作流 前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...
- 选美?作秀?MES系统的选择更应该从实际出发
MES选型不是做秀,不是选美. 如今不少企业在信息化推广应用过程中面面求好.追求完美,用意没错,然而在MES开发过程中,软件商不可能将今后各种可能出现的问题考虑周全,不可能将系统做到十全十美.随着系统 ...