Cannonball

시간 제한2초메모리 제한1024 MB

요약
위치 S에서 파워 1로 오른쪽으로 출발해 점프 패드(파워를 높이고 방향을 뒤집음)와 타겟 위를 튕기며 선을 벗어나거나 영원히 반복되기 전에 부순 타겟의 수를 구한다.
난이도

어려움10점 중 8점

유형
시뮬레이션, 구현, 이분 탐색, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

Bessie has mastered the art of turning into a cannonball and bouncing along a number line of length NN (1≤N≤105)(1 \leq N \leq 10^5) with locations numbered 1,2,…,N1,2,\dots,N from left to right. She starts at some integer location SS (1≤S≤N)(1 \leq S \leq N) bouncing to the right with a starting power of 11. If Bessie has power kk, her next bounce will be at a distance kk forward from her current location.

Every integer location from 11 to NN is either a target or a jump pad. Each target and jump pad has an integer value in the range 00 to NN inclusive. A jump pad with a value of vv increases Bessie's power by vv and reverses her direction. A target with a value of vv will be broken if landed on with a power of at least vv. Landing on a target does not change Bessie's power or direction. A target that is broken will remain broken and Bessie can still bounce on it, also without changing power or direction.

If Bessie bounces for an infinite amount of time or until she leaves the number line, how many targets will she break?

If Bessie starts on a target that she can break, she will immediately do so. Similarly, if Bessie starts on a jump pad, the pad's effects will be applied before her first jump.

입력

The first line of the input contains NN and SS, where NN is the length of the number line and SS is Bessie's starting location.

The next NN lines describe each of the locations. The iith of these lines contains integers q_iq\_i and v_iv\_i, where q_i=0q\_i = 0 if location ii is a jump pad and q_i=1q\_i = 1 if location ii is a target, and where v_iv\_i is the value of location ii.

출력

Output one number representing the number of targets that will be broken.

예제2

  1. 예제 1

    입력
    5 2
    0 1
    1 1
    1 2
    0 1
    1 1
    
    예상 출력
    1
    
  2. 예제 2

    입력
    6 4
    0 3
    1 1
    1 2
    1 1
    0 1
    1 1
    
    예상 출력
    3