Codeforces 915E. Physical Education Lessons(动态开点线段树)
题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和。(n<=1e9,q<=3e5)
题意:用线段树做的话,没什么思维量,主要是空间复杂度的问题。因此采用动态开点的办法,即需要用到的节点,在使用前分配内存,没有用到的就虚置。这样每次操作新增的节点约logn个。则q次修改需要的空间大约是qlogn。但是,在这个数量级上尽可能开的再大一些,防止RE。
#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=1e7+5e6+10,mod=1e9+7,INF=0x3f3f3f3f;
int sum[maxn],ls[maxn],rs[maxn],lazy[maxn],id,root;
void push_dw(int l,int r,int rt)
{
if (lazy[rt]!=-1)
{
if (!ls[rt])
ls[rt]=++id;
if (!rs[rt])
rs[rt]=++id;
int lson=ls[rt],rson=rs[rt];
int m=(l+r)>>1;
lazy[lson]=lazy[rson]=lazy[rt];
sum[lson]=lazy[rt]*(m-l+1);
sum[rson]=lazy[rt]*(r-m);
lazy[rt]=-1;
}
}
void upd(int L,int R,int c,int l,int r,int& rt)
{
if (!rt)
rt=++id;
if (L<=l&&r<=R)
{
lazy[rt]=c;
sum[rt]=(r-l+1)*c;
return;
}
push_dw(l,r,rt);
int m=(l+r)>>1;
if (L<=m)
upd(L,R,c,l,m,ls[rt]);
if (R>m)
upd(L,R,c,m+1,r,rs[rt]);
sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
int main()
{
int n,q;
cin>>n>>q;
memset(lazy,-1,sizeof(lazy));
for (int i=0;i<q;++i)
{
int l,r,ty;
scanf("%d%d%d",&l,&r,&ty);
upd(l,r,ty==1?1:0,1,n,root);
printf("%d\n",n-sum[1]);
}
return 0;
}
Codeforces 915E. Physical Education Lessons(动态开点线段树)的更多相关文章
- codeforces 915E - Physical Education Lessons 动态开点线段树
题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- CodeForces - 915E 动态开点线段树
题目 晚上有n个亮着的灯泡,标号从1到n. 现在存在2种操作,如下: 操作1,关掉标号 [l,r] 区间的灯 操作2,打开标号 [l,r] 区间的灯 下面有q次询问,每次询问执行其中一种操作,询问格式 ...
- CF915E Physical Education Lessons(珂朵莉树)
中文题面 据说正解是动态开点线段树而且标记也不难下传的样子 然而这种区间推平的题目还是喜欢写珂朵莉树啊……码量小…… 虽然真要构造的话随便卡…… //minamoto #include<cstd ...
- Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树
思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用 lazy=0 没被覆盖过 els ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
随机推荐
- C++反汇编第五讲,认识C++中的Try catch语法,以及在反汇编中还原
我们以前讲SEH异常处理的时候已经说过了,C++中的Try catch语法只不过是对SEH做了一个封装. 如果不懂SEH异常处理,请点击博客链接熟悉一下,当然如果不想知道,也可以直接往下看.因为异常处 ...
- Rikka with Competition hdu 6095
签到题目,排序然后按序清理掉一定会输的结果就可以. ac代码: #include <iostream> #include <cstdio> #include <cstri ...
- Nopcommerce 使用Task时dbcontext关闭问题
1.开启一个线程 Task.Run(() => { CreatPrintImage(preViewModel.DiyProductGuid); }); 2.线程代码 /// <summar ...
- 设置centos7中的mysql5.7不区分表名大小写有关操作
1.#which mysqld //查看mysql的命令路径 /usr/sbin/mysqld 2.#/usr/sbin/mysqld --verbose --help | grep ...
- mysql5.7 密码字段名更改
由password更改为authentication_string update user set authentication_string=password("123456") ...
- 【C++】如何提高Cache的命中率,示例
参考链接 https://stackoverflow.com/questions/16699247/what-is-a-cache-friendly-code 只是堆积:缓存不友好与缓存友好代 ...
- Spark机器学习API之特征处理(一)
Spark机器学习库中包含了两种实现方式,一种是spark.mllib,这种是基础的API,基于RDDs之上构建,另一种是spark.ml,这种是higher-level API,基于DataFram ...
- css样式背景图片设置缩放
一.背景颜色图片平铺 background-color 背景颜色 background-image 背景图片地址 background-repeat 是否平铺 默认是平铺 background-pos ...
- React ~ 小结
React 小结 state 与 props react 里,只需更新组件的state,然后根据新的state重新渲染用户界面,不需要操作dom. 添加类的构造函数来初始化状态this.state,类 ...
- 测试clang-format的格式化效果
我自己写的业余框架已告一段落,主体功能已完成,剩下的就是优化.第一个要优化的,就是代码格式.我一直是用编辑器写代码的,从之前的UltraEdit到notepad++到sublime text,再到现在 ...