Counting Rectangles
Time limit5sMemory limit1024 MB
Arrays A and B grow by appends, and after some appends the task asks for the number of all-black subrectangles of the grid where A_i + B_j is at least 0, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Segment tree, Stack
- Solved
- No attempts yet
Problem
For two integer arrays of size and of size , we define a grid of size , where cell is black if and white otherwise.
We define as the number of black rectangles inside . Each cell of is either entirely included in or disjoint from the rectangle.
In other words, is the number of tuples such that , , and every cell with and is black.
Initially, only and are given. Then, you process the following queries.
- 0 : append to the current array .
- 1 : append to the current array . Then, print .
- 2 : append to the current array .
- 3 : append to the current array . Then, print .
Input
The first line contains one integer .
The second line contains two space-separated integers and .
Each of the following lines contains two space-separated integers, which describe a query in the form above.
Output
For each query of type 1 or 3, print a single integer, the answer to that query, on its own line.
Constraints
- ()
- ()
Here is the size of array after all queries are processed, and is the size of array after all queries are processed. The last query has type 1 or 3.