Given up to 50000 non-overlapping axis-aligned rectangles, answer online queries that ask for the total rectangle area inside a query rectangle, with coordinates decoded from the previous answer.
Hard8Segment treeSortingBinary searchPrefix sumNo attempts yetTime limit2sMemory limit1024 MBDeep inside a secret base there is a wall covered with rectangular posters. The posters are hard to come by, so they are placed without overlapping.
Every now and then a batch of new posters worth a place on the wall arrives, and the keepers have to decide where to put them. Your part of that process is one step: for a candidate position, compute the total area of the posters already hanging that a new poster placed there would cover.
You are given n grey rectangles in a plane, no two of which overlap. Answer q queries. Each query gives one rectangle and asks for the total grey area inside it. A query does not change the plane.
The answers have to be produced online. The coordinates of each query are encoded with the answer to the previous query, so you cannot read every query before answering the first one.
The first line contains five integers r, c, n, q, m (1≤r,c<m≤109+9, 0≤n,q≤50000): the height of the wall, the width of the wall, the number of posters on the wall, the number of queries, and the modulus used to encode the queries.
Each of the next n lines contains four integers x1,y1,x2,y2 (0≤x1,x2≤r, 0≤y1,y2≤c), two opposite corners of one poster.
Each of the last q lines contains five integers x1′,y1′,x2′,y2′,v, each between 0 and m−1 inclusive. Let l be the answer to the previous query, with l=0 for the first query. The real coordinates follow from the formulas below.
xi=(xi′+l⋅v)(modm)
yi=(yi′+l⋅v)(modm)
The decoded values x1,y1,x2,y2 are two opposite corners of the query rectangle and satisfy 0≤x1,x2≤r and 0≤y1,y2≤c.
In some inputs v is zero in every query. The encoding then leaves the coordinates unchanged.
For each query print one line with a single integer: the total grey area inside the query rectangle.
The picture below shows the whole plane of the first example.

The second example encodes the same four queries as the first one with a nonzero v. After decoding, the queries of the two examples agree, and so do the answers.