#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mx = 1e6+5, p = 1e6+3;
int n, q; int vals[mx], sorted[mx];
int par[mx], sz[mx]; ll pPow[mx], cnP[mx], cnQ[mx];
map<ll, ll> diff; ll cnt;
int find(int node) { return (node == par[node]) ? node : par[node] = find(par[node]); }
void upd(int i, int change){
change *= sz[i];
if (cnP[i] != cnQ[i]) cnt += diff[cnQ[i]-cnP[i]]*change;
diff[cnP[i]-cnQ[i]] += change;
}
void merge(int a, int b){
a = find(a); b = find(b);
if (a == b) return;
if (sz[a] > sz[b]) swap(a, b);
upd(a, -1); upd(b, -1);
par[a] = b; sz[b] += sz[a]; cnP[b] += cnP[a]; cnQ[b] += cnQ[a];
upd(b, 1);
}
void Swap(int a, int b){
int parA = find(a), parB = find(b);
upd(parA, -1); upd(parB, -1);
cnP[parA] = (cnP[parA]-pPow[vals[a]]+pPow[vals[b]]);
cnP[parB] = (cnP[parB]-pPow[vals[b]]+pPow[vals[a]]);
upd(parA, 1); upd(parB, 1);
swap(vals[a], vals[b]);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q; pPow[0] = 1;
for (int i = 0; i < n; i++){
cin >> vals[i]; sorted[i] = vals[i];
pPow[i+1] = (pPow[i]*p);
}
sort(sorted, sorted+n); iota(par, par+n, 0); fill(sz, sz+n, 1);
for (int i = 0; i < n; i++){
cnP[i] = pPow[vals[i]], cnQ[i] = pPow[sorted[i]];
diff[cnP[i]-cnQ[i]]++;
}
map<int, bool> visited;
for (int i = 0; i < n; i++){
if (cnP[i] != cnQ[i] and !visited[cnP[i]-cnQ[i]]){
cnt += diff[cnP[i]-cnQ[i]]*diff[cnQ[i]-cnP[i]];
visited[cnP[i]-cnQ[i]] = true; visited[cnQ[i]-cnP[i]] = true;
}
}
for (int i = 0; i < q; i++){
int t, a, b; cin >> t; if (t <= 2) { cin >> a >> b; a--; b--; }
if (t == 1) Swap(a, b);
if (t == 2) merge(a, b);
if (t == 3) cout<<((diff[0] == n) ? "DA":"NE")<<endl;
if (t == 4) cout<<cnt<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
308 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
928 KB |
Output is correct |
2 |
Correct |
14 ms |
964 KB |
Output is correct |
3 |
Correct |
14 ms |
948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
4676 KB |
Output is correct |
2 |
Correct |
180 ms |
6468 KB |
Output is correct |
3 |
Correct |
246 ms |
9988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1976 ms |
36144 KB |
Output is correct |
2 |
Execution timed out |
6076 ms |
146860 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3232 ms |
81336 KB |
Output is correct |
2 |
Correct |
5998 ms |
115516 KB |
Output is correct |
3 |
Correct |
2738 ms |
103348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1916 ms |
47172 KB |
Output is correct |
2 |
Correct |
3983 ms |
86676 KB |
Output is correct |
3 |
Correct |
2792 ms |
89900 KB |
Output is correct |