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. TfrxReport.Clear。尽量慎用。

    for MyXuHaoKey in MyXuHaoJianRongSanJieKouDataDicApi.KeySortList do begin //标记下打印编号,吸入淘打的客户 MyTradeA ...

  2. java 从零开始手写 RPC (02)-netty4 实现客户端和服务端

    说明 上一篇代码基于 socket 的实现非常简单,但是对于实际生产,一般使用 netty. 至于 netty 的优点可以参考: 为什么选择 netty? http://houbb.github.io ...

  3. MySQL8.0使用mysqlsh配置主从复制 InnoDB ReplicaSet

    InnoDB ReplicaSet InnoDB ReplicaSet 由一个主节点和多个从节点构成. 可以使用ReplicaSet对象和AdminAPI操作管理复制集, 例如检查InnoDB复制集的 ...

  4. 贝壳云P1刷机记录(5.10内核Armbian)

    说明 贝壳云基于瑞芯微的RK3328芯片, 芯片介绍, Cortex-A53架构, 4核, 1G内存, 8G eMMC. 板载1个千兆网口, 4个USB3.0. 这个盒子比较赞的地方就是不到百元的价格 ...

  5. win32 - 创建子线程中的窗口

    跟创建普通的win32窗口一样,线程中的窗口也需要注册和窗口处理过程 // Test_WM_CLOSE.cpp : Defines the entry point for the applicatio ...

  6. 【Android 逆向】【攻防世界】Ph0en1x-100

    1. apk 安装到手机,老套路需要输入flag 2. jadx 打开apk,没有加壳 ...... public void onGoClick(View v) { String sInput = t ...

  7. 【Android逆向】破解看雪9月算法破解第三题

    这题的目标是算法还原,并写出注册机 1. 9月份算法第一题.apk 安装到手机 2. 随意输入账号密码,提示错误 3. apk拖入到jadx中 public native boolean regist ...

  8. 学Python只需一张图

    有编程基础的人一看就可以了解 Python 的用法了.真正的 30 分钟上手.

  9. 最经典的TCP性能问题

    目录 问题描述 问题的原因 什么是delay ack 什么是Nagle算法 如果client启用Nagle,并且server端启用了delay ack会有什么后果呢? 再来看一个经典例子和数据分析 回 ...

  10. 【Azure 存储服务】Storage Account Blob 使用REST API如何获取磁盘大小(Content-Length), IOPS信息

    问题描述 1)关于使用Rest API获取非托管磁盘信息比如获取磁盘大小 2)关于使用Rest API获取非托管磁盘信息比如iops 问题答案 #1:关于使用Rest API获取非托管磁盘信息比如获取 ...