【離散化】線段樹練習題1

LinkLink

SSL 2644

DescriptionDescription

桌子上零散地放着若干個盒子,桌子的後方是一堵牆。如右圖所示。現在從桌子的前方射來一束平行光, 把盒子的影子投射到了牆上。問影子的總寬度是多少?

SampleSample InputInput

20   //桌面總寬度
4    //盒子數量
1 5 
3 8
7 10
13 19

SampleSample OutputOutput

15

HintHint

數據範圍 
1<=n<=100000,1<=m<=100000,保證座標範圍爲[1,n].

TrainTrain ofof ThoughtThought

離散化~~~~

CodeCode

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int L, M, k, a[200005], ans;

struct line
{
	int start, end;
}b[100005];

int main()
{
	int x, y;
	scanf("%d%d", &L, &M);
	for (int i = 1; i <= M; ++i) {
		scanf("%d%d", &x, &y);
		a[++k] = x; a[++k] = y;
		b[i].start = x; b[i].end = y;
	}//讀入
	
	sort(a + 1, a + k + 1);//排個序
	
	for (int i = 1; i <= k; ++i)
	 for (int j = 1; j <= M; ++j)
	 {
	    if (a[i] > b[j].start && a[i] <= b[j].end)//是否在範圍內
			{
				ans += a[i] - a[i - 1];
				break;
			}
	 }
	
	printf("%d", ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章