From Bucharest 2 Piatra Neamț

Time limit: 2s Memory limit: 256MB Input: Output:

Matei, an esteemed student from the National High School of Computer Science in Piatra Neamț, is now studying in Bucharest. Hearing that the IIOT finals will take place in his hometown, he wants to make sure he doesn't miss out on the event!

The road network of Moldova and Muntenia can be modeled as a directed graph with NN vertices (cities) and MM edges (roads). The road network is in severe disrepair, so the government recently approved the construction of several new roads. The roads will be constructed in the following ways:

  • 1 x l r\text{1 x l r} — Constructs a new road from city xx to every city in the interval [l,r][l, r].
  • 2 x l r\text{2 x l r} — Constructs a new road from every city in the interval [l,r][l, r] to city xx.
  • 3 l r\text{3 l r} — Constructs a new road between every pair of cities (x,y)(x, y) within the interval [l,r][l, r].

Task

Seeing the new road network, Matei wonders about the following question: how many pairs of cities (u,v)(u, v) are there such that both paths from uvu \to v and vuv \to u exist?

Input data

The first line of the input contains two space-separated integers, NN and MM, where NN represents the number of cities in the network and MM the number of initial roads.

The following MM lines each contain two space-separated integers, uu and vv, representing a road from city uu to city vv in the original network.

The following line contains the integer QQ, the number of new construction phases.

The next QQ lines describe the construction phases. Each line begins with an integer type{1,2,3}type \in \{1, 2, 3\}, followed by additional space-separated integers corresponding to the parameters of the construction type described above.

Output data

The first line of the output must contain a single integer representing the total number of pairs (u,v)(u, v) for which both directed paths uvu \to v and vuv \to u exist in the final network.

Constraints and clarifications

  • 1N,M,Q200 0001 \leq N, M, Q \leq 200 \ 000
  • 1u,vN1 \leq u, v \leq N for every road in the network.
  • 1xN, 1lrN1 \leq x \leq N, \ 1 \leq l \leq r \leq N for every construction phase.
# Score Constraints
0 0 Examples
1 10 N,Q500N, Q \leq 500
2 8 Q=0Q = 0
3 12 All construction phases are of type=3type = 3.
4 15 N200 000N \leq 200 \ 000, for all construction phases of type1,2type \in {1, 2}, we guarantee that rl40r - l \leq 40.
5 55 No further restrictions.

Example

stdin

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

stdout

25

Log in or sign up to be able to send submissions!