HDU 1541 Stars solving report Fenwick tree

HDU 1541 Stars solving report

The meaning of problems: Each star has its own level, the level equal to the number of stars is located in the bottom left of stars (including star peer with columns), the output number of stars from 0 to N-1 of each level.
Problem-solving ideas: the input data is already sorted up, y useless, just consider x on the line, not difficult, with Fenwick tree on the line, look at the code. Remember to use while you enter n, or will wa.
Here Insert Picture Description

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 200010;
const int maxn = 1e9;
using namespace std;
int n;
int a[40000];
int sum[40000];
int lowbit(int x)
{
	return x & (-x);
}
void Add(int x)//将新星星加入
{
	for (int i = x; i <= 40000; i += lowbit(i))
	{
		a[i] += 1;
	}
}
int get_sum(int x)//获取等级
{
	int total = 0;
	for (int i = x; i >= 1; i -= lowbit(i))
	{
		total += a[i];
	}
	return total;
}
int main()
{
	while (~scanf("%d", &n))
	{
		memset(a, 0, sizeof(a));
		memset(sum, 0, sizeof(sum));
		int x, y;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &y);
			x++;
			sum[get_sum(x)]++;
			Add(x);
		}
		for (int i = 0; i < n; i++)
		{
			printf("%d\n", sum[i]);
		}
	}
}



Published 64 original articles · won praise 0 · Views 1445

Guess you like

Origin blog.csdn.net/weixin_45566331/article/details/104823504
Recommended