#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define N 1000001
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
int p[N], sz[N], n;
ll ans, cnt;
gp_hash_table <ll, int> ht;
ll h[N];
void change (int x, ll d) {
if (h[x]) ans -= sz[x] * ht[-h[x]];
ht[h[x]] -= sz[x];
if (!d) return;
h[x] += d;
if (h[x]) ans += sz[x] * ht[-h[x]];
ht[h[x]] += sz[x];
}
int find (int x) {
if (p[x] == x) return x;
return p[x] = find (p[x]);
}
void unite (int a, int b) {
a = find (a), b = find (b);
if (a == b) return;
p[b] = a;
if (h[a]) ans -= sz[a] * ht[-h[a]];
ht[h[a]] -= sz[a];
h[a] += h[b];
sz[a] += sz[b];
if (h[a]) ans += sz[a] * ht[-h[a]];
ht[h[a]] += sz[a];
change (b, 0);
cnt--;
}
int q, a[N], s[N], rn[N];
int main () {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
s[i] = a[i];
}
sort (s, s + n);
for (int i = 1; i <= n; i++) {
rn[i] = uniform_int_distribution<int>(0, (1 << 30))(rng);
}
for (int i = 0; i < n; i++) {
p[i] = i, sz[i] = 1;
h[i] = rn[a[i]] - rn[s[i]];
ht[h[i]]++;
}
for (int i = 0; i < n; i++) {
if (h[i]) ans += ht[-h[i]];
}
ans /= 2;
cnt = n;
for (int i = 0; i < q; i++) {
int t, x, y;
cin >> t;
if (t == 2) {
cin >> x >> y;
x--, y--;
unite (x, y);
}
if (t == 1) {
cin >> x >> y;
x--, y--;
int px = find (x), py = find (y);
change (px, -rn[a[x]]);
change (py, -rn[a[y]]);
swap (a[x], a[y]);
change (px, rn[a[x]]);
change (py, rn[a[y]]);
}
if (t == 3) {
if (ht[0] == n) cout << "DA\n";
else cout << "NE\n";
}
if (t == 4) {
cout << ans << '\n';
}
cout << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
376 KB |
Output is correct |
2 |
Correct |
7 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
504 KB |
Output is correct |
2 |
Correct |
7 ms |
504 KB |
Output is correct |
3 |
Correct |
7 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
504 KB |
Output is correct |
2 |
Correct |
9 ms |
632 KB |
Output is correct |
3 |
Correct |
8 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
504 KB |
Output is correct |
2 |
Correct |
8 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1016 KB |
Output is correct |
2 |
Correct |
35 ms |
1144 KB |
Output is correct |
3 |
Correct |
36 ms |
1148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
5048 KB |
Output is correct |
2 |
Correct |
337 ms |
9332 KB |
Output is correct |
3 |
Correct |
343 ms |
13928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3118 ms |
65092 KB |
Output is correct |
2 |
Incorrect |
3563 ms |
175852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3501 ms |
117516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3277 ms |
52532 KB |
Output is correct |
2 |
Incorrect |
3509 ms |
117400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |