Pianissimo

시간 제한1초메모리 제한2048 MB

요약
연주된 세기 값과 마디별 셈여림 구간, 셈여림의 순서가 주어질 때, 더 센 셈여림의 음이 더 큰 세기로 연주되지 않은 음의 쌍 개수를 센다.
난이도

보통10점 중 7점

유형
분할 정복, 정렬, 배열, 이분 탐색
정답자
아직 제출이 없습니다

문제

Composers use dynamics in sheet music to indicate how loud or soft the notes shall be played. Consider an 88-scale dynamic system:

  • ppp: pianississimo (the softest)
  • pp: pianissimo (very soft)
  • p: piano (soft)
  • mp: mezzopiano (moderately soft)
  • mf: mezzoforte (moderately loud)
  • f: forte (loud)
  • ff: fortissimo (very loud)
  • fff: fortississimo (the loudest)

When a musician performs, it practically does not matter how absolutely loud or soft a note is played. The audience only hear their relative difference. Suppose we use numbers to describe the absolute power of a note, where larger numbers indicate larger absolute power. Consider a musician who plays all her notes with power up to 100100. When she goes from power 4040 to 7070, some audience may think that she goes from p to f, while some others may think that she goes from pp to mf.

The musician’s interpretation must be consistent throughout the entire piece. That is, between two notes of different dynamics, the note with a louder dynamic must always have a strictly larger absolute power. Between two notes of the same dynamic, their power can vary (usually slightly, but there is no strict requirement).

You just heard a musician perform a piece with nn notes. You know the piece very well and you remember all the dynamics that the composer wrote. How consistent is the musician’s performance according to the written dynamics? You would like to count the unordered pairs of notes that violate the dynamics. Such a pair of notes is not necessarily consecutive and could be far from each other in the sheet music.

입력

The first line of input contains two integers nn and mm (1≤m≤n≤2⋅1051 ≤ m ≤ n ≤ 2 \cdot 10^5), the number of notes and the number of dynamic changes.

The next nn lines each have an integer between 11 and 10910^9, giving the absolute power of a note played by the musician. Larger numbers indicate larger absolute power.

The next mm lines each have a 11-based index ii and a dynamic dd, which mean the composer wrote that starting from note ii the dynamic should change to dd. Each dynamic is a string from the 88 dynamics: ppp, pp, p, mp, mf, f, ff, fff. The dynamics have distinct indices and are given in increasing order of their index. The first dynamic always starts from note 11.

출력

Output a single integer, the number of unordered pairs of notes that violate the dynamics.

예제2

  1. 예제 1

    입력
    8 6
    10
    15
    20
    30
    19
    20
    35
    40
    1 p
    3 f
    4 ff
    5 p
    7 f
    8 ff
    
    예상 출력
    2
    
  2. 예제 2

    입력
    12 5
    5
    10
    9
    20
    30
    40
    90
    90
    11
    10
    4
    3
    1 p
    4 f
    7 ff
    9 p
    11 ppp
    
    예상 출력
    0