Codeforces - 915E 离散化区间覆盖
我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散
不然看一下test3就知道
不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重复的排序..)
线段树区间覆盖没啥好说的,自我感觉struct里写的足够清晰了
终于能睡个好觉了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e6+11;
int ll[maxn],rr[maxn],mark[maxn];
int ra[maxn],n,q;
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define println(a) printf("%lld\n",a)
typedef long long LL;
struct ST{
LL sum[maxn<<2];
int lazy[maxn<<2];
#define lc o<<1
#define rc o<<1|1
void init(){memset(lazy,-1,sizeof lazy);}
void pu(int o){sum[o]=sum[lc]+sum[rc];}
void pd(int o,int l,int r){
if(~lazy[o]){
lazy[lc]=lazy[rc]=lazy[o];
int m = l+r>>1;
sum[lc]=1ll*(ra[m]-ra[l-1])*lazy[o];
sum[rc]=1ll*(ra[r]-ra[m])*lazy[o];
lazy[o]=-1;
}
}
void build(int o,int l,int r){
if(l==r){
sum[o]=1ll*ra[r]-ra[l-1];
return;
}
int m = l+r>>1;
build(lc,l,m);
build(rc,m+1,r);
pu(o);
}
void update(int o,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
sum[o]=1ll*(ra[r]-ra[l-1])*v;
lazy[o]=v;
return;
}
pd(o,l,r);
int m = l+r>>1;
if(L<=m) update(lc,l,m,L,R,v);
if(R>m) update(rc,m+1,r,L,R,v);
pu(o);
}
LL query(int o,int l,int r,int L,int R){
if(L<=l&&r<=R) return sum[o];
pd(o,l,r);
int m = l+r>>1;
LL ans=0;
if(L<=m) ans+=query(lc,l,m,L,R);
if(R>m) ans+=query(rc,m+1,r,L,R);
return ans;
}
}st;
int main(){
while(scann(n,q)^-1){
rep(i,1,q) {scann(ll[i],rr[i]); scan(mark[i]);}
rep(i,1,q) {ra[i]=ll[i]; ra[i+q]=rr[i];} ra[2*q+1]=1; ra[2*q+2]=n;
sort(ra+1,ra+2*q+3);
int qq = unique(ra+1,ra+2*q+3)-ra-1;
int tot=qq;
rep(i,1,qq-1){
if(ra[i]+1<ra[i+1]) ra[++tot]=(ra[i]+1>=n?n:ra[i]+1);
}
sort(ra+1,ra+tot+1);
tot=unique(ra+1,ra+tot+1)-ra-1;
qq=tot;
for(int i=qq;i>1;i--){
if(ra[i]-1>ra[i-1]) ra[++tot]=(ra[i]-1<=1?1:ra[i]-1);
}
sort(ra+1,ra+tot+1);
tot=unique(ra+1,ra+tot+1)-ra-1;
rep(i,1,q) ll[i]=lower_bound(ra+1,ra+tot+1,ll[i])-ra,rr[i]=lower_bound(ra+1,ra+tot+1,rr[i])-ra;
st.init();st.build(1,1,tot);
rep(i,1,q){
st.update(1,1,tot,ll[i],rr[i],(mark[i]==1?0:1));
println(st.query(1,1,tot,1,tot));
}
}
return 0;
}
Codeforces - 915E 离散化区间覆盖的更多相关文章
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- Guess Your Way Out! II---cf 558D (区间覆盖,c++STL map 的使用)
题目链接:http://codeforces.com/contest/558/problem/D 题意就是有一个二叉树高度为 h ,人站在根节点上,现在要走出去,出口在叶子节点上,有 q 条信息,每条 ...
- Mayor's posters 线段树区间覆盖
题目链接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that t ...
- HDU2883 kebab(最大流判断满流 + 离散化 + 区间化点)
[题意]: 有一个烤箱,烤箱在一个时刻最多考M个肉串,N个顾客,每个顾客有属性s,n,e,t s是来的时间,n是想要的肉串数量,e是最晚离开的时间,t是烤的时间(几分熟). 顾客的烤肉可以分开烤,比如 ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)
http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...
随机推荐
- opennebula 模板参数说明
两种模板配置方式一.光驱引导启动需要配置:disk1:磁盘类型:cdrom 驱动类型:file 磁盘标记:hd 是否只读:yesDisk2:磁盘类型:DATABLOCK驱 ...
- 19-字符串匹配(kmp || substr,find)
链接:https://www.nowcoder.com/acm/contest/77/C来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- Spring下面的@Transactional注解标志的讲解
最近在开发中对Spring中的事务标记@Transactional用的比较多,今天上网收集了一些内容,做一个简单的总结~~~ 在service类前加上@Transactional,声明这个servic ...
- 使用Sqlserver事务发布实现数据同步(转)
出处:http://www.cnblogs.com/daizhj/archive/2009/11/18/1605293.html 事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案 ...
- 现代C++学习笔记之二入门篇1
现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...
- Deep Residual Learning for Image Recognition
Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun Microsoft Research {kahe, v-xiangz, v-sh ...
- Android Bundle传递数据
1.传递普通数据 Intent intent=new Intent(MainActivity.this,TwoActivity.class); Bundle bundle=new Bundle(); ...
- delphi TString使用(取有规律的字符串中某一项内容)
{目的,取得下面字符串中的每一项内容,如s:='a,b,c,d',要去的用逗号隔开的每一项内容这时采用Tstring,会方便很多,TString是继承TStringList带有List所有属性.} v ...
- React学习笔记4
遇到的问题 目前模板是自己任意定义的,样式不好控制 在组件设计时,可以把页面数据显示的地方,分割父子组件嵌套的结构,比如,商品数据显示列表,把组外层容器看成是父组件,里面是数据显示的渲染模板,看成是子 ...
- orcl 对table的一些操作
删除 table:drop table 表名: 恢复删除 : flashback table 表名 to before drop: 清空table : truncate table 表名; 恢复清空: ...