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神 ...
随机推荐
- configure,make和make install关系
linux编译安装中configure.make和make install各自的作用 ./configure是用来检测你的安装平台的目标特征的.configure根据给定的参数和系统环境会生成Make ...
- promise, async和await
最开始实现异步的方法:回调函数 method1(function(err, result) { if (err) { throw err; } method2(function(err, result ...
- asp.net core 中hangfire面板的配置及使用
1.定义校验授权类DyDashboardAuthorizationFilter /// <summary> /// Hangfire仪表盘配置授权 /// </summary> ...
- MYSQL WHERE语句
过滤条件(WHERE) 如果你失忆了,希望你能想起曾经为了追求梦想的你. QQ群:651080565(php/web 学习课堂) 例子:淘宝首页上,我们会看到很多个商品,但这些商品,并不是 ...
- 15.SpringMVC核心技术-数据验证
在 Web 应用程序中,为了防止客户端传来的数据引发程序的异常,常常需要对数据进行验证. 输入验证分为客户端验证与服务器端验证.客户端验证主要通过 JavaScript 脚本进 行, 而服务器端验证则 ...
- gulp die('click').live('click' composer
gulp die('click').live('click' composer packagist.org https://getcomposer.org/ 下载后 php composer.pha ...
- Nginx配置客户端SSL双向认证
对于 NGINX 的 HTTPS 配置,通常情况下我们只需要实现服务端认证就行,因为浏览器内置了一些受信任的证书颁发机构(CA),服务器端只需要拿到这些机构颁发的证书并配置好,浏览器会自己校验证书的可 ...
- C# Winform 禁止一个进程运行多次
禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...
- (转)android(SignalA)接收.net(SignalR)推送过来的消息
从网络上搜索到的Demo,自己进行了稍微的改动 Signala类库从https://github.com/erizet/SignalA获得,不过相关引用有错误,需要手动修正. 下载相关源码 packa ...
- socket 多线程安全、粘包问题
脚本如下: # -*- coding:utf-8 -*- ''' @Author: Stefan @File: server_listener.py @Date: 2016-11-09 If you ...