Stargates

Time limit1sMemory limit128 MB

Summary
Implement a union-find structure over up to 6,000,000 nodes supporting batched connect and query commands defined by arithmetic sequences of pairs.
Level

Medium6 of 10

Topics
Union-find, Implementation, Simulation
Solved
No attempts yet

Problem

Long ago, in a distant galaxy, an advanced civilization discovered instant travel between solar systems and began building pairs of stargates that link far-apart planets. Their network grew so complex that they need help tracking which worlds are connected.

Write a program that maintains connectivity information about the systems. Two planets AA and BB are connected if there is a direct stargate between them, or if there is a sequence of planets P1,P2,…,PnP_1, P_2, \ldots, P_n with P1=AP_1 = A and Pn=BP_n = B such that Pk−1P_{k-1} and PkP_k are directly connected for every k∈{2,…,n}k \in \{2, \ldots, n\}. All connections are bidirectional, and there may be several paths between two planets.

Input

The input consists of one or more data sets and contains no blank lines. Each command is on its own line and starts with a single letter — 'D', 'C', or 'Q' (upper or lower case) — followed by 11 to 55 integers:

  • 'D' (define) takes one integer NN (N≤6000000N \le 6000000): it starts a new data set with planets numbered 11 to NN and no connections.
  • 'C' (connect) adds stargate connections between one or more pairs of planets.
  • 'Q' (query) asks whether one or more pairs of planets are connected.

The 'C' and 'Q' commands (written 'X' below) share the same argument syntax:

  • X src dst — one pair (src,dst)(src, dst).
  • X src dst nnn — the nnnnnn pairs (src,dst),(src,dst+1),…,(src,dst+nnn−1)(src, dst), (src, dst+1), \ldots, (src, dst+nnn-1). For example, X 1 100 3 refers to (1,100),(1,101),(1,102)(1,100), (1,101), (1,102).
  • X src dst nnn step — the nnnnnn pairs (src,dst+i⋅step)(src, dst + i \cdot step) for i=0,…,nnn−1i = 0, \ldots, nnn-1. For example, X 1 100 3 5 refers to (1,100),(1,105),(1,110)(1,100), (1,105), (1,110).
  • X src dst nnn dststep srcstep — the nnnnnn pairs (src+i⋅srcstep,dst+i⋅dststep)(src + i \cdot srcstep, dst + i \cdot dststep) for i=0,…,nnn−1i = 0, \ldots, nnn-1. For example, X 1 100 3 5 15 refers to (1,100),(16,105),(31,110)(1,100), (16,105), (31,110).

For a 'C' command every listed pair is connected; for a 'Q' command every listed pair is tested. All referenced planet numbers lie between 11 and the current NN.

Output

Print one line for every 'Q' (query) command, in order. Each line contains two integers separated by the three-character sequence ' - ' (a space, a hyphen-minus, and a space): first the number of pairs in that query that are connected, then the number of pairs that are not connected. Do not print any trailing spaces.

Examples3

  1. Example 1

    Input
    d 5
    C 1 3
    D 20
    q 1 3
    c 1 10 10
    Q 1 2 18 1 1
    
    Expected output
    0 - 1
    9 - 9
    
  2. Example 2

    Input
    D 6
    C 1 2
    C 2 3
    C 3 4
    Q 1 4
    Q 1 5
    Q 4 6
    
    Expected output
    1 - 0
    0 - 1
    0 - 1
    
  3. Example 3

    Input
    D 10
    C 1 2 4
    Q 1 2 4
    Q 2 3 3
    
    Expected output
    4 - 0
    3 - 0