Street Trees
Time limit2sMemory limit256 MB
Maintain a minimum-cost coloring of N vertices with two colors under incremental equality/inequality constraints and point cost updates, reporting the optimum after each operation.
- Level
Hard9 of 10
- Topics
- Union-find, Graph, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Yonsei University is planting street trees across the whole campus. The school wants to work out how much budget the job needs before it starts.
The campus has buildings, and exactly one tree goes in front of each building, so trees in total. Every tree is one of two kinds. One is a ginkgo, which attracts no pests but smells bad in autumn. The other is a plane tree, which gives shade in summer but can cause respiratory allergies in some people in spring.
The school has already surveyed, for every building, the cost of planting a ginkgo and the cost of planting a plane tree. The terrain and the surroundings differ from building to building, so the same kind of tree can cost different amounts in front of different buildings.
The school wrote a plan that plants a tree in front of every building at the lowest total cost. A designer who joined the project late said the plan looks bad and needs a lot of revision. Each designer request is one of two kinds.
- Building and building must get the same kind of tree.
- Building and building must get different kinds of trees.
Worse, revising the plan took so long that the surveyed costs started to change too. Each cost change is one of two kinds.
- The cost of planting a ginkgo in front of building changes.
- The cost of planting a plane tree in front of building changes.
The school decided that revising the plan by hand is no longer workable, and asked the computer science department for a program that applies the incoming requests and price changes at once and reports the optimal plan. Write that program.
Input
The first line has the number of buildings and the number of plan requests the designer has made so far, . (, )
Buildings are numbered from 1.
Each of the next lines has the cost of planting a ginkgo and the cost of planting a plane tree in front of one building, given in order from building 1. ()
Each of the next lines has one designer request in the form C i j. ( is 0 or 1, , , )
means building and building must get the same kind of tree, and means building and building must get different kinds of trees.
The next line has , the number of later designer requests and cost changes combined. ()
Each of the next lines has one request or change in the form C A B, classified as follows.
- with and is a request that building and building get the same kind of tree.
- with and is a request that building and building get different kinds of trees.
- with and means the cost of planting a ginkgo in front of building has changed to .
- with and means the cost of planting a plane tree in front of building has changed to .
For any two different designer requests and , never equals and never equals .
Every request and every change accumulates.
Output
Print lines.
On the first line print the minimum cost of planting one tree in front of every building when only the requests given above are applied, before any later request or cost change.
On each of the next lines print the minimum cost of planting one tree in front of every building right after that request or change is applied.
While the requests and all the later requests are processed, planting one tree in front of every building never becomes impossible.
Hint
In the example the campus has 4 buildings, and the designer first asks for the same tree in front of buildings 1 and 3. The best plan is a ginkgo, a plane tree, a ginkgo, and a ginkgo in order from building 1, at a cost of 17.
The three later changes work out as follows. After the request for the same tree in front of buildings 1 and 2, planting a ginkgo in front of every building becomes optimal, at a cost of 18. After the request for different trees in front of buildings 1 and 4, the best plan is a plane tree, a plane tree, a plane tree, and a ginkgo, at a cost of 30. Finally the cost of planting a plane tree in front of building 4 drops from 100 to 1, so the best plan becomes a ginkgo, a ginkgo, a ginkgo, and a plane tree, at a cost of 18.