#FT0000225. Thủ thư không chăm chỉ (Lazy Librarian)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thủ thư không chăm chỉ (Lazy Librarian)
Lazy Librarian
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
There are books arranged in a row, whose current positions are numbered from to from left to right. Each book has its own correct position in the sorted order and its page count. The correct positions form a permutation of .
A pair of books at current positions is out of order if the correct position of the book at is greater than that of the book at . Such a pair incurs discomfort equal to the sum of the two books' page counts. Total discomfort is the sum of these contributions over all out-of-order pairs.
On each of consecutive days, two books at current positions exchange places. If , nothing changes. Each book retains both of its attributes when moved.
After every swap, print the total discomfort of the resulting arrangement modulo . No automatic sorting occurs between days.
Input
- The first line contains integers , the number of books and swap days.
- Each of the next lines contains integers , the correct position and page count of the book initially at current position . All values are distinct.
- Each of the next lines contains integers , the two current positions whose books are swapped on day .
Output
Print exactly lines. Line contains the total discomfort after swap , modulo .
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; ; .
- Subtask 2 (30 points): ; is a permutation of ; ; .
- Subtask 3 (50 points): ; is a permutation of ; ; .
Examples
Input
3 3
2 5
1 3
3 2
1 2
2 3
1 3
Output
0
7
13
Explanation
Initially the books, recorded as (correct position, page count), are . The initial inversion contributes . After swapping positions , the books become , with no inversion, so the first output is . Next, swapping positions produces , whose only inversion has weight . Finally, swapping positions produces . The inversions between positions and contribute and respectively, totaling . Thus the outputs are .