AcWing 1265. 数星星 树状数组 java 降维打击

🤠 原题地址
在这里插入图片描述
在这里插入图片描述
🥚 输入样例

5
1 1
5 1
7 1
3 3
5 5

🥚 输出样例

1
2
1
1
0

⭐ 缓冲流解决输入输出慢的问题
⭐ 由于单调输入,二维可转一维,降维打击
👨‍🏫 题目的 x 是从 0 开始的,但数组数组的下标得从 1 开始!安排:每个 x + 1

import java.util.*;
import java.io.*;

public class Main
{
	static int N = 32010;
	static int[] a = new int[N];
	static int[] tr = new int[N];
	static int[] level = new int[N];

	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	static int lowbit(int x)
	{
		return x & -x;
	}

	static void add(int x, int v)
	{
		for (int i = x; i < N; i += lowbit(i))
		{
			tr[i] += v;
		}
	}

	static int query(int x)
	{
		int res = 0;
		for (int i = x; i > 0; i -= lowbit(i))
			res += tr[i];

		return res;
	}

	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(in.readLine());

		for (int i = 0; i < n; i++)
		{
//			题目保证了星星的坐标是按 x、y 轴增序输入的,所以在 x 左方的一定是在 y 轴下方的
			String[] ss = in.readLine().split(" ");
			int x = Integer.parseInt(ss[0])+1;
			int y = Integer.parseInt(ss[1]);
			level[query(x)]++;
			add(x, 1);
		}
		for (int i = 0; i < n; i++)
			out.write(level[i]+"\n");
			
		out.flush();

	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值