leetccode count-integers-in-intervals 线段树解决:

class CountIntervals {
Seg root; public CountIntervals() {
root = new Seg(1, 1000000000);
} public void add(int left, int right) {
root.add(left, right);
} public int count() {
return root.cnt;
} public static void main(String[] args) {
CountIntervals countIntervals = new CountIntervals();
System.out.println(countIntervals.count());
countIntervals.add(39, 44); System.out.println(countIntervals.count());
countIntervals.add(13, 49);
System.out.println(countIntervals.count());
System.out.println(countIntervals.count());
countIntervals.add(47, 50);
} class Seg {
int cl, cr;
int cnt;
boolean tag;
Seg l, r; public Seg(final int cl, final int cr) {
this.cl = cl;
this.cr = cr;
// this.cnt = cr - cl + 1;
this.cnt = 0;
this.tag = false;
} public void pushdown() {
if (this.tag) {
int mid = (cl + cr) >> 1;
if (this.l == null) {
this.l = new Seg(cl, mid);
}
if (this.r == null) {
this.r = new Seg(mid + 1, cr);
}
this.l.tag = true;
this.r.tag = true;
this.l.cnt = mid - cl + 1;
this.r.cnt = cr - mid;
}
} public void pushup() {
/// this.cnt = 0; this.tag = this.l.tag & this.r.tag;
this.cnt = this.l.cnt + this.r.cnt; } public void add(int L, int R) {
if (cl == L && cr == R) {
this.tag = true;
this.cnt = R - L + 1;
return;
} if (this.tag) {
return;
} // pushdown();
int mid = (cl + cr) >> 1;
if (this.r == null) {
this.r = new Seg(mid + 1, cr);
}
if (this.l == null) {
this.l = new Seg(cl, mid);
}
if (R <= mid) {
this.l.add(L, R);
} else {
if (L > mid) {//再右边加入
this.r.add(L, R);
} else {
this.l.add(L, mid);
this.r.add(mid + 1, R);
}
}
pushup();
}
}
} /**
* Your CountIntervals object will be instantiated and called as such:
* CountIntervals obj = new CountIntervals();
* obj.add(left,right);
* int param_2 = obj.count();
*/

 

【leetcode https://leetcode.cn/problems/count-integers-in-intervals/】 线段树的更多相关文章

  1. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. Count the Colors(线段树染色)

    Count the Colors Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Submit ...

  3. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  4. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  5. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  6. F - Count the Colors(线段树)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  7. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  8. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

  9. ZOJ-1610 Count the Colors(线段树染色,求染色段)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...

  10. ZOJ - 1610 Count the Colors(线段树区间更新)

    https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...

随机推荐

  1. Cpu是如何选择线程的?

    Cpu是如何选择线程的? linux中线程存放格式 linux中线程与进程对应的结构体都是task_struct 唯一不同的点在于线程存放的东西少了点(由于一个进程中的线程们是共享一定数据的那些东西就 ...

  2. 用superxmlparser.pas的XMLParseString----XML转Json注意

    了解XML转成Json时候用的时候多了个#号: ---------------------------------------------------------------------------- ...

  3. google三驾马车之一:Bigtable解读(英文版)

    本文重点关注了系统设计相关的内容,paper后半部分的具体应用此处没有过多涉及.从个人笔记修改而来,因此为英文版本. Bigtable: A Distributed Storage System fo ...

  4. JS leetcode 杨辉三角Ⅱ 题解分析

    壹 ❀ 引 今天是的题目来自leetcode的119. 杨辉三角 II,还记得几天前,我第一次遇到118. 杨辉三角,一段代码调试半天写不出来,这次遇到升级版终于开开心心快快乐乐轻松解题,题目描述如下 ...

  5. 使用yum查询系统安装的软件及可以更新的软件并单独指定升级某一个软件

    Linux系统下yum命令查看安装了哪些软件包: $yum list installed //列出所有已安装的软件包 yum针对软件包操作常用命令: 1.使用YUM查找软件包 命令:yum searc ...

  6. Java 根据Map的值对 List<Map<String, Object>> 进行排序

    对 List<Map<String, Object>> 类型数据的排序 有一个Map列表, 需要对这个列表, 按Map的某几个value进行排序, 并且还要分别指定正序或者倒序 ...

  7. win32-LPCSTR->String

    #include <string> void makebox(LPCSTR name) { std::string res(name); res += " is X"; ...

  8. 详解SSL证书系列(1)什么是SSL证书?

    你一定遇到过这种情况,打开一个网站,浏览器弹出警告"您与此网站之间建立的连接不安全.由于此连接不安全,因此信息(如密码或信用卡)不会安全地发送到此网站,并且可能被其他人截获或看到" ...

  9. 麒麟系统开发笔记(九):在国产麒麟系统上搭建宇视摄像头SDK基础环境Demo

    前言   国产麒麟系统开发上,使用宇视摄像头,本篇使用宇视官网的提供的SDK,搭建基础的国产系统上宇视摄像头SDK开发化境Demo.   效果演示      宇视SDK下载   CSDN粉丝0积分下载 ...

  10. django中如果不是第一次迁移的时候就配置AUTH_USER_MODEL(用来告知django认证系统识别我们自定义的模型类),那么该如何解决才能让django的认证系统识别且不会报未知错误?

    Django认证系统中提供的用户模型类及方法很方便,我们可以使用这个模型类,但是字段有些无法满足项目需求,如还需要保存用户的手机号,需要给模型类添加额外的字段. Django提供了django.con ...