#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)
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 int MOD = (int)1e9 + 7;
int MAX_A;
void add(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void sub(int &x, int y) {
x -= y;
if (x < 0) x += MOD;
}
int nNode, q;
int a[MAX_N], sorted[MAX_N];
int poww[MAX_N];
int nCom;
int par[MAX_N], sz[MAX_N], modelHash[MAX_N], curHash[MAX_N];
map<int, int> cnt;
long long ans = 0;
int getPair(int u) {
return curHash[u] - modelHash[u];
}
void updateMap(int u, int delta) {
int 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] = 1LL * poww[i - 1] * base % MOD;
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) return;
updateMap(u, -1);
updateMap(v, -1);
sub(curHash[u], poww[MAX_A - a[i]]);
sub(curHash[v], poww[MAX_A - a[j]]);
swap(i, 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;
nCom--;
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";
}
}
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!!!! */
Compilation message (stderr)
zamjene.cpp: In function 'int32_t main()':
zamjene.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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".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... |