#P3759. [TJOI2017] 不勤劳的图书管理员

    ID: 1381 远端评测题 5000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017线段树各省省选枚举,暴力树套树天津

[TJOI2017] 不勤劳的图书管理员

Description

At Jialidun University, there is an Imperial Library. Xiao Dou is a librarian in the library’s reading room. His job is to keep the books in order, so disorder annoys him. A pair of books that are out of order causes an annoyance equal to the sum of their page counts. There are nn books currently in a jumbled order. During the next mm days, readers’ use will cause the books’ positions to change each day. Xiao Dou is required to tidy the books at least once during these mm days. He wants to know, if he does not tidy during the first ii days, what his annoyance will be on day ii, so that he can choose the day with the minimum annoyance to tidy.

Input Format

The first line contains two integers n,mn,m, denoting nn books and mm days. The next nn lines each contain two integers ai,via_i,v_i, meaning that book ii should be placed at position aia_i, and this book has viv_i pages. It is guaranteed that no two books have the same intended position. The next mm lines each contain two integers xjx_j and yjy_j, meaning that on day jj, book xjx_j and book yjy_j swap positions due to readers’ usage.

Output Format

Output mm lines. The ii-th line is the annoyance on day ii if he does not tidy during the first ii days. Since this number can be large, output the result modulo 109+710^9 +7.

5 5
1 1
2 2
3 3
4 4
5 5
1 5
1 5
2 4
5 3
1 3
42
0
18
28
48

Hint

Constraints

  • For 20%20\% of the testdata, 1ai,xj,yjn5×1031\le a_i,x_j,y_j\le n \le 5\times 10^3, 1m5×1031\le m\le 5\times 10^3, 1vi1051\le v_i\le10^5.
  • For 100%100\% of the testdata, 1ai,xj,yjn5×1041\le a_i,x_j,y_j\le n\le 5\times 10^4, 1m5×1041\le m\le 5\times 10^4, 1vi1051\le v_i\le 10^5.

Translated by ChatGPT 5