CodeForces 500E.New Year Domino

E. New Year Domino

  • 原题

Time Limit 2s

Memory Limit 256M

Celebrating the new year, many people post videos of falling dominoes; Here's a list of them: https://www.youtube.com/results?search_query=New+Years+Dominos

User ainta, who lives in a 2D world, is going to post a video as well.

There are n dominoes on a 2D Cartesian plane. i-th domino (1 ≤ i ≤ n) can be represented as a line segment which is parallel to the y-axis and whose length is li. The lower point of the domino is on the x-axis. Let's denote the x-coordinate of the i-th domino as pi. Dominoes are placed one after another, so p1 < p2 < ... < pn - 1 < pn holds.

User ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.

Also, if the s-th domino touches the t-th domino while falling down, the t-th domino will also fall down towards the right, following the same procedure above. Domino s touches domino t if and only if the segment representing s and t intersects.

See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (A), (B), (C) will also fall towards the right. However, domino (D) won't be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time.

The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes.

User ainta has q plans of posting the video. j-th of them starts with pushing the xj-th domino, and lasts until the yj-th domino falls. But sometimes, it could be impossible to achieve such plan, so he has to lengthen some dominoes. It costs one dollar to increase the length of a single domino by 1. User ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, i. e. if domino's length is increased in some plan, it doesn't affect its length in other plans. Set of dominos that will fall except xj-th domino and yj-th domino doesn't matter, but the initial push should be on domino xj.

Input

The first line contains an integer n (2 ≤ n ≤ 2 × 105)— the number of dominoes.

Next n lines describe the dominoes. The i-th line (1 ≤ i ≤ n) contains two space-separated integers pili (1 ≤ pi, li ≤ 109)— the x-coordinate and the length of the i-th domino. It is guaranteed that p1 < p2 < ... < pn - 1 < pn.

The next line contains an integer q (1 ≤ q ≤ 2 × 105) — the number of plans.

Next q lines describe the plans. The j-th line (1 ≤ j ≤ q) contains two space-separated integers xjyj (1 ≤ xj < yj ≤ n). It means the j-th plan is, to push the xj-th domino, and shoot a video until the yj-th domino falls.

Output

For each plan, print a line containing the minimum cost needed to achieve it. If no cost is needed, print 0.

Examples

input1

6
1 5
3 3
4 4
9 2
10 1
12 1
4
1 2
2 4
2 5
2 6

output1

0
1
1
2

Note

Consider the example. The dominoes are set like the picture below.

Let's take a look at the 4th plan. To make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) should be increased by 1, and the 5th domino (whose x-coordinate is 9) should be increased by 1 (other option is to increase 4th domino instead of 5th also by 1). Then, the dominoes will fall like in the picture below. Each cross denotes a touch between two dominoes.

  • 题意

给你domino骨牌的坐标和高度(按坐标从小到大给出),问你从推到x,要把其中的一些骨牌一共增加多少长度才可以使y倒下。

  • Solution

一开始想的太简单了,竟然只打了一个离散化和前缀和。

#include <cstdio>
#include <algorithm>
#define N 200010

