【CodeForces】915 E. Physical Education Lessons 线段树
【题目】E. Physical Education Lessons
【题意】10^9范围的区间覆盖,至多3*10^5次区间询问。
【算法】线段树
【题解】每次询问至多增加两段区间,提前括号分段后线段树。
#include<cstdio>
#include<cctype>
#include<set>
#include<algorithm>
using namespace std;
int read(){
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
const int maxn=;
struct tree{int l,r,delta,sum,p;}t[maxn*];//
int au[maxn],av[maxn],k[maxn],a[maxn],n,q,tot;
set<int>s;
void build(int k,int l,int r){
t[k].l=l;t[k].r=r;t[k].delta=-;
if(l==r){t[k].p=t[k].sum=a[l]-a[l-];return;}
int mid=(l+r)>>;
build(k<<,l,mid);build(k<<|,mid+,r);
t[k].p=t[k<<].p+t[k<<|].p;
t[k].sum=t[k<<].sum+t[k<<|].sum;
}
void modify(int k,int x){t[k].sum=t[k].p*x;t[k].delta=x;}
void down(int k){
if(~t[k].delta){
modify(k<<,t[k].delta);modify(k<<|,t[k].delta);
t[k].delta=-;
}
}
void up(int k){t[k].sum=t[k<<].sum+t[k<<|].sum;}
void cover(int k,int l,int r,int x){
if(l<=t[k].l&&t[k].r<=r){modify(k,x);return;}
down(k);
int mid=(t[k].l+t[k].r)>>;
if(l<=mid)cover(k<<,l,r,x);
if(r>mid)cover(k<<|,l,r,x);
up(k);
}
int main(){
n=read();q=read();
for(int i=;i<=q;i++){
au[i]=read();av[i]=read();k[i]=read();
s.insert(au[i]-);s.insert(av[i]);
}
if(*s.begin()==)s.erase(s.begin());
s.insert(n);
for(set<int>::iterator it=s.begin();it!=s.end();it++){
a[++tot]=*it;
}
for(int i=;i<=q;i++){
au[i]=lower_bound(a+,a+tot+,au[i])-a;
av[i]=lower_bound(a+,a+tot+,av[i])-a;
}
n=tot;
build(,,n);
for(int i=;i<=q;i++){
cover(,au[i],av[i],k[i]-);
printf("%d\n",t[].sum);
}
return ;
}
【CodeForces】915 E. Physical Education Lessons 线段树的更多相关文章
- Codeforces 915 E Physical Education Lessons
题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...
- 【Codeforces 915E】 Physical Education Lessons
[题目链接] 点击打开链接 [算法] 线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace std; ; ,root ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons
题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...
- E. Physical Education Lessons 动态开辟线段树区间更新
E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- CF915E Physical Education Lessons
题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...
随机推荐
- 在.net项目中使用Consul
1.创建.net core web程序并运行 2.在Consul中注册该服务 Consul支持两种服务注册的方式,一种是通过Consul的服务注册HTTP API,由服务自身在启动后调用API注册自己 ...
- 1 RabbitMQ 安装,配置
1:安装 yum install -y rabbitmq-server 2:主要程序介绍 # 管理插件的程序 /usr/sbin/rabbitmq-plugins # 服务程序 /usr/sbin ...
- C#获取当前路径的方法如下
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. System.Environm ...
- IF与SWITCH
今晚刚刚看了一点儿<大话设计模式>这本书,看到它示例的第一个程序,好像有点理解IF与SWITCH的区别了.大致的思考了总结出来. IF适用于每个条件都必须判断,就是IF语句中的判断是不同类 ...
- webgl helloworld
之前的webgl 初识1, 初识2 已经介绍了webgl的基本概念,工作原理. 没有理解的自己yy. 现呈上例子一枚 <!DOCTYPE html> <html lang=" ...
- ubuntu下搭建openGL环境
1. 建立基本编译环境 sudo apt-get install build-essential 2. 安装OpenGL Library sudo apt-get install ...
- RxSwift基本使用(一)
备注:本文参考自田腾飞博文 [RxSwift入坑解读-你所需要知道的各种概念] (http://www.codertian.com/2016/11/27/RxSwift-ru-keng-ji-read ...
- HUST1017-Exact Cover
给出一个\(n\times m\)的01矩阵,每行最多有\(c\)个1,求一个精确覆盖,即选出一些行使得每列有有且仅有一个1.输出方案. 分析 被这个题坑到了啊!!第一次上HUSTOJ做题,不知道没有 ...
- Python网络编程socket
网络编程之socket 看到本篇文章的题目是不是很疑惑,what is this?,不要着急,但是记住一说网络编程,你就想socket,socket是实现网络编程的工具,那么什么是socket,什么是 ...
- day 05 万恶之源-基本数据类型(dict)
05. 万恶之源-基本数据类型(dict)本节主要内容:1. 字典的简单介绍2. 字典增删改查和其他操作3. 字典的嵌套⼀一. 字典的简单介绍字典(dict)是python中唯⼀一的⼀一个映射类型.他 ...