CC's

Back

Petya recently found a game “Choose a Square”. In this game, there are nn points numbered from 11 to nn on an infinite field. The ii-th point has coordinates (xi,yi) and cost ci.

You have to choose a square such that its sides are parallel to coordinate axes, the lower left and upper right corners belong to the line y=x, and all corners have integer coordinates.

The score you get is the sum of costs of the points covered by the selected square minus the length of the side of the square. Note that the length of the side can be zero.

Petya asks you to calculate the maximum possible score in the game that can be achieved by placing exactly one square.

{% asset_img a.png %}

分析#

这道题其实和多校的Snowy Smile(题文无关)挺像的。

需要转化条件。正方形全部位于坐标系第一象限的平分线上,可以表示为(l,r)(l,r)这种形式。一个在正方形内的点(x,y)(x,y)(假设x<yx < y,由于这个题目的特性,不满足时可以直接调换)满足的条件为

lxyrl \leq x \leq y \leq r

由此,这道题就变成了二维偏序。本来的本来,上个长得像树状数组一类的东西就解决了。

将点按照第一维排序,从大到小枚举,在数据结构中维护结尾到r的前缀和最大值,并查询,更新全局答案。不过这个题目还要求权值要减去正方形边长w。

f(r)(rl)=l+[f(r)r]f(r)-(r-l)=l+[f(r)-r]

维护最大值,y分开,只在pushup时合在一起。

然后还要离散化……

代码#

这题给我整自闭了,我想用线段树单点修改来魔幻维护我需要的数据,加加减减。结果发现2种维护方式都有无法解决的问题。最后只能打tag。

线段树维护f(n)f(n)为到nn为止的前缀和。每次加点以xx为组,对已有点减去新增线段的长度,再加上新点的影响。那么在线段树上维护的就是答案。取出最大值即可。

代码写得又长又丑,不过过了。

[CF1221F] Choose a Sequence
https://astro-pure.js.org/blog/cf1221f-choose-a-sequence
Author Cheng Chen
Published at 2019年9月23日
Comment seems to stuck. Try to refresh?✨