#include <bits/stdc++.h>
using namespace std;
#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define ull long long
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
const int MAX_N = (int)1e6 + 3;
const int base = 1234577;
const long long MOD = (long long)1e18 + 7;
int MAX_A;
inline void add(ull &x, ull y) {
    x += y;
}
inline void sub(ull &x, ull y) {
    x -= y;
}
int nNode, q;
int a[MAX_N], sorted[MAX_N];
ull poww[MAX_N];
int nCom;
int par[MAX_N], sz[MAX_N];
ull modelHash[MAX_N], curHash[MAX_N];
map<ull, int> cnt;
long long ans = 0;
inline ull getPair(int u) {
    return curHash[u] - modelHash[u];
}
inline void updateMap(int u, int delta) {
    ull val = getPair(u);
    if (val != 0)
        ans += 1LL * delta * sz[u] * cnt[-val];
    cnt[val] += delta * sz[u];
}
void init() {
    poww[0] = 1;
    FOR(i, 1, MAX_A) poww[i] = poww[i - 1] * base;
    FOR(i, 1, nNode) {
        par[i] = i;
        sz[i] = 1;
        modelHash[i] = poww[MAX_A - sorted[i]];
        curHash[i] = poww[MAX_A - a[i]];
        updateMap(i, 1);
    }
    nCom = nNode;
}
int getP(int u) {
    return (u == par[u] ? u : par[u] = getP(par[u]));
}
void swapPos(int i, int j) {
    int u = getP(i), v = getP(j);
    if (u == v) {
        swap(a[i], a[j]);
        return;
    }
    updateMap(u, -1);
    updateMap(v, -1);
    sub(curHash[u], poww[MAX_A - a[i]]);
    sub(curHash[v], poww[MAX_A - a[j]]);
    swap(a[i], a[j]);
    add(curHash[u], poww[MAX_A - a[i]]);
    add(curHash[v], poww[MAX_A - a[j]]);
    updateMap(u, +1);
    updateMap(v, +1);
}
bool unite(int u, int v) {
    u = getP(u), v = getP(v);
    if (u == v) return false;
    if (sz[u] < sz[v]) swap(u, v);
    updateMap(u, -1);
    updateMap(v, -1);
    add(modelHash[u], modelHash[v]);
    add(curHash[u], curHash[v]);
    par[v] = u;
    sz[u] += sz[v];
    updateMap(u, +1);
    return true;
}
void solve() {
    cin >> nNode >> q;
    FOR(i, 1, nNode) cin >> a[i], sorted[i] = a[i];
    sort(sorted + 1, sorted + nNode + 1);
    MAX_A = sorted[nNode];
    init();
    while (q--) {
        int type, i, j;
        cin >> type;
        if (type == 1) {
            cin >> i >> j;
            swapPos(i, j);
        }
        else if (type == 2) {
            cin >> i >> j;
            unite(i, j);
        }
        else if (type == 3)
            cout << (cnt[0] == nNode ? "DA\n" : "NE\n");
        else
            cout << ans << "\n";
//        FOR(i, 1, nNode) cout << i << ": " << curHash[i] << " " << modelHash[i] << "\n";
    }
}
int32_t main() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    bool multitest = 0;
    int numTest = 1;
    if (multitest) cin >> numTest;
    while (numTest--) {
        solve();
    }
    return 0;
}
/* Lak lu theo dieu nhac!!!! */
컴파일 시 표준 에러 (stderr) 메시지
zamjene.cpp: In function 'int32_t main()':
zamjene.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
zamjene.cpp:154:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |