CodeForces - 1000C Covered Points Count(差分+思维)

题目链接:点击查看

题目大意:给出n个区间,现在要求输出覆盖次数为1,2,3....n-1,n的点分别有多少个

题目分析:一开始看到区间问题想用线段树去做,但想了想又可以直接用差分去做,不过因为数比较大,所以用map代替差分数组,后续求前缀和的时候就可以实时维护答案了

代码:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<unordered_map>
using namespace std;
 
typedef long long LL;
 
const int inf=0x3f3f3f3f;
 
const int N=2e5+100;

LL ans[N];

map<LL,int>mp;

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		LL l,r;
		scanf("%lld%lld",&l,&r);
		mp[l]++;
		mp[r+1]--;
	}
	int cnt=0;
	for(map<LL,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		map<LL,int>::iterator next=it;
		next++;
		if(next==mp.end())
			break;
		cnt+=it->second;
		ans[cnt]+=next->first-it->first;
	}
 	for(int i=1;i<=n;i++)
 		printf("%lld ",ans[i]);
 
	
	
	
	
	
	
	
	
	
	return 0;
}
发布了549 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104076422