Under construction

Still being built. Things may change

Pianissimo

Interview

Time limit1sMemory limit2048 MB

Summary
Given each note's played power and the sheet's dynamic markings, count the unordered pairs of notes with different dynamics whose power order breaks the dynamic order.
Level

Medium5 of 10

Topics
Sorting, Array
Solved
No attempts yet

Problem

Composers use dynamics in sheet music to indicate how loud or soft the notes shall be played. Consider an 88-scale dynamic system, from softest to loudest: ppp (pianississimo), pp (pianissimo), p (piano), mp (mezzopiano), mf (mezzoforte), f (forte), ff (fortissimo), and fff (fortississimo).

When a musician performs, the absolute loudness of a note matters little. The audience only hears the relative difference. Suppose absolute power is a number, where larger numbers mean larger absolute power. A musician plays all her notes with power up to 100100. When she goes from power 4040 to 7070, some audience may hear her go from p to f, while others may hear her go from pp to mf.

The musician's interpretation must be consistent across the whole piece. Between two notes of different dynamics, the note with the louder dynamic must 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 well and remember every dynamic the composer wrote. Count the unordered pairs of notes that violate the written dynamics. Such a pair is not necessarily consecutive and can be far apart in the sheet music.

Input

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

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

The next mm lines each contain a 1-based index ii and a dynamic dd, meaning that starting from note ii, the dynamic changes to dd. Each dynamic is one of the 8 dynamics: ppp, pp, p, mp, mf, f, ff, fff. The indices are distinct and given in increasing order. The first dynamic always starts at note 11.

Output

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

Examples2

  1. Example 1

    Input
    8 6
    10
    15
    20
    30
    19
    20
    35
    40
    1 p
    3 f
    4 ff
    5 p
    7 f
    8 ff
    
    Expected output
    2
    
  2. Example 2

    Input
    12 5
    5
    10
    9
    20
    30
    40
    90
    90
    11
    10
    4
    3
    1 p
    4 f
    7 ff
    9 p
    11 ppp
    
    Expected output
    0