题意:

Alex高中毕业了,他现在是大学新生。虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外。快要期末了,但是不幸的Alex的体育学分还是零蛋!

Alex可不希望被开除,他想知道到期末还有多少天的工作日,这样他就能在这些日子里修体育学分。但是在这里计算工作日可不是件容易的事情:

从现在到学期结束还有 n 天(从 1 到 n 编号),他们一开始都是工作日。接下来学校的工作人员会依次发出 q 个指令,每个指令可以用三个参数 l,r,k 描述:

  • 如果 k=1,那么从 l 到 r (包含端点)的所有日子都变成非工作日。

  • 如果 k=2,那么从 l 到 rr(包含端点)的所有日子都变成工作日。

帮助Alex统计每个指令下发后,剩余的工作日天数。

输入格式:

第一行一个整数 n,第二行一个整数 q (1≤n≤109,1≤q≤3⋅105),分别是剩余的天数和指令的个数。

接下来 q 行,第 i 行有 3 个整数 li​,ri​,ki​,描述第 i 个指令 (1≤li​,ri​≤n,1≤k≤2)。

输出格式:

输出 q 行,第 i 行表示第 i 个指令被下发后剩余的工作日天数。

Translated by 小粉兔

题目描述

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 nn days left before the end of the term (numbered from 11 to nn ), and initially all of them are working days. Then the university staff sequentially publishes qq orders, one after another. Each order is characterised by three numbers ll , rr and kk :

  • If k=1k=1 , then all days from ll to rr (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=2k=2 , then all days from ll to rr (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 nn , and the second line — one integer qq ( 1<=n<=10^{9}1<=n<=109, 1<=q<=3·10^{5}1<=q<=3⋅105 ) — the number of days left before the end of the term, and the number of orders, respectively.

Then qq lines follow, ii -th line containing three integers l_{i}li​ , r_{i}ri​ and k_{i}ki​ representing ii -th order ( 1<=l_{i}<=r_{i}<=n1<=li​<=ri​<=n , 1<=k_{i}<=21<=ki​<=2 ).


输出格式:

Print qq integers. ii -th of them must be equal to the number of working days left until the end of the term after the first ii orders are published.

输入输出样例

输入样例#1:

4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2
输出样例#1:

2
0
2
3
1
4

Solution:

  本题动态开点线段树。

  题目和那个开关灯的用线段树解决的问题是一样的,只不过区间范围比那个大。

  对于本题当然可以离散化,但更为简单的就是动态开点了。

  我们对于每次修改的一段区间,都在$[1,n]$范围内二分,对于没有建节点的区间就兴建节点维护,然后加上懒惰标记,记得下放标记时也得判断子节点有无并动态开节点就好了。(注意,空间一定得开大,粗略的算下得开到$2\log n$倍$q$)

代码:

/*Code by 520 -- 9.25*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,m,root,cnt;
int ls[N*],rs[N*],sum[N*],lazy[N*]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void pushup(int rt){sum[rt]=sum[ls[rt]]+sum[rs[rt]];} il void pushdown(int l,int r,int rt){
if(~lazy[rt]){
if(l!=r) {
if(!ls[rt]) ls[rt]=++cnt;
if(!rs[rt]) rs[rt]=++cnt;
lazy[ls[rt]]=lazy[rt],
lazy[rs[rt]]=lazy[rt];
sum[ls[rt]]=((l+r>>)-l+)*lazy[rt];
sum[rs[rt]]=(r-(l+r>>))*lazy[rt];
}
lazy[rt]=-;
}
} void update(int L,int R,int x,int l,int r,int &rt){
if(!rt) rt=++cnt;
if(L<=l&&R>=r){lazy[rt]=x;sum[rt]=(r-l+)*x;return;}
pushdown(l,r,rt);
int m=l+r>>;
if(L<=m) update(L,R,x,l,m,ls[rt]);
if(R>m) update(L,R,x,m+,r,rs[rt]);
pushup(rt);
} int main(){
memset(lazy,-,sizeof(lazy));
n=gi(),m=gi();
int x,y,z;
while(m--){
x=gi(),y=gi(),z=gi();
if(z==) update(x,y,,,n,root);
else update(x,y,,,n,root);
printf("%d\n",n-sum[]);
}
return ;
}

CF915E Physical Education Lessons的更多相关文章

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

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

  2. 【题解】Luogu CF915E Physical Education Lessons

    原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...

  3. CF915E Physical Education Lessons 珂朵莉树

    问题描述 CF915E LG-CF915E 题解 \(n \le 10^9\) 看上去非常唬人. 但是这种区间操作的题,珂朵莉树随便跑啊. \(\mathrm{Code}\) #include< ...

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

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

  5. Codeforces 915E Physical Education Lessons

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

  6. 【CodeForces】915 E. Physical Education Lessons 线段树

    [题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...

  7. Codeforces 915 E Physical Education Lessons

    题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...

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

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

  9. Codeforces 915E. Physical Education Lessons(动态开点线段树)

    E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...

随机推荐

  1. TCP Over HTTP 的Buffer问题

    记录下备忘. 场景:要把TCP拆成一个个HTTP请求,通过Proxy 1.HTTP Client上载数据到CCProxy ,然后再到Web服务器的时候. 如果数据量比较小,例如10个字节,Proxy就 ...

  2. .Net Core Linux centos7行—jenkins linux 构建.net core web app

    1.安装jdk.jenkins 是一个java web程序.所以必然需要jdk. yum install java 或者 yum install java-1.8.0-openjdk 2.下载jenk ...

  3. 手动搭建openstack的痛苦经历

    openstack真的是一个十分痛苦的东西,好在有自动部署工具,虽然有自动部署工具可以方便我们部署使用,但是学习的话,第一次最好手动部署,因为手动部署更能我们了解openstack的工作流程和各组建之 ...

  4. redis-4.0.2

    redis-4.0.2.tar.gz 链接:https://pan.baidu.com/s/1qj4bSgM1s2InLikugRNqKA 提取码:tozq 复制这段内容后打开百度网盘手机App,操作 ...

  5. git push失败

    不知道弄错了什么上传项目到github上失败 git commit的时候提示 On branch masternothing to commit, working tree clean git pus ...

  6. 打包一个传统的ASP.NET web app作为Docker镜像

    (1)针对NerdDinner应用的Dockerfile内容如下 PS E:\DockeronWindows\Chapter02\ch02-nerd-dinner> cat .\Dockerfi ...

  7. 图片人脸检测(OpenCV版)

    图片人脸检测 人脸检测使用到的技术是OpenCV,上一节已经介绍了OpenCV的环境安装,点击查看. 功能展示 识别一种图上的所有人的脸,并且标出人脸的位置,画出人眼以及嘴的位置,展示效果图如下: 多 ...

  8. easyui panel异步获取后台数据在前台显示

    我在使用easyui的时候,想做一个向下图所示的效果,这个panel的样式已经做好了,想从后台异步获取json数据,然后填入到文本框中,不知道哪位大神能给点指导?万分感谢! 放入表单中,使用form对 ...

  9. fiddler常识汇总

    Fiddler 抓包工具总结   名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FTP ...

  10. 20172301 2017-2018-2 《程序设计与数据结构》实验一《Java开发环境的熟悉》实验报告

    20172301 2017-2018-2 <程序设计与数据结构>实验一<Java开发环境的熟悉>实验报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 郭 ...