using namespace std;
int n, p[N], l[N], po[N], cnt, f[N], u[N];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &p[i], &l[i]);
		po[++cnt] = p[i];
		po[++cnt] = p[i] + l[i];
	}
	sort(po + 1, po + cnt + 1);
	int m = unique(po + 1, po + cnt + 1) - (po + 1);
	printf("%d\n", m);
	for(int i = 1; i <= m; ++i) {
		printf("%d ", po[i]);
	}
	printf("\n");
	for (int i = 1; i <= n; ++i) {
		int o = lower_bound(po + 1, po + m + 1, p[i]) - po;
		f[o]++;
		printf("%d ", o);
		o = lower_bound(po + 1, po + m + 1, p[i] + l[i]) - po;
		f[o]--;
		printf("%d\n", o);
	}
	for (int i = 1; i <= m; ++i) {
		if (f[i] > 0) f[i] += f[i - 1];
		else if (f[i] < 0) {
			if (f[i - 1] + f[i] == 0) {
				u[i + 1] = po[i + 1] - po[i];
			}
			f[i] += f[i - 1];
		}
		else u[i + 1] = po[i + 1] - po[i];
		printf("%d ", f[i]);
	}
	printf("\n");
	for (int i = 1; i <= m; ++i) {
		u[i] += u[i - 1];
		printf("%d ", u[i]);
	}
	printf("\n");
	int q, ll, rr;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i) {
		scanf("%d%d", &ll, &rr);
		ll = lower_bound(po + 1, po + m + 1, p[ll]) - po;
		rr = lower_bound(po + 1, po + m + 1, p[rr]) - po;
		printf("%d %d %d\n", u[rr] - u[ll], ll, rr);
	}
	return 0;
}

后来发现有问题,因为如果在x与y之间有一个位置是被x前面的很高的骨牌覆盖掉的,就不能算。

很气啊,于是参考了一下来自: 码代码的猿猿的AC之路,后面半篇倍增是一种很好的方法(有码风强迫症)

#include <cstdio>
#define N 200005
#define INF 2100000000
 
using namespace std;

int n, dn, a[N], b[N], d[N], p[N][20], q[N][20];
 
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d%d", &a[i], &b[i]);
	}
	a[n] = INF;
	d[dn++] = n;
	p[n][0] = n;
	for (int i = n - 1; i >= 0; i--) {
		while (a[d[dn - 1]] + b[d[dn - 1]] <= a[i] + b[i]) dn--;
		p[i][0] = d[dn - 1];
		if (a[i] + b[i] >= a[d[dn - 1]]) q[i][0] = 0;
		else q[i][0] = a[d[dn - 1]] - a[i] - b[i];
		d[dn++] = i;
	}
	for (int j = 1; j < 20; j++) {
		for (int i = n; i >= 0; i--) {
			p[i][j] = p[p[i][j - 1]][j - 1];
			q[i][j] = q[i][j - 1] + q[p[i][j - 1]][j - 1];
		}
	}
	int t;
	scanf("%d",&t);
	while (t--) {
		int l, r, ans = 0;
		scanf("%d%d", &l, &r);
		l--;
		r--;
		for (int i = 19; i >= 0; i--) {
			if (p[l][i] <= r) {
				ans += q[l][i];
				l = p[l][i];
			}
		}
		printf("%d\n", ans);
	}
    return 0;
}

 

内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
数据集一个高质量的医学图像数据集,专门用于脑肿瘤的检测和分类研究以下是关于这个数据集的详细介绍:该数据集包含5249张脑部MRI图像,分为训练集和验证集。每张图像都标注了边界框(Bounding Boxes),并按照脑肿瘤的类型分为四个类别:胶质瘤(Glioma)、脑膜瘤(Meningioma)、无肿瘤(No Tumor)和垂体瘤(Pituitary)。这些图像涵盖了不同的MRI扫描角度,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构,为模型训练提供了丰富多样的数据基础。高质量标注:边界框是通过LabelImg工具手动标注的,标注过程严谨,确保了标注的准确性和可靠性。多角度覆盖:图像从不同的MRI扫描角度拍摄,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构。数据清洗与筛选:数据集在创建过程中经过了彻底的清洗,去除了噪声、错误标注和质量不佳的图像,保证了数据的高质量。该数据集非常适合用于训练和验证深度学习模型,以实现脑肿瘤的检测和分类。它为开发医学图像处理中的计算机视觉应用提供了坚实的基础,能够帮助研究人员和开发人员构建更准确、更可靠的脑肿瘤诊断系统。这个数据集为脑肿瘤检测和分类的研究提供了宝贵的资源,能够帮助研究人员开发出更准确、更高效的诊断工具,从而为脑肿瘤患者的早期诊断和治疗规划提供支持。
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值