This page is still under construction.

Parts of this page are still being built. What you see may change.

Bridges

Time limit1sMemory limit256 MB

Summary
Towns on two banks link east on the north side and west on the south side while bridges are added and roads close and queries ask if one town reaches another.
Level

Hard8 of 10

Topics
Graph, Intervals, Binary search
Solved
No attempts yet

Problem

A river runs east-west with NN towns on the north bank (11 to NN) and NN on the south (N+1N+1 to 2N2N). On each bank, labels increase toward the east.

On the north bank, each town except NN has a one-way road to its eastern neighbor. On the south bank, each town except N+1N+1 has a one-way road to its western neighbor.

Roads may be permanently closed. Bridges connect one north town to one south town, work in both directions, never break, never cross, and each town uses at most one bridge.

Initially all roads are open and no bridges exist. Process MM events in order:

  • A G1 G2: build a bridge between G1G1 and G2G2.
  • B G1 G2: close the one-way road between G1G1 and G2G2.
  • Q G1 G2: ask whether G1G1 can reach G2G2 with current roads and bridges.

Input

Line 1: NN (1≤N≤1091 \le N \le 10^9) and MM (1≤M≤200 0001 \le M \le 200\,000).

Next MM lines describe events. Town ids satisfy 1≤G1,G2≤2N1 \le G1, G2 \le 2N and differ. Closed roads were open before; built bridges did not exist before.

Output

For each Q event, print DA if a route exists, otherwise NE, in input order.

Examples2

  1. Example 1

    Input
    5 6
    A 4 9
    Q 1 7
    B 3 2
    Q 1 7
    A 1 8
    Q 1 7
    
    Expected output
    DA
    NE
    DA
    
  2. Example 2

    Input
    6 10
    A 3 7
    A 4 10
    Q 1 11
    A 12 5
    Q 2 11
    B 10 11
    Q 2 10
    Q 9 6
    B 1 2
    Q 1 2
    
    Expected output
    NE
    DA
    DA
    DA
    NE