Intergalactic Chords
Time limit1sMemory limit1024 MB
Maintain an array of N notes (0 to 8); for each chord [a,b] find the most frequent note in the range, break ties by largest, then add it modulo 9 to every note in the range.
- Level
Hard8 of 10
- Topics
- Segment tree, Implementation, Math, Brute force
- Solved
- No attempts yet
Problem
The intergalactic piano sonata composition marathon keeps raising the difficulty for its competitors, because more and more beings of superior intelligence enter it.
The piano has keys, numbered to . The intergalactic tonal system has nine notes, valued to . At the start every key is assigned the same note, .
A competitor plays a sequence of chords. Each intergalactic chord is made of two distinct keys and with . When the chord is played, the piano sounds , the most frequent note among the keys in the interval . If several notes tie for most frequent, the piano sounds the largest of them. Right after sounding the note, the piano changes the note assigned to every key in the interval . The new note of key , for , is its previous note plus , modulo .
For example, suppose the notes of a piano with keys are the following at some moment.
If the chord is played, the most frequent note is , and the notes after the chord are the following.
Given the sequence of chords, print the note assigned to each key of the piano after every chord in the sequence has been played.
Input
The first line contains two integers and separated by a space, the number of keys of the intergalactic piano and the number of chords. (, )
Each of the next lines contains two integers and , one chord. () The chords are played in the given order.
Output
Print integers, one per line, the notes assigned to keys through after all chords have been played.