IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> POJ 2481 Cows Java IndexTree 树形数组 -> 正文阅读

[Java知识库]POJ 2481 Cows Java IndexTree 树形数组

IndexTree 树形数组 POJ 2481 Cows
Description

Farmer John’s cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John’s N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].

But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.

For each cow, how many cows are stronger than her? Farmer John needs your help!
Input

The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.

The end of the input contains a single 0.
Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.

Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0

题目大意: 水平坐标上,有N个区域,每个区域的起始坐标为S,终点坐标E,求各区域跟其他N-1个区域相比,有多少个比他的范围大的,例:有i,j两个区域[Si,Ei]和[Sj,Ej],果Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj,则第i个区域范围大。

Input
3
1 2
0 3
3 4
0
此case中,比[1,2] 范围大的有[0,3],1个.
没有比[0,3]范围大的, 0个
也没比[3,4]范围打的,0个。
所以output: 1 0 0

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * Index Tree
 * POJ 2481
 */
public class Main {
    static int[] tree;

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        st = new StringTokenizer(bf.readLine());

        int N = Integer.parseInt(st.nextToken());
        while (N != 0) {
            Range[] range = new Range[N];
            int max = 0;
            for (int i = 0; i < N; i++) {
                st = new StringTokenizer(bf.readLine());
                int s = Integer.parseInt(st.nextToken());
                int e = Integer.parseInt(st.nextToken());
                max = Math.max(max, e); //水平坐标最大值,即叶子结点上的最大值
                range[i] = new Range(i, s, e);
            }

            Arrays.sort(range);
            int len = 1;
            while (len < max) {  //计算叶子节点的长度2^n
                len *= 2;
            }

            tree = new int[2 * len]; //tree的长度

            int[] ans = new int[N];
            int consist = 0;

            for (int i = 0; i < N; i++) {
                if (i == 0) {
                //第一头牛,没有比他范围大的,update为0
                    ans[range[i].idx] = 0;
                    update(range[i].e + len - 1);
                    continue;
                }

                if (range[i].e == range[i - 1].e && range[i].s == range[i - 1].s) {
                //连续两个牛的范围一样,因为前牛update时,会在tree中+1,
                //后牛query会查到+1后的值,所以后牛要减去范围一样的牛。
                    consist++;
                } else {
                    consist = 0;
                }
                
                //query,update都是模板代码。
                int cnt = query(range[i].e + len - 1, len + max - 1) - consist;
                ans[range[i].idx] = cnt;
                update(range[i].e + len - 1);
            }

            for (int i = 0; i < ans.length; i++) {
                System.out.print(ans[i] + " ");
            }

            System.out.println();

            st = new StringTokenizer(bf.readLine());

            N = Integer.parseInt(st.nextToken());
        }

        bf.close();
    }

    private static void update(int node) {
        int p = node;
        while (p > 0) {
            tree[p]++;
            p >>= 1;
        }
    }

    private static int query(int start, int end) {
        int s = start;
        int e = end;
        int res = 0;

        while (s <= e) {
            if (s % 2 == 1) {
                res += tree[s];
            }

            if (e % 2 == 0) {
                res += tree[e];
            }

            s = (s + 1) >> 1;
            e = (e - 1) >> 1;
        }

        return res;
    }

}

class Range implements Comparable<Range> {
    int idx;
    int s;
    int e;

    public Range(int idx, int s, int e) {
        this.idx = idx;
        this.s = s;
        this.e = e;
    }

    public int compareTo(Range o) {
        if (this.s == o.s) {
            return o.e - this.e;
        } else {
            return this.s - o.s;
        }
    }
}
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-08 11:07:41  更:2021-08-08 11:11:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 2:39:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码