#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;
typedef long long ll;
int add(int a, int b)
{
ll x = a+b;
if(x >= mod)
x -= mod;
if(x < 0)
x += mod;
return x;
}
ll mul(ll a, ll b)
{
return (a*b) % mod;
}
int n, q;
int v[1000002];
int st[1000002];
int tt[1000002], sz[1000002];
ll hsh[1000002], sthsh[1000002], pw[1000002];
map<ll, int> diff_nodes;
ll tot_pairs;
void add_nodes(int sgn, ll diff, int m)
{
if(diff != 0)
tot_pairs += sgn * m * diff_nodes[-diff];
diff_nodes[diff] += sgn * m;
}
int Find(int a)
{
if(tt[a] == a)
return a;
return tt[a] = Find(tt[a]);
}
void Union(int a, int b)
{
add_nodes(-1, hsh[a] - sthsh[a], sz[a]);
add_nodes(-1, hsh[b] - sthsh[b], sz[b]);
if(sz[a] > sz[b])
{
tt[b] = a, sz[a] += sz[b];
hsh[a] = add(hsh[a], hsh[b]);
sthsh[a] = add(sthsh[a], sthsh[b]);
add_nodes(1, hsh[a] - sthsh[a], sz[a]);
}
else
{
tt[a] = b, sz[b] += sz[a];
hsh[b] = add(hsh[a], hsh[b]);
sthsh[b] = add(sthsh[a], sthsh[b]);
add_nodes(1, hsh[b] - sthsh[b], sz[b]);
}
}
void swp(int a, int b)
{
int s1 = Find(a);
int s2 = Find(b);
add_nodes(-1, hsh[s1] - sthsh[s1], sz[s1]);
add_nodes(-1, hsh[s2] - sthsh[s2], sz[s2]);
sthsh[s1] = add(sthsh[s1], -pw[a]);
sthsh[s2] = add(sthsh[s2], -pw[b]);
hsh[s1] = add(hsh[s1], -pw[v[a]]);
hsh[s2] = add(hsh[s2], -pw[v[b]]);
swap(v[a], v[b]);
hsh[s1] = add(hsh[s1], pw[v[a]]);
hsh[s2] = add(hsh[s2], pw[v[b]]);
sthsh[s1] = add(sthsh[s1], pw[a]);
sthsh[s2] = add(sthsh[s2], pw[b]);
add_nodes(1, hsh[s1] - sthsh[s1], sz[s1]);
add_nodes(1, hsh[s2] - sthsh[s2], sz[s2]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
pw[0] = 1;
for(int i = 1; i <= 1000000; ++i)
pw[i] = mul(pw[i-1], 1000003);
for(int i = 1; i <= n; ++i)
cin >> v[i], st[i] = v[i], tt[i] = i, sz[i] = 1;
sort(st + 1, st + n + 1);
for(int i = 1; i <= n; ++i)
{
hsh[i] = pw[v[i]], sthsh[i] = pw[st[i]];
add_nodes(1, hsh[i] - sthsh[i], sz[i]);
}
for(int i = 1; i <= q; ++i)
{
int tip;
cin >> tip;
if(tip == 1)
{
int a, b;
cin >> a >> b;
if(Find(a) == Find(b))
{
swap(v[a], v[b]);
continue;
}
swp(a, b);
}
if(tip == 2)
{
int a, b;
cin >> a >> b;
if(Find(a) != Find(b))
Union(Find(a), Find(b));
}
if(tip == 3)
{
if(diff_nodes[0] == n)
cout << "DA\n";
else
cout << "NE\n";
}
if(tip == 4)
{
cout << tot_pairs << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
8184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8184 KB |
Output is correct |
2 |
Correct |
14 ms |
8184 KB |
Output is correct |
3 |
Incorrect |
14 ms |
8212 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8184 KB |
Output is correct |
2 |
Incorrect |
14 ms |
8312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
8284 KB |
Output is correct |
2 |
Incorrect |
14 ms |
8184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8184 KB |
Output is correct |
2 |
Incorrect |
15 ms |
8312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
8668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
81 ms |
11720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1597 ms |
41128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2734 ms |
81480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1226 ms |
47776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |