P1018

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值