This page is still under construction.

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

Lucky Charms

Time limit1sMemory limit128 MB

Summary
Given a bracelet of length L nailed at position N, with charms at positions P_i hanging on strings of length S_i, compute how far below the nail each charm droops.
Level

Medium6 of 10

Topics
Geometry, Implementation, Math, Simulation
Solved
No attempts yet

Problem

Bessie has a lovely charm bracelet of length LL (4≤L≤327684 \le L \le 32768) mm. Hanging from the bracelet are CC (1≤C≤5121 \le C \le 512) charms, each at a unique integer distance from the bracelet's left end. Charm ii dangles from a string of length SiS_i mm (1≤Si≤251 \le S_i \le 25) that is attached at position PiP_i (0≤Pi≤L0 \le P_i \le L) mm from the left end of the bracelet.

Margaret snatches the bracelet and nails it (with a zero-width nail) to a fencepost. The nail is at position NN (1≤N≤L−11 \le N \le L-1) mm from the left end, so the bracelet hangs down on both sides of the nail while gravity pulls the bracelet and every charm straight down.

Bessie wonders: how far below the nail does each charm end up?

As an example, consider a bracelet of length 16 mm with three charms. In the schematic below each + is 1 mm apart and each vertical bar (|) represents 1 mm of string. The three charms hang on strings of length 4, 7, and 3 mm:

                        1 1 1 1 1 1 1
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |         |             |
            |         |             |
            |         |             *
            *         |
                      |
                      |
                      *

When the bracelet is nailed at position 5, it droops like this (ignore the left-right spread, which is drawn only for clarity):

Droop  Bracelet         Bracelet
dist.  location         location
  0      5         +        5    <---- nail is here
  1      4        + +       6
  2      3      | + +       7
  3      2      | + +       8              D
  4      1      | + +       9              O
  5      0      * + + |    10              W
  6                 + |    11              N
  7                 + |    12              |
  8                 + |    13              |
  9                 + |    14              V
 10                 + |    15
 11                 + *    16
 12                     |
 13                     |
 14                     *

The first charm droops 5 mm below the nail, the second 11 mm, and the third 14 mm. Compute the droop distance of every charm.

Input

  • Line 1: three space-separated integers LL, CC, and NN.
  • Lines 2 to C+1C+1: line i+1i+1 describes charm ii with two space-separated integers SiS_i and PiP_i.

Output

  • Lines 1 to CC: line ii contains the distance from charm ii to the nail.

Examples1

  1. Example 1

    Input
    16 3 5
    4 4
    7 9
    3 16
    
    Expected output
    5
    11
    14