【leetcode https://leetcode.cn/problems/count-integers-in-intervals/】 线段树
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/】 线段树的更多相关文章
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- Count Color POJ - 2777 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- F - Count the Colors(线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
随机推荐
- 覆盖第三方jar包中的某一个类。妙!!
在我们日常的开发中,经常需要使用第三方的jar包,但是很多时候总是会发现第三方的jar包中的某一个类,有问题,但是又无法继承,因为你继承后 变成了你自己的,jar包中 调用的 还是 他自己内部包含的, ...
- DbgridEh 1900-01-01 00:00:00 问题解决
--------------------------------------------------
- 《ASP.ENT Core 与 RESTful API 开发实战》-- (第4章)-- 读书笔记(下)
第 4 章 资源操作 4.5 创建资源 由于创建资源的 Id 会在服务端生成,因此在创建资源时,不建议使用与获取数据时相同的 DTO,而要单独创建一个新的 DTO 类,并通过数据注解特性对相应 的属性 ...
- .NET应用程序7种最常见的性能问题及其解决方案
译者注:这篇文章依然是介绍.NET Framework框架下的性能问题排查,可能并不直接适用于.NET Core,但有时也能提供一些参考. .NET应用程序7种最常见的性能问题及其解决方案 原文地 ...
- typescript 实现enum枚举值定义为对象
壹 ❀ 引 最近因为有一些闲散时间,所以一直在做将Class组件重构为typescript + hooks组件的工作,结果今天就遇到一个有趣的问题.我们知道react Class组件一般都会定义Com ...
- Linux 中竖线“|”与双竖线“||”的意思
linux中竖线'|',双竖线'||',&和&&的意思 对于初学者来说这几个意思可能只知道其中几个的意思,下面我们来看一下. 1.竖线'|' ,在linux中是作为管道符的,将 ...
- Js中Math对象
Js中Math对象 Math是一个内置对象,它拥有一些数学常数属性和数学函数方法,Math用于Number类型,其不支持BigInt. 描述 Math不是一个函数对象,也就是说Math不是一个构造器, ...
- Fiddler捕获Java发送的HttpURLConnection请求
1.说明 平常使用Fiddler抓包工具查看浏览器的请求和响应信息很方便, 但有时候我们也需要拦截java代码执行的http请求. 以便更好的调试程序.具体方法如下: 2.编写Java代码 // 配置 ...
- python字符串模板文本处理之Template
from string import Template s = Template('$who 在 $do') ts = s.substitute(who="张三", do=&quo ...
- docker 发布.net core 项目(linux)
一.准备阶段:前提:一台linux系统,安装好了Docker并启动 1.上传.netcore项目压缩文件 2.解压 注:若没有解压软件,先下载rar解压软件再安装:需注意系统是64位还是32 (下 ...