Wiring Engineering
Time limit8sMemory limit1024 MB
Given a weight matrix and building and tower install costs, answer many range queries for the maximum profit of non-crossing wires within chosen index ranges.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Divide and conquer, Matrix
- Solved
- No attempts yet
Problem
On the north side of Bytestreet, there are buildings standing in a row. They are labeled from east to west, and the coordinate of the -th building is .
On the south side of Bytestreet, there are communication towers standing in a row. They are labeled from east to west, and the coordinate of the -th tower is .
You are an electrical engineer in Byteland. Your job is to design a wiring scheme. A wire connects one building and one tower as a straight segment from to . For each pair of building and tower, you can connect at most one wire. Connecting the -th building with the -th tower earns dollars from the building owner.
Each building can have multiple wires, but you must pay dollars to connect at least one wire to the -th building, because you must first install equipment there. Likewise, each tower can have multiple wires, but you must pay dollars to connect at least one wire to the -th tower. Two wires can only intersect at their endpoints, which prevents short circuits.
Some places cannot have equipment installed, so no wire can connect to them. You are given queries. In the -th query, you get four integers , , , and . You can install equipment only in buildings with labels in and in towers with labels in . For each query, find the wiring scheme that maximizes your earnings.
The answer cannot be negative, because you can choose to do nothing.
Input
The input contains only a single test case.
The first line contains two integers and (, ), the number of buildings (or towers) and the number of queries.
The second line contains integers (), the cost to install equipment in each building.
The third line contains integers (), the cost to install equipment in each tower.
Each of the next lines contains integers. The -th integer on the -th line is (), the money you get for connecting the -th building with the -th tower.
Each of the next lines contains four integers , , , and (, ), describing the -th query.
Output
For each query, print a single line containing the maximum amount of dollars you can earn.