我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为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. 如何从Win7上卸载Sql 2008 R2 Express,再重装

    这两天,因工作需要,需要在一台新的机器上,Win7 64位英文操作系统上,安装Sql Server 2008 R2 Express,安装的过程中出现些问题,在查找问题的过程中,考虑重装 Sql Ser ...

  2. linux环境配置与使用合集

    配置linux和samba共享 1. 安装linux操作系统 2. 通过windows操作系统ping linux看看是否可以ping通 3. 相关软件安装 a. 安装samba sudo apt-g ...

  3. chrome浏览器-Toolbar工具条不显示

    来源于<sencha touch 权威指南> ------------------------------------ 工具条按钮在chrome下不显示,不知是不支持还是代码有问题.app ...

  4. 16.数据类型(data_type)

    CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...

  5. eclipse——配置maven插件

    Step 1 配置installations installations:指定Maven核心程序的位置  从本地磁盘中找到本地maven的位置 Step 2 配置user settings user ...

  6. 字符串的查找删除---C++中string.find()函数与string::npos

    给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串 输入: 输入只有一组数据 输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止 输出: 删除输入的短字符串 ...

  7. 用JQuery获取输入框中的光标位置

    (function ($, undefined) { $.fn.getCursorPosition = function () { var el = $(this).get(0); var pos = ...

  8. HTTP Debugger Pro安装教程

    相关链接:HTTP Debugger Pro使用教程 安装步骤:   1.解压压缩包   2.双击运行安装文件   3.根据向导提示点击Next   4.选择接受协议,点击Next   5.选择高级模 ...

  9. Struts2 让跳转指定执行某个方法

    很多时候,我们想让jsp页面中的某个超链接,点击后执行后台的某个方法,里面该如何做呢? 这里方法很多种 我举例两种: 1.在struts.xml配置,配置如下: <package name=&q ...

  10. netty使用以及聊天小程序

    <从零开始搭建游戏服务器>Netty导入创建Socket服务器 Netty入门教程 Netty 聊天小程序