我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为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. suse配置dhcp服务器

    Suse  dhcp服务器安装在安装系统时勾选 Suse dhcp 默认配置文件 /etc/dhcpd.conf Suse dhcp 启动程序 /etc/init.d/dhcpd restart 配置 ...

  2. spark源码阅读之network(2)

    在上节的解读中发现spark的源码中大量使用netty的buffer部分的api,该节将看到netty核心的一些api,比如channel: 在Netty里,Channel是通讯的载体(网络套接字或组 ...

  3. hdu 4768 Flyer (异或操作的应用)

    2013年长春网络赛1010题 继巴斯博弈(30分钟)签到后,有一道必过题(一眼即有思路). 思路老早就有(40分钟):倒是直到3小时后才被A掉.期间各种换代码姿态! 共享思路: unlucky st ...

  4. 云存储上传控件(cloud2)-Xproer.CloudUploader

    版权所有 2009-2016荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  5. java 异常捕获与异常处理

    try{会产生异常的代码 }catch(出现异常的类型 e){ 异常出现后处理的方法 } 一旦异常出现又没有异常处理,程序就会中断. public static void main(String[] ...

  6. Kernel的意义

    在第7章最后一段讲到Kernel,Kernel就是用向量表示元素的和的乘积. Back in our discussion of linear regression, we had a problem ...

  7. myeclipse 8.0 注册码

    Subscriber:accptechSubscription Code:nLR8ZC-855550-6765855429037911 Subscriber:Hello World Subscript ...

  8. C++语法知识小结(持续更新中)

    1)在适用构造函数创建对象时,有时会创建临时对象.如 Stock::Stock(const std::string & co,long n,double pr); 在使用时,下面两条语句有根本 ...

  9. Diameter协议摘要

    ---------选择同学整理文档 1.   协议概述 Diameter协议主要为应用程序提供认证.鉴权.计费框架,即AAA,并支持本地AAA和漫游场景下的AAA. 1.1.  特点介绍 以前的AAA ...

  10. Spring MVC:Model、View、ModelAndView

    个人理解:View为服务器上的某个文件容器,可以为JSP,FTL等动态页面文件,甚至是媒体文件等等,单单是一个文件.Model的作用是存储动态页面属性,动态页面文件即View可以在Model中获取动态 ...