【CF500E】 New Year Domino

题目

题目描述
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 nn dominoes on a 2D Cartesian plane. ii -th domino ( 1<=i<=n1<=i<=n ) can be represented as a line segment which is parallel to the yy -axis and whose length is l_{i}l
i

. The lower point of the domino is on the xx -axis. Let’s denote the xx -coordinate of the ii -th domino as p_{i}p
i

. Dominoes are placed one after another, so p_{1}<p_{2}<…<p_{n-1}<p_{n} 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 ss -th domino touches the tt -th domino while falling down, the tt -th domino will also fall down towards the right, following the same procedure above. Domino ss touches domino tt if and only if the segment representing ss and tt intersects.

See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and ©. As a result, dominoes (A), (B), © 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 © 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 qq plans of posting the video. jj -th of them starts with pushing the x_{j}x
j

-th domino, and lasts until the y_{j}y
j

-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 11 . 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 x_{j}x
j

-th domino and y_{j}y
j

-th domino doesn’t matter, but the initial push should be on domino x_{j}x
j

.

输入格式
The first line contains an integer nn ( 2<=n<=2×10^{5}2<=n<=2×10
5
)— the number of dominoes.

Next nn lines describe the dominoes. The ii -th line ( 1<=i<=n1<=i<=n ) contains two space-separated integers p_{i}p
i

, l_{i}l
i

( 1<=p_{i},l_{i}<=10^{9}1<=p
i

,l
i

<=10
9
)— the xx -coordinate and the length of the ii -th domino. It is guaranteed that p_{1}<p_{2}<…<p_{n-1}<p_{n} .

The next line contains an integer qq ( 1<=q<=2×10^{5}1<=q<=2×10
5
) — the number of plans.

Next qq lines describe the plans. The jj -th line ( 1<=j<=q1<=j<=q ) contains two space-separated integers x_{j}x
j

, y_{j}y
j

( 1<=x_{j}<y_{j}<=n ). It means the jj -th plan is, to push the x_{j}x
j

-th domino, and shoot a video until the y_{j}y
j

-th domino falls.

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

题意翻译
有n个多米诺骨牌,从左到右排列,每一个骨牌都有一个高度Li,向右推倒,它会直接向右倒下,如下图,倒下后该骨牌的顶端落在Xi+Li的位置,(Xi是它位于的坐标,即倒下时该骨牌不会发生移动)

在倒下过程中,骨牌会碰到其他骨牌,碰到的骨牌会向右倒,如下图,最左边的骨牌倒下会碰倒A,B,C,A,B,C会倒下,但是不会直接碰到D,但是D会因为C的倒下而碰倒。

现在给你N个骨牌的坐标Xi,和每个骨牌的高度Li。则一个骨牌能碰倒另一个骨牌当切仅当xi+li≥xj。同时有Q个询问 [L,R],问向右推到第L个骨牌,最少需要多少代价让R倒下。你可以临时增加某个骨牌的高度,增加1个高度的代价是1.

输入输出样例
输入 #1复制
6
1 5
3 3
4 4
9 2
10 1
12 1
4
1 2
2 4
2 5
2 6
输出 #1复制
0
1
1
2
说明/提示
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.

思路

发现对于区间(l,r)询问的答案就是将骨牌全推倒后,未被覆盖的区间长度之和

这个可以离散化后用线段树维护

然而我们仍然发现,左边的骨牌不该对右边的区间询问造成影响

我们按照询问左端点从大到小排序,逐个添加骨牌即可

代码

#include<bits/stdc++.h>
using namespace std;

const int N=1<<18;

int n,m,mx;
int siz[N<<1];
int tree[N<<2];
bool tag[N<<2];
int ans[N];
struct rpg{
	int x,y,id;
}a[N<<1],b[N];

int read()
{
	int x=0;char ch=getchar();
	while(ch<'0'||'9'<ch) ch=getchar();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x;
}

bool cmp1(rpg a,rpg b){return a.x<b.x;}
bool cmp2(rpg a,rpg b){return a.id<b.id;}
bool cmp3(rpg a,rpg b){return a.x>b.x;}
void build(int k,int l,int r)
{
	if(l==r){tree[k]=siz[l];return;}
	int i=k<<1,mid=l+r>>1;
	build(i,l,mid);build(i|1,mid+1,r);
	tree[k]=tree[i]+tree[i|1];
	return;
}

void ctag(int k,int l,int r,int le,int ri)
{
	if(tag[k]) return;
	if(le<=l&&r<=ri){tree[k]=0;tag[k]=1;return;}
	int i=k<<1,mid=l+r>>1;
	if(le<=mid) ctag(i,l,mid,le,ri);
	if(mid<ri) ctag(i|1,mid+1,r,le,ri);
	tree[k]=tree[i]+tree[i|1];
	if(!tree[k]) tag[k]=1;
	return;
}

int query(int k,int l,int r,int le,int ri)
{
	if(tag[k]) return 0;
	if(le<=l&&r<=ri) return tree[k];
	int i=k<<1,mid=l+r>>1,sum=0;
	if(le<=mid) sum+=query(i,l,mid,le,ri);
	if(mid<ri) sum+=query(i|1,mid+1,r,le,ri);
	return sum;
}

int main()
{
	n=read();
	for(int i=1;i<=n;++i) a[i].x=read(),a[i+n].x=read()+a[i].x,a[i].id=i,a[i+n].id=i+n;
	sort(a+1,a+(n<<1)+1,cmp1);a[1].y=1;
	for(int i=2;i<=n<<1;++i){
		if(a[i].x==a[i-1].x){a[i].y=a[i-1].y;continue;}
		a[i].y=a[i-1].y+1;siz[a[i-1].y]=a[i].x-a[i-1].x;
	}mx=a[n<<1].y;
	sort(a+1,a+(n<<1)+1,cmp2);
	m=read();
	for(int i=1;i<=m;++i) b[i].x=read(),b[i].y=read(),b[i].id=i;
	build(1,1,mx);
	sort(b+1,b+m+1,cmp3);
	int ct=n;
	for(int i=1;i<=m;++i){
		while(ct>=b[i].x){
			ctag(1,1,mx,a[ct].y,a[ct+n].y-1);
			--ct;
		}ans[b[i].id]=query(1,1,mx,a[b[i].x].y,a[b[i].y].y-1);
	}for(int i=1;i<=m;++i) printf("%d\n",ans[i]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值