hdu-1566-Color the ball-线段树-java
来源:互联网 发布:syslog 数据库 编辑:程序博客网 时间:2024/06/10 22:43
当N = 0,输入结束。
31 12 23 331 11 21 30
1 1 13 2 1
解题思路: 线段树水题 类型区间更新 就是传说中的懒惰标记
我是先放在呢 等最后输出的时候再全部更新掉
这题不能用数组写线段树 内存会炸掉 因为这题时间给的很充裕但是内存跟c是一样的 没翻2倍
心塞
下面是代码:
import java.util.Scanner;
public class Main {
private static int n;
private static Node head;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
while (true) {
n = scanner.nextInt();
if(n==0)return;
Node l = createTree( 1, (1+n)/2);
Node r = createTree((1+n)/2+1, n);
head = new Node(0, l, r,1,n);
for (int i = 0; i < n; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
update(head,a,b);
}
scan(head,0);
System.out.println();
}
}
private static void scan(Node head2,int k) {
head2.date+=k;
if (head2.left==null&&head2.right==null) {
System.out.print(head2.s==1?head2.date:" "+head2.date);
}else {
scan(head2.left, head2.date);
scan(head2.right, head2.date);
}
}
private static void update(Node head2, int a, int b) {
if (head2.s>=a&&head2.e<=b) {
head2.date += 1;
}else if (head2.s>b||head2.e<a) {
}else {
update(head2.left, a, b);
update(head2.right, a, b);
}
}
private static Node createTree( int s, int e) {
if(s>e)return null;
if(s==e)return new Node(0, null, null, s, e);
Node l = createTree(s, (s+e)/2);
Node r = createTree((s+e)/2+1, e);
return new Node(0, l, r,s,e);
}
}
class Node{
Node left;
Node right;
int date;
int s;
int e;
public Node(int d,Node l,Node r,int s,int e) {
date = d;
left = l;
right = r;
this.s = s;
this.e = e;
}
}
- hdu 1566 Color the ball---线段树
- hdu-1566-Color the ball-线段树-java
- HDU 1556 Color the ball 线段树
- HDU 1199 Color the Ball (线段树)
- HDU 1556 Color the ball (线段树)
- HDU 1556 Color the ball 线段树
- hdu 1556 Color the ball(线段树)
- hdu 1556 Color the ball 线段树
- HDU 1556 Color the ball 线段树
- HDU 1556 Color the ball [线段树]
- HDU 1556 Color the ball (线段树)
- hdu - 1556 (Color the ball 线段树)
- hdu 1566 Color the ball(线段树区间更新)
- Color the ball (线段树)
- Color the ball(线段树)
- Color the ball(线段树)
- hdu 1556 Color the ball(线段树的另一种应用)
- hdu 1556 Color the ball(线段树+懒惰标记)
- 构建Spring Restful API请求参数解析
- 关于哈希,分布式哈希表,一致性哈希
- Python 使用 asyncio 包处理并发
- 如何利用颜色和线条让表格更有魅力以奖金分配表为例
- 吝啬的国度 深搜
- hdu-1566-Color the ball-线段树-java
- 设计模式(23个)
- 《剑指offer》刷题笔记(数组):构建乘积数组
- 《Java源码分析》:LinkedHashMap
- 【TX1学习与开发系列】(一)介绍与刷机安装、配置
- java中数据结构(二)双链表总结
- 基于哈希的图像检索框架
- https 证书实现
- js中取得不同精度的时间,时间戳,时间差的方法