Each client buys a_i colored or b_i black-and-white paintings, and after each update count the sales with at least C colored buyers modulo 10007.
Hard8Dynamic programmingSegment treeCombinatoricsNo attempts yetTime limit4sMemory limit32 MBLuka is an art dealer. He has N clients and sells paintings to each of them.
Every client buys only colored paintings or only black and white paintings, never both kinds at once. Client i buys at most ai colored paintings and at most bi black and white paintings, and whichever kind the client picks, the client buys at least one painting. Luka's stock is large enough that a client never has to settle for fewer paintings than requested.
Luka dislikes selling black and white paintings. If fewer than C clients receive colored paintings, he ends up unhappy.
The clients keep revising the largest number of paintings they are willing to buy. After each revision, count the sales in which at least C clients receive at least one colored painting.
The first line contains two integers N and C (1≤N≤100000, 1≤C≤20).
The second line contains N integers ai (1≤ai≤109).
The third line contains N integers bi (1≤bi≤109).
The fourth line contains the number of revisions Q (1≤Q≤100000).
Each of the next Q lines contains three integers P, aP and bP (1≤P≤N, 1≤aP≤109, 1≤bP≤109). Client P changes the largest number of colored paintings to aP and the largest number of black and white paintings to bP. Each revision stays in effect for every later revision.
Print Q lines. Line j holds the number of sales right after the j-th revision, modulo 10007.
Two sales differ when some client buys a different kind of painting, or the same kind in a different quantity. One client therefore contributes ai possibilities when buying colored paintings and bi possibilities when buying black and white paintings. If C is larger than N, no sale meets the requirement and the answer is 0.