我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为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 离散化区间覆盖的更多相关文章

  1. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  2. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  3. Codeforces 915E Physical Education Lessons

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

  4. Guess Your Way Out! II---cf 558D (区间覆盖,c++STL map 的使用)

    题目链接:http://codeforces.com/contest/558/problem/D 题意就是有一个二叉树高度为 h ,人站在根节点上,现在要走出去,出口在叶子节点上,有 q 条信息,每条 ...

  5. Mayor's posters 线段树区间覆盖

    题目链接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that t ...

  6. HDU2883 kebab(最大流判断满流 + 离散化 + 区间化点)

    [题意]: 有一个烤箱,烤箱在一个时刻最多考M个肉串,N个顾客,每个顾客有属性s,n,e,t s是来的时间,n是想要的肉串数量,e是最晚离开的时间,t是烤的时间(几分熟). 顾客的烤肉可以分开烤,比如 ...

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

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

  8. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  9. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

随机推荐

  1. 【转】request的cache-control和response cache-control不同点

    原文地址:http://www.cnblogs.com/lwhkdash/archive/2012/11/04/2748291.html HTTP协议中,关于一些头域的解释很模糊,网上的解释有些甚至是 ...

  2. CSS文本溢出处理

    1.超出层的高度和宽度时文本自动隐藏 overflow:hidden;text-overflow:ellipsis; 2.超出层的宽度时隐藏溢出的文本以...表示,Firefox不兼容,只隐藏溢出的文 ...

  3. CF407B Long Path

    好玩的题. 首先我们(看一下题解之后)发现当你第一次走到了一个点的时候,那么它之前的所有点一定都访问过了偶数次. 假设我们第一次走到了一个点$i$,那么$i - 1$一定访问了偶数次,那么第一次走$i ...

  4. Vue.js路由组件

    1.如果在创建项目中,没有自动安装vue router,那就自行安装.cnpm install vue-router --save vue-router两种模式 hash模式和history模式. 默 ...

  5. Eclipse中新建applet 错误

    出现的问题:  “错误,请单击以获取详细信息” Java Plug-in 1.6.0_45 使用 JRE 版本 1.6.0_45-b06 Java HotSpot(TM) Client VM 用户主目 ...

  6. <abbr> 元素的样式为显示在文本底部的一条虚线边框,当鼠标悬停在上面时会显示完整的文本(只要您为 <abbr> title 属性添加了文本)

    <abbr title="World Wide Web">WWW</abbr><br><abbr title="Real Sim ...

  7. Position Independent Code (PIC) in shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/下一文: ...

  8. MySQL性能调优与架构设计——第6章 MySQL Server 性能的相关因素

    第6章 MySQL Server 性能的相关因素 前言 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的大部 ...

  9. Tomcat与Web.xml配置

    1.编码配置 <Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableL ...

  10. 百度图片API

    转载请注明出处:http://blog.csdn.net/yuanwofei/article/details/16343743 一.通用api http://image.baidu.com/i?tn= ...