Posters on the wall
Time limit2sMemory limit1024 MB
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.
- Level
Hard8 of 10
- Topics
- Segment tree, Sorting, Binary search, Prefix sum
- Solved
- No attempts yet
Problem
Deep 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 grey rectangles in a plane, no two of which overlap. Answer 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.
Input
The first line contains five integers , , , , (, ): 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 lines contains four integers (, ), two opposite corners of one poster.
Each of the last lines contains five integers , each between and inclusive. Let be the answer to the previous query, with for the first query. The real coordinates follow from the formulas below.
The decoded values are two opposite corners of the query rectangle and satisfy and .
In some inputs is zero in every query. The encoding then leaves the coordinates unchanged.
Output
For each query print one line with a single integer: the total grey area inside the query rectangle.
Hint
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 . After decoding, the queries of the two examples agree, and so do the answers.