#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const ll mod = 1e9 + 7;
ll pr = 1000003;
ll pr1 = 1e9 + 9;
const int NMAX = 1e6 + 1;
map<pll, ll> mp;
ll par[NMAX];
ll p[NMAX];
ll po[NMAX][2];
ll a[NMAX][2];
ll b[NMAX][2];
ll q[NMAX];
ll sz[NMAX];
int bad = 0;
ll curans = 0;
void make_set(int v){
par[v] = v;
sz[v] = 1;
a[v][0] += po[p[v]][0];
b[v][0] += po[q[v]][0];
a[v][1] += po[p[v]][1];
b[v][1] += po[q[v]][1];
if(a[v][0] != b[v][0]|| a[v][1] != b[v][1]) bad++;
if(b[v][0] != a[v][0] || b[v][1] != a[v][1])curans += mp[{b[v][0] - a[v][0], b[v][1] - a[v][1]}];
mp[{a[v][0] - b[v][0], a[v][1] - b[v][1]}]++;
}
int find_set(int v){
return v == par[v] ? v : par[v] = find_set(par[v]);
}
void rem(int x){
if(b[x][1] != a[x][1] || b[x][0] != a[x][0] )curans -= mp[{b[x][0] - a[x][0], b[x][1] - a[x][1]}] * sz[x];
mp[{a[x][0] - b[x][0], a[x][1] - b[x][1]}] -= sz[x];
}
void add(int x){
if(b[x][1] != a[x][1] || b[x][0] != a[x][0] )curans += mp[{b[x][0] - a[x][0], b[x][1] - a[x][1]}] * sz[x];
mp[{a[x][0] - b[x][0], a[x][1] - b[x][1]}] += sz[x];
}
void swp(int u, int v){
int x = find_set(u);
int y = find_set(v);
if(x == y) return;
if(a[x][0] != b[x][0] || a[x][1] != b[x][1]) bad--;
if(a[y][0] != b[y][0] || a[y][1] != b[y][1]) bad--;
rem(x);
rem(y);
a[x][0] -= po[p[u]][0];
a[y][0] -= po[p[v]][0];
a[x][1] -= po[p[u]][1];
a[y][1] -= po[p[v]][1];
a[y][0] += po[p[u]][0];
a[x][0] += po[p[v]][0];
a[y][1] += po[p[u]][1];
a[x][1] += po[p[v]][1];
a[x][0] %= mod; a[x][0] += mod; a[x][0] %= mod;
a[y][0] %= mod; a[y][0] += mod; a[y][0] %= mod;
a[x][1] %= mod; a[x][1] += mod; a[x][1] %= mod;
a[y][1] %= mod; a[y][1] += mod; a[y][1] %= mod;
add(x);
add(y);
if(a[x][0] != b[x][0] || a[x][1] != b[x][1]) bad++;
if(a[y][0] != b[y][0] || a[y][1] != b[y][1]) bad++;
}
void add_edge(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v) return;
if(a[u][0] != b[u][0] || a[u][1] != b[u][1]) bad--;
if(a[v][0] != b[v][0] || b[v][1] != a[v][1]) bad--;
rem(u);
rem(v);
//cout << curans << "\n";
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
a[u][0] += a[v][0]; a[u][0] %= mod;
a[u][1] += a[v][1]; a[u][1] %= mod;
b[u][1] += b[v][1]; b[u][1] %= mod;
b[u][0] += b[v][0]; b[u][0] %= mod;
if(a[u][0] != b[u][0] || a[u][1] != b[u][1]) bad++;
add(u);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int m; cin >> m;
po[0][0] = po[0][1] = 1;
for(int i = 1; i < NMAX; i++) po[i][0] = po[i - 1][0] * pr % mod, po[i][1] = po[i - 1][1] * pr1 % mod;
for(int i = 1; i <= n; i++){
cin >> p[i];
q[i] = p[i];
}
sort(q + 1, q + 1 + n);
for(int i = 1; i <= n; i++) make_set(i);
while(m--){
int type; cin >> type;
if(type == 1){
int u, v; cin >> u >> v;
swp(u, v);
swap(p[u], p[v]);
}
if(type == 2){
int u, v; cin >> u >> v;
add_edge(u, v);
}
if(type == 3){
if(bad == 0) cout << "DA\n";
else cout << "NE\n";
}
if(type == 4){
cout << curans << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
15976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15956 KB |
Output is correct |
2 |
Correct |
14 ms |
15952 KB |
Output is correct |
3 |
Incorrect |
15 ms |
15956 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
15956 KB |
Output is correct |
2 |
Incorrect |
16 ms |
15972 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
15956 KB |
Output is correct |
2 |
Incorrect |
14 ms |
15988 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16084 KB |
Output is correct |
2 |
Incorrect |
15 ms |
16096 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
16684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
22588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1067 ms |
64604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1907 ms |
120760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
858 ms |
86776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |