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. (C语言)每日代码||2023.12.21||C语言预处理命令,#define、#line、__LINE__、__FILE__

    #include <stdio.h> #define AAA 111 void test() { printf("__LINE__ = % d\n", __LINE__ ...

  2. 在QEMU-KVM环境下部署Oracle 19.16 RAC

    KVM环境和其他虚拟化或真实生产最大差异主要就是在实施前期准备工作上: 具体在 DB节点 和存储环境 的准备工作上有差异,本文会详细说明. 而剩余基本软件安装和补丁应用部分无差异,若不清楚可以直接参考 ...

  3. IDE 不用鼠标 向下选择

  4. 吉特日化MES & 日化制药工厂信息化系统架构图

    作者:情缘   出处:http://www.cnblogs.com/qingyuan/ 关于作者:从事仓库,生产软件方面的开发,在项目管理以及企业经营方面寻求发展之路 版权声明:本文版权归作者和博客园 ...

  5. AT_arc125_c [ARC125C] LIS to Original Sequence 题解

    题目传送门 前置知识 贪心 | 构造 解法 对于任意一个未加入序列 \(P\) 的数 \(x<A_{i}(1 \le i \le k-1)\),如果其放在了 \(A_{i}\) 的前面,会导致最 ...

  6. 反悔贪心&模拟费用流

    贪心是一种常用的算法,它能够获得局部最优解,但我们往往需要的是全局最优解,所以我们在贪心的时候加入和反悔的机制,让他能够得到全局最优解. 由于网络流中的退流操作本质上也是反悔贪心,所以在实现反悔贪心时 ...

  7. JS Leetcode 374. 猜数字大小 题解分析

    壹 ❀ 引 本题来自LeetCode 374. 猜数字大小,题目难度简单,与昨天的题目一样,也是一道标准二分法的题目,不知道是不是端午节的缘故,这两天的题目都比较简单,题目描述如下: 猜数字游戏的规则 ...

  8. Linux查看系统版本的方法

    记录几种查看当前Linux系统的版本的方法 一.使用命令:cat /proc/version 查看 linux版本号:Linux version 5.4.0-99-generic (buildd@lg ...

  9. Js捕获异常的方法

    Js捕获异常的方法 JavaScript的异常主要使用try catch finally语句以及窗口对象window的onerror事件来捕获. try catch finally try catch ...

  10. 将docker镜像推送到阿里云镜像仓库

    1.注册阿里云账号(支付宝扫码登录也可以) 进入控制台,找到[容器镜像服务] 2.创建命名空间 3.创建镜像仓库 4.设置授权凭证 5.登录 docker login --username=index ...