【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可不希望被开除,他想知道到期末还 ...
随机推荐
- C++ Primer Plus学习:第六章
C++入门第六章:分支语句和逻辑运算符 if语句 语法: if (test-condition) statement if else语句 if (test-condition) statement1 ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- Java final用法
//继承弊端:打破了封装性. /* final关键字: 1,final是一个修饰符,可以修饰类,方法,变量. 2,final修饰的类不可以被继承. 3,final修饰的方法不可以被覆盖. 4,fina ...
- sublime py不能输入中文
设置环境变量PYTHONIOENCODING=UTF-8,重启sublime即可 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/
- 怎么用JavaScript实现tab切换
先看一下代码实现后的最终效果: 用JavaScript实现思路很简单,就是先把所有的内容隐藏,点击标题对应的内容显示, css代码如下: <style type="text/css&q ...
- 第146天:移动H5前端性能优化
移动H5前端性能优化 一.概述 1. PC优化手段在Mobile侧同样适用 2. 在Mobile侧我们提出三秒种渲染完成首屏指标 3. 基于第二点,首屏加载3秒完成或使用Loading 4. 基于联通 ...
- 公告:请访问我的个人博客新站点——www.huangshujia.me
我的个人博客现在在(http://www.huangshujia.me/)cnblog这里不会做任何更新了.或者关注我的个人公众号:碱基矿工
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore (续)
java semaphore实现: Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有应用.Java 并发库 的Semaphore 可以很轻松完成信号 ...
- Tomcat+JDK安装和配置
Tomcat+JDK安装和配置 一.打开FlashFXP软件,建立连接,选择需要的包,右击传输到 /home/guest中 二.进入到:cd /home/guest中,对tomcat包进行解压 三.将 ...
- 用CSS实现3D 滚动的立方体
用css3写3D立方体用到的属性不多,就那么几个:perspective,transform-style,以及transform. 目前来说能完美支持3D的浏览器有chrome.safari,火狐也支 ...