数组:4.覆盖的点数

【问题描述】

       给定坐标轴上的 n 个区间段,每个段的端点为整数坐标。有些段可以是一个点,可以彼此相交、相互嵌套,甚至重合,对于任意区间,0≤li ≤ ri≤6×105

       任务如下:对于每个 k ∈[1..n],计算被 k 个区间段覆盖的整数坐标点的个数。点 x被端点为 li  ri  的区间段覆盖,当且仅当 li≤ ri

【输入形式】

       输入的第一行为一个整数 n(1≤ n ≤ 2⋅ 105),区间段的个数。

       接下来的 n 行,每行一个整数对 li 和 ri,表示第 i 个区间的端点。
【输出形式】

       输出 n 个用空格分隔的整数cnt1、cnt2、...、cntn,这里 cnti 为被 i 个区间覆盖的点的个数。

【样例输入1】

3
0 3
1 3
3 8

【样例输出1】

6 2 1

【样例输入2】

3
1 3
2 4
5 7

【样例输出2】

5 2 0

【样例说明】

样例1描述如下:

坐标点0、4、5、6、7、8被1个区间段覆盖,点1、2被2个区间段覆盖, 点3倍3个区间段覆盖。

样例2描述如下:

坐标点1、4、5、6、7被1个区间段覆盖,点2、3被两个区间段覆盖,每有点被3个区间段覆盖Points 

【评分标准】

#include<iostream>
using namespace std;
int main()
{
	int n,l,r;
	int arr[60];
	cin>>n;
	int arr2[n];
	for(int j=0;j<60;j++)
	{
		arr[j]=0;//对数组arr中元素进行依次归零
		arr2[j]=0;
	}
	/*-------------------
	      越界开始 
    --------------------*/ 
	for(int i=0;i<n;i++)
	{
		cin>>l>>r;
		for(int k=l;k<=r;k++)
		{
			arr[k]++;
		} 
	}                               //截至此步为止arr中每个元素都为重复多少次了 
	for(int m=1;m<=n;m++)
	{
		for(int p=0;p<60;p++)
		{
			if(arr[p]==m)
			arr2[m]++;
		}
	}
	/*-------------------
	      越界结束 
    --------------------*/
	for(int i=1;i<=n;i++)
	{
			cout<<arr2[i]<<" ";
	}
	system("pause");
	return 0;
}

这样编运行时结果正确,但是会造成数组越界

所以可以使用指针,分别给两个数组分配堆内存:

#include<iostream>
using namespace std;
int main()
{
	int n,l,r,len;
	len=600000;
	int *arr=new int[len];
	cin>>n;
	int *arr2=new int[n+1];
	for(int j=0;j<len;j++)
	{
		arr[j]=0;//对数组arr中元素进行依次归零
	}
	for(int j=0;j<n+1;j++)
	{
		arr2[j]=0;
	}
	for(int i=0;i<n;i++)
	{
		cin>>l>>r;
		for(int k=l;k<=r;k++)
		{
			arr[k]++;
		} 
	}//截至此步为止arr中每个元素都为重复多少次了 
	for(int m=1;m<=n;m++)
	{
		for(int p=0;p<len;p++)
		{
			if(arr[p]==m)
			arr2[m]++;
		}
	}
	for(int i=1;i<=n;i++)
	{
			cout<<arr2[i]<<" ";
	}
	delete[]arr;
	system("pause");
	return 0;
}

reset() { // 1. 强制停止所有状态 console.log('停止所有状态'); this.isPlaying = false; this.isPaused = false; this.currentIndex = 0; // 2. 清除所有动画 if (this.animationTimer) { console.log('清除动画定时器'); clearTimeout(this.animationTimer); this.animationTimer = null; } // 3. 检查数据有效性 if (!this.coordinate || this.coordinate.length === 0) { console.warn('无有效坐标数据'); return; } const startPoint = this.coordinate[0]; console.log('起始点坐标:', startPoint); // 4. 双重更新策略(关键修改) this.$set(this.covers, 0, { ...this.covers[0], latitude: startPoint.latitude, longitude: startPoint.longitude, rotate: 0, }); console.log('更新 covers 数组中的位置:', this.covers); this.covers = [{ id: 1, latitude: startPoint.latitude, longitude: startPoint.longitude, // #ifdef MP-WEIXIN width: 20, height: 20, rotate: 0, iconPath: '../../static/cheche.png', // #endif // #ifdef APP-PLUS width: 5, height: 5, rotate: 0, iconPath: '../../static/chechex3.png', // #endif anchor: { x: 0.55, y: 0.55, }, }]; console.log('更新后的 covers:', this.covers); // 5. 强制地图刷新(多平台兼容) this.$nextTick(() => { console.log('触发 nextTick,准备更新地图'); if (!this.map) { console.log('创建 map 实例'); this.map = uni.createMapContext('map', this); } // 方法2:标记位置更新(微信小程序必需) console.log('调用 translateMarker 更新标记位置'); this.map.translateMarker({ markerId: 1, destination: { latitude: startPoint.latitude, longitude: startPoint.longitude }, rotate: 0, duration: 50, }); }); // 微信小程序特殊处理(关键) // #ifdef MP-WEIXIN setTimeout(() => { if (this.map.updateComponents) { console.log('调用 updateComponents 强制刷新地图'); this.map.updateComponents(); } this.map.updateComponents && this.map.updateComponents(); console.log('微信地图强制刷新111111'); }, 500); // #endif }
07-01
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一二爱上蜜桃猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值