HDU 1556(Color the ball)

使用差分數組即可。

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 100005;

int t[MAXN]; //差分數組

int main()
{
	int N;
	while (cin >> N)
	{
		if (N == 0)
			break;

		memset(t, 0, sizeof(t));
		int a, b;
		for (int i = 0; i < N; i++)
		{
			cin >> a >> b;
			t[a]++; 
			t[b + 1]--;
		}
		for (int i = 1; i <= N; i++)
			t[i] += t[i - 1];
		for (int i = 1; i < N; i++)
			cout << t[i] << " ";
		cout << t[N] << endl;
	}
	return 0;
}

繼續加油。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章