#P16148. [ICPC 2017 NAIPC] Apple Market

[ICPC 2017 NAIPC] Apple Market

题目描述

You are managing a market with some stores. The stores are arranged in an n×mn \times m grid. Each store sells apples. Apples cost exactly 1 Malaysian Ringgit per apple at every store.

There will be several customers who walk through this market. Each customer will only visit stores within a subrectangle of the market, and each customer has a fixed amount of money to spend. Also, each store has a limited inventory of apples, which will not be replenished between customers; the number available differs from store to store. Assuming you can control how many apples each store sells to each customer, what is the most money you can make?

输入格式

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain three space-separated integers nn, mm, and kk, where the market has nn rows and mm columns (1n,m501 \leq n, m \leq 50), and there will be kk (1k1051 \leq k \leq 10^5) customers.

Each of the next nn lines will have mm integers aa (0a1090 \leq a \leq 10^9). This is a matrix in row major order, indicating the number of apples in the inventory of each store. a[r,c]a[r, c] is the number of apples in the store in the rthr^{\text{th}} row, cthc^{\text{th}} column. The rows range from 1..n1..n and the columns from 1..m1..m. The top left corner is a[1,1]a[1, 1], and the bottom right corner is a[n,m]a[n, m].

Each of the next kk lines will describe a customer, with five integers: tt, bb (1tbn1 \leq t \leq b \leq n), ll, rr (1lrm1 \leq l \leq r \leq m), and xx (0x1090 \leq x \leq 10^9). The customer will only shop in the subrectangle from (t,l)(t, l) to (b,r)(b, r) inclusive (t=t= top, b=b= bottom, l=l= left, r=r= right). The customer has exactly xx Malaysian Ringgits to spend.

输出格式

Output a single integer, representing the maximum amount of money to be made by controlling how many items each store sells to each customer.

2 3 2
1 2 3
4 5 6
1 2 2 3 20
2 2 1 3 15
20