我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散

不然看一下test3就知道

不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重复的排序..)

线段树区间覆盖没啥好说的,自我感觉struct里写的足够清晰了

终于能睡个好觉了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 3e6+11;
  4. int ll[maxn],rr[maxn],mark[maxn];
  5. int ra[maxn],n,q;
  6. #define rep(i,j,k) for(int i = j; i <= k; i++)
  7. #define scan(a) scanf("%d",&a)
  8. #define scann(a,b) scanf("%d%d",&a,&b)
  9. #define println(a) printf("%lld\n",a)
  10. typedef long long LL;
  11. struct ST{
  12. LL sum[maxn<<2];
  13. int lazy[maxn<<2];
  14. #define lc o<<1
  15. #define rc o<<1|1
  16. void init(){memset(lazy,-1,sizeof lazy);}
  17. void pu(int o){sum[o]=sum[lc]+sum[rc];}
  18. void pd(int o,int l,int r){
  19. if(~lazy[o]){
  20. lazy[lc]=lazy[rc]=lazy[o];
  21. int m = l+r>>1;
  22. sum[lc]=1ll*(ra[m]-ra[l-1])*lazy[o];
  23. sum[rc]=1ll*(ra[r]-ra[m])*lazy[o];
  24. lazy[o]=-1;
  25. }
  26. }
  27. void build(int o,int l,int r){
  28. if(l==r){
  29. sum[o]=1ll*ra[r]-ra[l-1];
  30. return;
  31. }
  32. int m = l+r>>1;
  33. build(lc,l,m);
  34. build(rc,m+1,r);
  35. pu(o);
  36. }
  37. void update(int o,int l,int r,int L,int R,int v){
  38. if(L<=l&&r<=R){
  39. sum[o]=1ll*(ra[r]-ra[l-1])*v;
  40. lazy[o]=v;
  41. return;
  42. }
  43. pd(o,l,r);
  44. int m = l+r>>1;
  45. if(L<=m) update(lc,l,m,L,R,v);
  46. if(R>m) update(rc,m+1,r,L,R,v);
  47. pu(o);
  48. }
  49. LL query(int o,int l,int r,int L,int R){
  50. if(L<=l&&r<=R) return sum[o];
  51. pd(o,l,r);
  52. int m = l+r>>1;
  53. LL ans=0;
  54. if(L<=m) ans+=query(lc,l,m,L,R);
  55. if(R>m) ans+=query(rc,m+1,r,L,R);
  56. return ans;
  57. }
  58. }st;
  59. int main(){
  60. while(scann(n,q)^-1){
  61. rep(i,1,q) {scann(ll[i],rr[i]); scan(mark[i]);}
  62. rep(i,1,q) {ra[i]=ll[i]; ra[i+q]=rr[i];} ra[2*q+1]=1; ra[2*q+2]=n;
  63. sort(ra+1,ra+2*q+3);
  64. int qq = unique(ra+1,ra+2*q+3)-ra-1;
  65. int tot=qq;
  66. rep(i,1,qq-1){
  67. if(ra[i]+1<ra[i+1]) ra[++tot]=(ra[i]+1>=n?n:ra[i]+1);
  68. }
  69. sort(ra+1,ra+tot+1);
  70. tot=unique(ra+1,ra+tot+1)-ra-1;
  71. qq=tot;
  72. for(int i=qq;i>1;i--){
  73. if(ra[i]-1>ra[i-1]) ra[++tot]=(ra[i]-1<=1?1:ra[i]-1);
  74. }
  75. sort(ra+1,ra+tot+1);
  76. tot=unique(ra+1,ra+tot+1)-ra-1;
  77. 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;
  78. st.init();st.build(1,1,tot);
  79. rep(i,1,q){
  80. st.update(1,1,tot,ll[i],rr[i],(mark[i]==1?0:1));
  81. println(st.query(1,1,tot,1,tot));
  82. }
  83. }
  84. return 0;
  85. }

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. ZIP压缩格式与RAR压缩格式

    早已习惯了安装系统之后必须安装winrar,压缩文件也已经习惯了rar格式,这种习惯的力量真的挺可怕的.在工作中你的同事可能没有安装winrar,或者他们不喜欢安装盗版软件,这时候你给他们发送过去的是 ...

  2. Cookie存中文乱码的问题

    有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...

  3. easyui-tabs 页签绑定click事件,动态加载jqgrid

    .前台代码 <%-- builed by manage.aspx.cmt [ver:] at // :: --%> <%@ Page Language="C#" ...

  4. java - Logback获取方法名称

    java - Logback获取方法名称 摘自: https://blog.csdn.net/qq853632587/article/details/78222780 我们目前正在从 Log4J 迁移 ...

  5. Django-Web框架之创建项目和应用

    Django我们是基于python3来演示的.首先我们来安装一下django框架.使用pip3 install django安装的是最新的版本: 我们在pycharm中创建django工程.如图所示: ...

  6. LightOJ 1284 Lights inside 3D Grid (数学期望)

    题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量: 析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开 ...

  7. iOS play video

    iOS: How to use MPMoviePlayerController up vote6down votefavorite 3 I've created a blank project (iO ...

  8. UltraEdit 回车符替换空格

    查找和替换    输入 ^r^n   替换为:(空格)

  9. js作用域解析原理

    当代码进入到<script>标签或者在调用一个方法,那么就会进入作用域,在解析代码的时候就会做以下两件事情: ①去找var 和function关键字进行js预解析如果有var把值全部定义成 ...

  10. [日常]无线鼠标滚动缩放EXCEL表时,缩放比例过大问题

    这也是一个奇葩问题,解决方法: 把USB接收器拔掉重新插上,效果拔群