CF1545E2.AquaMoon and Time Stop (hard version)

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Note that the differences between easy and hard versions are the constraints on nn and the time limit. You can make hacks only if both versions are solved.

AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts.

The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time 00 he is at coordinate xx, moving with a speed of 11 unit per second. In particular, at time ii the person will be at coordinate x+ix+i.

The ghosts are going to cast nn curses on the street. The ii-th curse will last from time tli−1+10−18tl_i-1+10^{-18} to time tri+1−10−18tr_i+1-10^{-18} (exclusively) and will kill people with coordinates from li−1+10−18l_i-1+10^{-18} to ri+1−10−18r_i+1-10^{-18} (exclusively). Formally that means, that the person, whose coordinate is between (li−1+10−18,ri+1−10−18)(l_i-1+10^{-18},r_i+1-10^{-18}) in the time range (tli−1+10−18,tri+1−10−18)(tl_i-1+10^{-18},tr_i+1-10^{-18}) will die.

To save the person on the street, AquaMoon can stop time at any moment tt, and then move the person from his current coordinate xx to any coordinate yy (tt, xx and yy are not necessarily integers). The movement costs AquaMoon ∣x−y∣|x-y| energy. The movement is continuous, so if there exists some cursed area between points xx and yy at time tt, the person will die too.

AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all nn curses. But she is not good at programming. As her friend, can you help her?

注意:简单版本与困难版本的区别在于对 nn 的约束以及时间限制。仅当两个版本均被解决时,才允许进行 hack。

AquaMoon 通过预知得知,一些幽灵打算在一条步行街上诅咒游客。但不幸的是,这一次这些幽灵藏身于一道屏障之中,而她无法在短时间内进入该屏障并消灭它们。因此,唯一能做的就是拯救步行街上任何不幸的路人。

步行街可建模为一维坐标系。此时,步行街上恰好有一名路人。在时刻 00,他位于坐标 xx 处,并以每秒 11 单位的速度移动。特别地,在时刻 ii,他将位于坐标 x+ix+i 处。

幽灵将在步行街上施放 nn 个诅咒。第 ii 个诅咒的作用时间为开区间 (tli−1+10−18, tri+1−10−18)(tl_i-1+10^{-18},\, tr_i+1-10^{-18}),作用范围为坐标开区间 (li−1+10−18, ri+1−10−18)(l_i-1+10^{-18},\, r_i+1-10^{-18})。形式化地说,这意味着:若某人在时间区间 (tli−1+10−18, tri+1−10−18)(tl_i-1+10^{-18},\, tr_i+1-10^{-18}) 内处于坐标区间 (li−1+10−18, ri+1−10−18)(l_i-1+10^{-18},\, r_i+1-10^{-18}) 中,则此人将死亡。

为拯救步行街上的路人,AquaMoon 可在任意时刻 tt 暂停时间,然后将路人从其当前坐标 xx 移动至任意坐标 yy(其中 tt、xx 和 yy 均不必为整数)。该移动操作消耗 AquaMoon 的能量值为 ∣x−y∣|x-y|。移动过程是连续的,因此若在时刻 tt,线段 [x,y][x, y](或 [y,x][y, x])上存在任何被诅咒的区域,则路人亦会死亡。

AquaMoon 想知道:为使路人免遭全部 nn 个诅咒的伤害,她所需的最小能量消耗是多少?但她不擅长编程。作为她的朋友,你能帮她吗?

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051\le n\le 2 \cdot 10^5) — the number of curses.

The next line contains a single integer xx (1≤x≤1061\le x\le 10^6) — the initial coordinate of the person.

The following nn lines contain four integers tlitl_i, tritr_i, lil_i, rir_i each (1≤tli≤tri≤1061\le tl_i\le tr_i\le 10^6, 1≤li≤ri≤1061\le l_i\le r_i\le 10^6).

第一行包含一个整数 nn(1≤n≤2⋅1051\le n\le 2 \cdot 10^5)—— 骂咒的数量。

第二行包含一个整数 xx(1≤x≤1061\le x\le 10^6)—— 该人的初始坐标。

接下来的 nn 行,每行包含四个整数 tlitl_i、tritr_i、lil_i、rir_i(1≤tli≤tri≤1061\le tl_i\le tr_i\le 10^6,1≤li≤ri≤1061\le l_i\le r_i\le 10^6)。

输出格式

Print a single integer — the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).

输出一个整数——AquaMoon 所需消耗的最小能量值,向上取整至最接近的整数(若存在两个同样接近的整数,则取较大的那个)。

输入输出样例

  • 输入#1

    2
    1
    1 2 1 2
    2 3 2 3

    输出#1

    2
  • 输入#2

    3
    4
    1 4 1 2
    1 4 4 15
    6 7 1 4

    输出#2

    8
  • 输入#3

    4
    3
    1 5 1 1
    4 10 1 4
    1 2 3 13
    1 10 7 19

    输出#3

    14
  • 输入#4

    7
    5
    78 96 76 91
    6 16 18 37
    53 63 40 56
    83 88 21 38
    72 75 17 24
    63 63 53 60
    34 46 60 60

    输出#4

    20

输入解题思路,AI测评打分。不知道怎么写?

首页