#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
mt19937_64 rnd(time(0));
const int N = 1000000 + 3;
int n, q, sz[N], a[N], b[N], p[N], cnt, ans3;
ll sum[N], nsum[N], ans;
unordered_map <ll, int> mp;
map <int, ll> code;
void init(){
for (int i = 0; i < N; i++)
code[i] = rnd();
for (int i = 0; i < n; i++){
p[i] = i;
sz[i] = 1;
sum[i] = code[a[i]];
nsum[i] = code[b[i]];
if (sum[i] != nsum[i]){
ans += mp[nsum[i] - sum[i]];
mp[sum[i] - nsum[i]]++;
}else ans3++;
}
}
int get_anc(int x){
if (p[x] == x) return x;
return p[x] = get_anc(p[x]);
}
void er(int x){
if (nsum[x] != sum[x]){
ans -= mp[nsum[x] - sum[x]] * sz[x];
mp[sum[x] - nsum[x]] -= sz[x];
}else ans3--;
}
void add(int x){
if (nsum[x] != sum[x]){
ans += mp[nsum[x] - sum[x]] * sz[x];
mp[sum[x] - nsum[x]] += sz[x];
}else ans3++;
}
void unite(int x, int y){
int px = get_anc(x);
int py = get_anc(y);
if (px == py) return;
cnt--;
er(px);
er(py);
if (sz[px] < sz[py]) swap(px, py);
p[py] = px;
sz[px] += sz[py];
sum[px] += sum[py];
nsum[px] += nsum[py];
add(px);
}
void sw(int x, int y){
int px = get_anc(x);
int py = get_anc(y);
if (px == py) {
swap(a[x], a[y]);
return;
}
er(px);
er(py);
sum[px] += code[a[y]] - code[a[x]];
sum[py] += code[a[x]] - code[a[y]];
add(px);
add(py);
swap(a[x], a[y]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
init();
cnt = n;
for (int i = 0; i < q; i++){
int tp, x, y;
cin >> tp;
if (tp == 1){
cin >> x >> y;
--x; --y;
sw(x, y);
}else
if (tp == 2){
cin >> x >> y;
--x; --y;
unite(x, y);
}else
if (tp == 3){
if (cnt==ans3) cout << "DA\n";
else cout << "NE\n";
}else{
cout << ans << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
63224 KB |
Output is correct |
2 |
Correct |
276 ms |
63056 KB |
Output is correct |
3 |
Correct |
285 ms |
63008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
351 ms |
63096 KB |
Output is correct |
2 |
Correct |
317 ms |
63036 KB |
Output is correct |
3 |
Correct |
278 ms |
63096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
284 ms |
62968 KB |
Output is correct |
2 |
Correct |
280 ms |
62992 KB |
Output is correct |
3 |
Correct |
298 ms |
63224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
63096 KB |
Output is correct |
2 |
Correct |
283 ms |
62968 KB |
Output is correct |
3 |
Correct |
300 ms |
63096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
279 ms |
63092 KB |
Output is correct |
2 |
Correct |
285 ms |
63096 KB |
Output is correct |
3 |
Correct |
282 ms |
63220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
63480 KB |
Output is correct |
2 |
Correct |
302 ms |
63480 KB |
Output is correct |
3 |
Correct |
303 ms |
63608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
66552 KB |
Output is correct |
2 |
Correct |
423 ms |
67892 KB |
Output is correct |
3 |
Correct |
391 ms |
69768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1288 ms |
90168 KB |
Output is correct |
2 |
Correct |
2502 ms |
147536 KB |
Output is correct |
3 |
Correct |
3039 ms |
187728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1783 ms |
123912 KB |
Output is correct |
2 |
Correct |
2259 ms |
146796 KB |
Output is correct |
3 |
Correct |
1447 ms |
127928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1349 ms |
100740 KB |
Output is correct |
2 |
Correct |
1977 ms |
126188 KB |
Output is correct |
3 |
Correct |
1533 ms |
128016 KB |
Output is correct |