This page is still under construction.

Parts of this page are still being built. What you see may change.

Counting Rectangles

Time limit5sMemory limit1024 MB

Summary
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 AA of size NN and BB of size MM, we define a grid G(A,B)G(A, B) of size N×MN \times M, where cell (i,j)(i, j) is black if Ai+Bj≥0A_i + B_j \ge 0 and white otherwise.

We define F(A,B)F(A, B) as the number of black rectangles inside G(A,B)G(A, B). Each cell of G(A,B)G(A, B) is either entirely included in or disjoint from the rectangle.

In other words, F(A,B)F(A, B) is the number of tuples (l1,r1,l2,r2)(l_1, r_1, l_2, r_2) such that 1≤l1≤r1≤N1 \le l_1 \le r_1 \le N, 1≤l2≤r2≤M1 \le l_2 \le r_2 \le M, and every cell (i,j)(i, j) with l1≤i≤r1l_1 \le i \le r_1 and l2≤j≤r2l_2 \le j \le r_2 is black.

Initially, only A1A_1 and B1B_1 are given. Then, you process the following QQ queries.

  • 0 vv: append vv to the current array AA.
  • 1 vv: append vv to the current array AA. Then, print F(A,B) mod 998 244 353F(A, B) \bmod 998\,244\,353.
  • 2 vv: append vv to the current array BB.
  • 3 vv: append vv to the current array BB. Then, print F(A,B) mod 998 244 353F(A, B) \bmod 998\,244\,353.

Input

The first line contains one integer QQ.

The second line contains two space-separated integers A1A_1 and B1B_1.

Each of the following QQ 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

  • 1≤N≤250 0001 \le N \le 250\,000
  • 1≤M≤250 0001 \le M \le 250\,000
  • 1≤Q=N+M−21 \le Q = N + M - 2
  • −109≤Ai≤109-10^9 \le A_i \le 10^9 (1≤i≤N1 \le i \le N)
  • −109≤Bi≤109-10^9 \le B_i \le 10^9 (1≤i≤M1 \le i \le M)

Here NN is the size of array AA after all queries are processed, and MM is the size of array BB after all queries are processed. The last query has type 1 or 3.

Examples2

  1. Example 1

    Input
    4
    -4 -3
    0 -5
    2 2
    0 3
    3 -5
    
    Expected output
    3
    
  2. Example 2

    Input
    8
    -187121777 648583176
    0 536185451
    1 77324177
    2 -543947071
    1 -495948203
    2 809620127
    2 918209957
    3 -724806401
    1 30094601
    
    Expected output
    6
    10
    40
    60