#P5524. Sequence Operations and Query Sum

    ID: 18756 Type: Default 1000ms 256MiB

Sequence Operations and Query Sum

Sequence Operations and Query Sum

You are given a sequence of length \(n\). There are \(m\) operations numbered from 1 to \(m\). Each operation is in one of the following three types:

  • Type 1: 1 x y — Swap the elements at positions \(x\) and \(y\).
  • Type 2: 2 l r x — Assign the value \(x\) to every element in the interval \([l, r]\) (inclusive).
  • Type 3: 3 x — Query the current value at position \(x\).

Furthermore, you are given \(q\) queries. Each query consists of two integers \(l\) and \(r\) representing an interval of operation indices. For each query, perform the following steps independently:

  1. Reset the sequence to all zeros.
  2. Apply the operations numbered from \(l\) to \(r\) (in order).
  3. For every type 3 operation executed in this range, record its answer. Output the sum of all these answers.

If there are no type 3 operations in the interval, the sum is 0.

inputFormat

The input consists of multiple lines:

  1. The first line contains two integers \(n\) and \(m\), representing the length of the sequence and the number of operations respectively.
  2. The next \(m\) lines each describe an operation in one of the following formats:
    • 1 x y
    • 2 l r x
    • 3 x
  3. The next line contains a single integer \(q\), the number of queries.
  4. Each of the following \(q\) lines contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq m\)), representing the range of operations to process for that query.

outputFormat

For each query, output a single line containing one integer — the sum of the results of all type 3 operations after processing the specified operations on an initially zeroed sequence.

sample

5 5
2 1 3 5
3 2
1 1 5
3 1
2 3 5 2
2
1 3
2 5
5

0

</p>