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块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
随机推荐
- liunx 之 Ubuntu 网速慢解决方法
打开终端依次输入以下指令: sudo lshw -numeric -class network sudo ip addr show sudo ip route show sudo tracepath ...
- 009.Ansible模板管理 Jinja2
一 Jinja2简介 Jinja2是基于python的模板引擎. 假设说现在我们需要一次性在10台主机上安装redis,这个通过playbook现在已经很容易实现.默认情况下,所有的redis安装完成 ...
- HTML中使用CSS样式(上)
在每一个标签上都可以设置style属性,这就是CSS样式: <div style="height:48px;border: 1px solid red;text-align:cente ...
- 《OSPF和IS-IS详解》一1.5 ARPANET内的路由选择
本节书摘来异步社区<OSPF和IS-IS详解>一书中的第1章,第1.5节,作者: [美]Jeff Doyle 译者: 孙余强 责编: 傅道坤,更多章节内容可以访问云栖社区"异步社 ...
- Linux 下如何产生core文件(core dump设置)
转自:https://blog.csdn.net/star_xiong/article/details/43529637 今天在Linux下调试C程序时,出现段错误,习惯性的ls下当前目录,发现没有生 ...
- POJ 3581 Prime Gap(二分)
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...
- unittest(执行用例)
from selenium import webdriver from time import sleep import unittest#导入unittest库 import HTMLTestRun ...
- speedtest 测试服务器上传下载速度
下载speedtest.py wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py 赋予执行权限 ...
- PUBG 1V3 线段树扫描线
PUBG 1V3 这个题目我觉得好难写啊. 感觉自己码力不太行啊. 题目大意是,给你n个人,n个人组成m个队伍,每个队伍最多4个人. 然后给你每一个人的位置队伍信息还有攻击范围. 问当一个队伍剩下一个 ...
- A. A Twisty Movement dp
https://codeforces.com/problemset/problem/933/A 这个是一个dp,但是我并没有看出来,然后也不太会写, 这种题一般应该要想到先预处理前缀和后缀,然后再进行 ...