CF1545E2.AquaMoon and Time Stop (hard version)

普及/提高-

通过率:0%

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 tli1+1018tl_i-1+10^{-18} to time tri+11018tr_i+1-10^{-18} (exclusively) and will kill people with coordinates from li1+1018l_i-1+10^{-18} to ri+11018r_i+1-10^{-18} (exclusively). Formally that means, that the person, whose coordinate is between (li1+1018,ri+11018)(l_i-1+10^{-18},r_i+1-10^{-18}) in the time range (tli1+1018,tri+11018)(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 xy|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?

输入格式

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

The next line contains a single integer xx ( 1x1061\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 ( 1tlitri1061\le tl_i\le tr_i\le 10^6 , 1liri1061\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).

输入输出样例

  • 输入#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
首页