LaLa and Magical Beast Summoning

아직 제출이 없습니다시간 제한5초메모리 제한1024 MB

문제

LaLa\color{blue}{\text{LaLa}} is about to cast a magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al beast summoning magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}.

The first thing LaLa\color{blue}{\text{LaLa}} do is creating a summoning field, which has 3 constants associated with it: nullity MM, elasticity EE, and viscosity VV. Such summoning field is denoted by F(M,E,V)\mathcal{F}(M, E, V)

A magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al beast summoning magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} is performed over a summoning cell within the summoning field, which is square-shaped and is associated with 3 variables: side length LL, agility AA, and intelligence II. Such summoning cell is denoted by C(L,A,I)\mathcal{C}(L, A, I).

C(L,A,I)\mathcal{C}(L, A, I) is in a null state if L=0L=0. Otherwise, it is in a positive state.

The density of C(L,A,I)\mathcal{C}(L, A, I) in positive state is defined as (A×I)/L2(A \times I) / L^2.

The problem of determining whether a magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al beast summoning magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} will succeed requires very heavy computation involving solving a system of 99999999999999999999-th order partial differential equations over 99999999999999999999999999999999 variables. Fortunately, LaLa\color{blue}{\text{LaLa}} already did all the math for you!

The magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al beast summoning magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} over C(L,A,I)\mathcal{C}(L, A, I) within F(M,E,V)\mathcal{F}(M, E, V) succeeds if and only if the function valid(M,E,V,L,A,I)\textrm{valid}(M, E, V, L, A, I) defined by the pseudocode in the note section returns true. We'll call such summoning cell valid.

Sometimes, LaLa\color{blue}{\text{LaLa}} isn't satisfied with the set of summoning cells she has, and wants to generate new ones by combining them. The problem of determining the result of combination of two valid summoning cells C_0=C(L_0,A_0,I_0)C\_0 = \mathcal{C}(L\_0, A\_0, I\_0) and C_1=C(L_1,A_1,I_1)C\_1 = \mathcal{C}(L\_1, A\_1, I\_1) within F=F(M,E,V)F = \mathcal{F}(M, E, V) requires another heavy computation, but thankfully, LaLa\color{blue}{\text{LaLa}} already did all the math for you again!

The result of combining two such cells C_0C\_0 and C_1C\_1 within FF, denoted by Combine_F(C_0,C_1)\textrm{Combine}\_F(C\_0, C\_1), is given by the function combine(M,E,V,L_0,A_0,I_0,L_1,A_1,I_1)\textrm{combine}(M, E, V, L\_0, A\_0, I\_0, L\_1, A\_1, I\_1) defined by the pseudocode in the note section, which returns a triple L_2,A_2,I_2L\_2, A\_2, I\_2 satisfying C(L_2,A_2,I_2)=Combine_F(C_0,C_1)\mathcal{C}(L\_2, A\_2, I\_2) = \textrm{Combine}\_F(C\_0, C\_1). Here, it can be proved that Combine_F(C_0,C_1)\textrm{Combine}\_F(C\_0, C\_1) is also valid. Note that swapping the order of C_0C\_0 and C_1C\_1 affects the result.

The result of combining K3K \ge 3 cells C_0,,C_K1C\_0, \cdots, C\_{K-1} within FF is given recursively by Combine_F(C_0,,C_K1)=Combine_F(Combine_F(C_0,,C_K2),C_K1)\textrm{Combine}\_F(C\_0, \cdots, C\_{K-1}) = \textrm{Combine}\_F(\textrm{Combine}\_F(C\_0, \cdots, C\_{K-2}), C\_{K-1})

For the sake of completeness, we define Combine_F(C)=C\textrm{Combine}\_F(C)=C.

LaLa\color{blue}{\text{LaLa}} is aware of a very special property about the combining operation that allows her to efficiently solve the range density query problem below. Can you figure it out?

You're given a summoning field F=F(M,E,V)F=\mathcal{F}(M, E, V) and an array of NN valid summoning cells C_0=C(L_0,A_0,I_0),,C_N1=C(L_N1,A_N1,I_N1)C\_0 = \mathcal{C}(L\_0, A\_0, I\_0), \cdots, C\_{N-1} = \mathcal{C}(L\_{N-1}, A\_{N-1}, I\_{N-1}) within FF. Write a program that processes the following two types of QQ queries:

  • 1 i L A I
    • Set C_iC(L,A,I)C\_i \leftarrow \mathcal{C}(L, A, I).
  • 2 l r
    • Let R=Combine_F(C_l,,C_r1)R=\textrm{Combine}\_F(C\_l, \cdots, C\_{r-1}). If RR is in the null state, print a single integer 1-1. Otherwise, print the density of RR, modulo MM. Here, an irreducible fraction p/qp/q, where pp is a non-negative integer and qq is a positive integer not divisible by MM, modulo MM is defined to be the unique integer p×q1modMp \times q^{-1} \bmod M where q1q^{-1} is the multiplicative inverse of qq modulo MM. It can be proved that if RR is in the positive state, the denominator of the density of RR as an irreducible fraction is not divisible by MM within the constraints of this problem.

입력

The input is given in the following format:

MM EE VV

NN

L_0L\_0 A_0A\_0 I_0I\_0

\vdots

L_N1L\_{N-1} A_N1A\_{N-1} I_N1I\_{N-1}

QQ

q_0q\_0

\vdots

q_Q1q\_{Q - 1}

Here, q_iq\_i denotes the ii-th query, and is given in the format described in the statement.

The input satisfies the following constraints:

  • All the numbers in the input are integers.
  • MM is a prime such that 900,000,000M1,000,000,000900\\,000\\,000 \le M \le 1\\,000\\,000\\,000
  • 1E,V1001 \le E, V \le 100
  • 1N,Q100,0001 \le N, Q \le 100\\,000
  • 0L_i,A_i,I_i<M0 \le L\_i, A\_i, I\_i < M for all integers 0i<N0 \le i < N
  • C(L_i,A_i,I_i)\mathcal{C}(L\_i, A\_i, I\_i) within F(M,E,V)\mathcal{F}(M, E, V) is valid for all integers 0i<N0 \le i < N.
  • For each query 1 i L A I, 0i<N0 \le i < N, 0L,A,I<M0 \le L, A, I < M, and C(L,A,I)\mathcal{C}(L, A, I) is valid within F(M,E,V)\mathcal{F}(M, E, V).
  • For each query 2 l r, 0l<rN0 \le l < r \le N

출력

For each query of the second type, print its answer in a single line.

힌트

The following pseudocode defines the validity of summoning cells and the Combine\textrm{Combine} operation.

Both functions do not modify their arguments

The author has attached a C++ implementation which will get "Time Limit Exceeded" verdict upon submission, but it will always print the correct answer within finite time. You may reuse some part of the implementation on your submission. You can find it on the "Problemset" tab on the domjudge site.