E. 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(动态开点线段树)的更多相关文章

  1. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  2. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  3. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

  4. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

  5. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  6. CodeForces - 915E 动态开点线段树

    题目 晚上有n个亮着的灯泡,标号从1到n. 现在存在2种操作,如下: 操作1,关掉标号 [l,r] 区间的灯 操作2,打开标号 [l,r] 区间的灯 下面有q次询问,每次询问执行其中一种操作,询问格式 ...

  7. CF915E Physical Education Lessons(珂朵莉树)

    中文题面 据说正解是动态开点线段树而且标记也不难下传的样子 然而这种区间推平的题目还是喜欢写珂朵莉树啊……码量小…… 虽然真要构造的话随便卡…… //minamoto #include<cstd ...

  8. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  9. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

随机推荐

  1. mysql replace substring 字符串截取处理

    SELECT a1,a2,replace(a2, "豫ICP备16006180号-", "") a22,a3,a4,a5 FROM `aaab` order b ...

  2. C#基础知识 (转)

    https://www.cnblogs.com/zhouzhou-aspnet/articles/2591596.html(原文地址) 本文是一个菜鸟所写,本文面向的人群就是像我这样的小菜鸟,工作一年 ...

  3. dev linechart动态加载数据(像股票一样的波动)

    图片地址:https://blog.csdn.net/qq_33459369/article/details/80060196:(盗图) 接下来是封装的代码 #region 动态折线图 public ...

  4. Java远程通讯可选技术及原理

    转自:https://www.linuxidc.com/index.htm 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MI ...

  5. Spring的启动流程

    spring的启动是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和监听器(Listener),下面就来看看web.xml ...

  6. android提升

    https://blog.csdn.net/lou_liang/article/details/82856531

  7. linux物理地址和虚拟地址

  8. linux入门常用指令1.配置本地yum源

    创建光盘挂载点 [root@localhost /]# mkdir /mnt/cdrom 挂载光盘 #挂载光盘 [root@localhost /]# mount /dev/cdrom /mnt/cd ...

  9. 2.03_01_Python网络爬虫urllib2库

    一:urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中抓取出来.在Python中有很多库可以用来抓取网页,我们先学习urllib2. urllib2 是 Python ...

  10. LoadRunner(7)

    一.参数化策略 1.Select next row(How? 如何取?)取值方式 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值: 数据取完可以从头循环重复使用 ...