This page is still under construction.

Parts of this page are still being built. What you see may change.

Tickets

Time limit2sMemory limit1024 MB

Summary
For each starting checkpoint, find the minimum total ticket price needed to reach both checkpoint 1 and checkpoint N, or -1 if impossible.
Level

Hard8 of 10

Topics
Shortest path, Segment tree, Graph
Solved
No attempts yet

Problem

Bessie is going on a hiking excursion. The trail she is currently traversing consists of NN checkpoints labeled 1…N1\ldots N (1≤N≤1051\le N\le 10^5).

There are KK tickets available for purchase (1≤K≤1051\le K\le 10^5). The ii-th ticket can be purchased at checkpoint cic_i (1≤ci≤N1\le c_i\le N) for price pip_i (1≤pi≤1091\le p_i\le 10^9) and provides access to all checkpoints in [ai,bi][a_i,b_i] (1≤ai≤bi≤N1\le a_i\le b_i\le N). Before entering any checkpoint, Bessie must have purchased a ticket that allows access to that checkpoint. Once Bessie has access to a checkpoint, she may return to it at any point in the future. She may travel between any two checkpoints to which she has access, whether or not their labels differ by 1.

For each i∈[1,N]i\in[1,N], suppose Bessie initially has access to only checkpoint ii. Output the minimum total price required to purchase access to both checkpoints 11 and NN. If it is impossible, output -1.

Input

The first line contains NN and KK.

Each of the next KK lines contains four integers cic_i, pip_i, aia_i, and bib_i.

Output

Output NN lines, one for each checkpoint.

Examples1

  1. Example 1

    Input
    7 6
    4 1 2 3
    4 10 5 6
    2 100 7 7
    6 1000 1 1
    5 10000 1 4
    6 100000 5 6
    
    Expected output
    -1
    -1
    -1
    1111
    10100
    110100
    -1