Mirror Circuit#13046

Mirror Circuit

5 seconds
512 megabytes
Very hard

You are given gates arranged in a line. A binary signal passes through the gates from left to right and can be in state or .

Gate has a digit d_i and two rewards w_{i,0} and w_{i,1}. Its effect on the signal depends on d_i \bmod 4:

After processing gate , if the resulting state is , you receive reward w_{i,s}.

You need to process operations of two types:

The replacement d_i \leftarrow 9-d_i is temporary and affects only the current query. It does not change the rewards w_{i,0} and w_{i,1}.

Input

The first line contains two integers and (1 \le n \le 10^5, 1 \le q \le 8 \cdot 10^4).

The next lines each contain three integers d_i, w_{i,0}, and w_{i,1} (0 \le d_i \le 9, -10^9 \le w_{i,0},w_{i,1} \le 10^9).

The next lines describe the operations:

  • 1 i d (1 \le i \le n, 0 \le d \le 9);

  • 2 l r (1 \le l \le r \le n).

Output

For every operation of type , print one integer — the maximum possible total reward.

Examples

Example 1

Loading
Loading

Note

Initially,

d=[2,7,6,6,1].

For the first query, choose the segment . Then d_4 temporarily changes from to .

The rewards obtained from gates through are

7,\ 2,\ 4,\ 2,\ 5,

so the total reward is .

Then the update changes d_2 from to .

For the second query, again choose the segment . The obtained rewards are

7,\ 1,\ 4,\ 2,\ 5,

so the maximum total reward is .

Scoring

  1. Subtask ( points): n \le 20 and q \le 20.

  2. Subtask ( points): every digit in the initial array and in every update satisfies d \bmod 4 \in \{2,3\}.

  3. Subtask ( points): there are no operations of type .

  4. Subtask ( points): every operation of type has l=1 and r=n.

  5. Subtask ( points): n \le 5000 and q \le 5000.

  6. Subtask ( points): every digit in the initial array and in every update satisfies d \bmod 4 \in \{0,1\}.

  7. Subtask ( points): No additional constraints.