QOJ.ac

QOJ

時間限制: 5 s 記憶體限制: 256 MB 總分: 100

#14632. The Lazy Librarian

统计

Xiao Dou is a librarian at the Imperial Library of Jialin University. His job is to keep the books in order, as an unordered collection of books makes him annoyed. Two out-of-order books will cause Xiao Dou a level of annoyance equal to the sum of their page counts. There are currently $n$ books in a jumbled order. Over the next $m$ days, the books will change positions every day because readers move them. Since Xiao Dou is required to tidy up the books at least once every $m$ days, he wants to know his total annoyance level before tidying up on each day $i$, so that he can choose the day with the minimum annoyance to tidy them up.

Input

The first line contains two integers $n$ and $m$, representing the number of books and the number of days, respectively.

The next $n$ lines each contain two integers $a_i$ and $v_i$, representing that the $i$-th book should originally be at position $a_i$, and this book has $v_i$ pages. It is guaranteed that no two books are at the same position.

The next $m$ lines each contain two integers $x_j$ and $y_j$, representing that on the $j$-th day, the books at position $x_j$ and position $y_j$ are swapped by readers.

Output

There are $m$ lines, each containing one integer. The $i$-th line represents Xiao Dou's annoyance level before tidying up on day $i$. Since this number can be very large, output the result modulo $10^9 + 7$.

Constraints

For 20% of the data: $1 \le a_i, x_j, y_j \le n \le 5000$, $m \le 5000$, $v_i \le 10^5$.

For 100% of the data: $1 \le a_i, x_j, y_j \le n \le 50000$, $m \le 50000$, $v_i \le 10^5$.

Examples

Input 1

5 5
1 1
2 2
3 3
4 4
5 5
1 5
1 5
2 4
5 3
1 3

Output 1

42
0
18
28
48

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.