E. Physical Education Lessons 动态开辟线段树区间更新
1 second
256 megabytes
standard input
standard output
This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!
Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:
There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers l, r and k:
- If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
- If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.
Help Alex to determine the number of working days left after each order!
The first line contains one integer n, and the second line — one integer q (1 ≤ n ≤ 109, 1 ≤ q ≤ 3·105) — the number of days left before the end of the term, and the number of orders, respectively.
Then q lines follow, i-th line containing three integers li, ri and ki representing i-th order (1 ≤ li ≤ ri ≤ n, 1 ≤ ki ≤ 2).
Print q integers. i-th of them must be equal to the number of working days left until the end of the term after the first i orders are published.
4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2
2
0
2
3
1
4
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int rt=,LL[N],RR[N],V[N],tot=,lazy[N];
void pdw(int x,int val){
if(lazy[x]!=-) {
if(!LL[x]) LL[x]=++tot;if(!RR[x]) RR[x]=++tot;
if(lazy[x]) V[LL[x]]=(val+)/,V[RR[x]]=val/;
else V[LL[x]]=V[RR[x]]=;
lazy[LL[x]]=lazy[RR[x]]=lazy[x];
lazy[x]=-;
}
}
void update(int &x,int l,int r,int L,int R,bool f){
if(!x) x=++tot;
if(L>=l&&R<=r) {
if(f) V[x]=R-L+;else V[x]=;
lazy[x]=f;
return;
}
pdw(x,R-L+);
int mid=(L+R)>>;
if(l<=mid) update(LL[x],l,r,L,mid,f);
if(r>mid) update(RR[x],l,r,mid+,R,f);
V[x]=V[LL[x]]+V[RR[x]];
}
int main(){
int n,m,x,y,z;
scanf("%d%d",&n,&m);
memset(lazy,-,sizeof(lazy));
for(int i=;i<=m;++i) {
scanf("%d%d%d",&x,&y,&z);
update(rt,x,y,,n,z==);
printf("%d\n",n-V[]);
}
}
E. Physical Education Lessons 动态开辟线段树区间更新的更多相关文章
- F. Pathwalks动态开辟线段树
题意:n点m边,然后要求走最多的路,走路的时候经过的边权必须是严格递增. 解法1:传统的区间更新 解法2:发现区间更新只是对两个固定的点所延长形成的区间段,所以问题可以退化成单点更新单点查询. 然后动 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
随机推荐
- QML-密码管理器
Intro 年初刚学Qml时写的密码管理器.用到Socket通信.AES加密等.UI采用Material Design,并实现了Android App的一些常见基本功能,如下拉刷新.弹出提示.再按一次 ...
- 学习 .net core 3----蒋金楠 笔记 构建 Asp.net core Web应用
前言:准备系统的学习一下.net core 所以购买了 蒋金楠的 ASP.NET CORE 3 书籍,为了加深印象 特此笔记,会持续更新到学习完为止 使用 命令行 dotnet new co ...
- 【DNS域名解析命令】 dig
dig - DNS lookup utility dig 命令主要用来从 DNS 域名服务器查询主机地址信息. Dig (domain information groper 域名信息搜索)是一个灵活的 ...
- iOS Block 页面传值
为什么80%的码农都做不了架构师?>>> 直接上代码 1.定义block @interface TopTypeCollectionView : UIView @property ...
- Node.js快速创建一个访问html文件的服务器
var http = require('http'), // 引入需要的模块 fs = require('fs'), //引入文件读取模块 cp = require('child_process'), ...
- Linux下进程的创建(system(); fork(); exec*())
0. system(); system()函数通过调用shell程序来执行所指向的命令(效率低),相当于先fork(),再execve(): 特点:原进程和子进程各自运行,且原进程需要等子进程运行完后 ...
- P4370 [Code+#4]组合数问题2
题目要求当\(0\leq a\leq b\leq n\)时,\(k\)个\(\tbinom{b}{a}\)的和的最大值 观察杨辉三角形,可以发现,最大的\(\tbinom{b}{a}\),为\(\tb ...
- Gitlab常规操作
一.Git和SVN的区别 和SVN类似,Git是一个版本控制系统(Version Control System,VCS),不同的是SVN为集中式版本控制系统,为单一的集中管理的服务器,保存所有文件的修 ...
- libevent(八)激活事件
激活事件添加流程 事件发生后,需要把对应的event加入到激活事件队列中. 整个流程如下: 对于定时器事件,在timeout_process过程中,会将事件从最小堆中删除. 激活事件处理流程 在eve ...
- spring mvc实现转发和重定向
转发:服务器端请求的跳转 同一个请求之内 重定向:客户端请求的跳转 两次请求 转发和重定向的区别: 1,转发地址栏不会发生变化,重定向地址栏会发生变化. 2,转发请求的参数不会丢失 重定向参数会丢失. ...