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;
}
}
}