Triangle Counting

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

You are given a tournament graph (a complete directed graph) of NN vertices, numbered from 11 to NN. Initially, all edges are directed towards the vertex with the higher number.

A triangle is a triplet of vertices (a,b,c)(a, b, c), such that there are edges aba \rightarrow b, bcb \rightarrow c, cac \rightarrow a in the graph,
with 1a,b,cN1 \leq a, b, c \leq N and aba \neq b, aca \neq c, bcb \neq c.

You need to process QQ updates, where in each update we flip an edge. The updates are permanent, and you are required to count the number of triangles in the graph following each update.

Input data

The first line of the input contains NN and QQ, representing the number of vertices in the graph and the number of updates.
The next QQ lines contain the updates in the form (Xi,Yi)(X_i, Y_i), meaning that the edge between XiX_i and YiY_i is to be flipped. It is guaranteed that the edge between XiX_i and YiY_i in the graph is directed towards YiY_i right before the update.

Output data

The output must contain QQ lines, the number of triangles after each update.

Constraints and clarifications

  • 3N200 0003 \leq N \leq 200 \ 000.
  • 1Q1 000 0001 \leq Q \leq 1 \ 000 \ 000.
  • 1Xi,YiN1 \leq X_i, Y_i \leq N and XiYiX_i \neq Y_i for each i=0...Q1i = 0 . . . Q − 1.
# Points Constraints
1 0 Examples
2 14 N,Q100N,Q \leq 100
3 16 N100N \leq 100
4 23 N2 000N \leq 2\ 000
5 47 No additional constraints

Example

stdin

4 4
2 4
1 2
4 2
2 3

stdout

1
2
0
1

Explanation

In the sample case, the initial graph (without updates) is diplayed in the figure below.

The first update flips edge 242 \rightarrow 4. The resulting graph contains only one triangle 2342 \rightarrow 3 \rightarrow 4 (see below).

The graphs obtained after each remaining update are displayed in the following figure (from left to right).

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