package sw.pro.prictice.P0108;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Solution {
static StreamTokenizer in;
static final int[] tree = new int[800000];
/*
parm[0] -->-1:update; 0:query;1:update
parm[1] 保存x -->排序(升序)
parm[2] 保存y值-->update时,更新位置;query时开始的位置
parm[3] 保存y值--> query时结束的位置
* */
static final PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
//排序规则,按照parm[1](x值)升序,如果parm[1]相等,先update +1,再query,最后update -1
return o1[1] == o2[1] ? o2[0] - o1[0] : o1[1] - o2[1];
}
});
static int N, offset, ans;
public static void main(String[] args) throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int T = nextInt();
for (int tc = 1; tc <= T; tc++) {
readCase();
work();
System.out.println("#" + tc + " " + ans);
}
}
static void work() {
ans = 0;
while (!pq.isEmpty()) {
int[] line = pq.poll();
if (line[0] == 0) {
ans += query(line[2],line[3]);
} else {
update(line[2],line[0]);
}
}
Arrays.fill(tree, 0, (offset + 1) << 1, 0);
}
static int query(int l, int r) {
int s = l + offset;
int e = r + offset;
int sum = 0;
while (s <= e) {
if (s % 2 == 1) sum += tree[s++];
if (e % 2 == 0) sum += tree[e--];
s >>= 1;
e >>= 1;
}
return sum;
}
static void update(int pos, int val) {
int idx = pos + offset;
while (idx > 0) {
tree[idx] += val;
idx >>= 1;
}
}
static void readCase() throws IOException {
N = nextInt();
offset = 1;
while (offset < N) offset <<= 1;
offset--;
for (int i = 0; i < N; i++) {
int x1 = nextInt();
int y1 = nextInt();
int x2 = nextInt();
int y2 = nextInt();
if (x1 == x2) {
pq.add(new int[]{
0, x1, y1, y2});
} else {
pq.add(new int[]{
1, x1, y1, 0});
pq.add(new int[]{
-1, x2, y1, 0});
}
}
}
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
}
P1018
猜你喜欢
转载自blog.csdn.net/awp0011/article/details/118255834
今日推荐
周排行