#include <bits/stdc++.h>
using namespace std;
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
using ll = long long;
const int N = 1e6 + 5;
int n, q, a[2][N], par[N], sz[N];
ll hsh[N], sum_hsh[N][2], answer;
map<ll, ll> hsh_counter;
ll cnt_hsh(ll x){
return (hsh_counter.count(x) ? hsh_counter[x] : 0ll);
}
int asc(int x){
return x == par[x] ? x : par[x] = asc(par[x]);
}
void upd_cnt(int x, int op){
ll cur_sz = sz[asc(x)];
ll target = sum_hsh[x][1] - sum_hsh[x][0];
if(target != 0) answer = answer + op * cur_sz * cnt_hsh(target);
hsh_counter[-target] += op * cur_sz;
}
void preprocess(){
FOR(i, 1, n){
par[i] = i, sz[i] = 1;
FOR(j, 0, 1) sum_hsh[i][j] = hsh[a[j][i]];
upd_cnt(i, +1);
}
}
bool join(int u, int v){
u = asc(u), v = asc(v);
if(u != v){
if(sz[u] < sz[v]) swap(u, v);
upd_cnt(u, -1), upd_cnt(v, -1);
sum_hsh[u][0] += sum_hsh[v][0];
sum_hsh[u][1] += sum_hsh[v][1];
par[v] = u, sz[u] += sz[v];
upd_cnt(u, +1);
return true;
}
return false;
}
void update_pos(int x, int y){
int par_x = asc(x), par_y = asc(y);
if(par_x == par_y){
swap(a[0][x], a[0][y]);
}
else{
upd_cnt(par_x, -1), upd_cnt(par_y, -1);
sum_hsh[par_x][0] -= hsh[a[0][x]];
sum_hsh[par_y][0] -= hsh[a[0][y]];
swap(a[0][x], a[0][y]);
sum_hsh[par_x][0] += hsh[a[0][x]];
sum_hsh[par_y][0] += hsh[a[0][y]];
upd_cnt(par_x, +1), upd_cnt(par_y, +1);
}
}
void solve()
{
cin >> n >> q;
vector<int> comp;
FOR(i, 1, n){
cin >> a[0][i];
a[1][i] = a[0][i];
comp.push_back(a[0][i]);
}
sort(a[1] + 1, a[1] + 1 + n);
sort(all(comp)), compact(comp);
FOR(i, 1, n){
a[0][i] = lower_bound(all(comp), a[0][i]) - comp.begin() + 1;
a[1][i] = lower_bound(all(comp), a[1][i]) - comp.begin() + 1;
}
hsh[0] = 1;
FOR(i, 1, sz(comp)) hsh[i] = hsh[i - 1] * ((ll) 1e11 + 3);
preprocess();
while(q--){
int op; cin >> op;
if(op == 1){
int a,b; cin >> a >> b;
update_pos(a, b);
}
else if(op == 2){
int a,b; cin >> a >> b;
join(a, b);
}
else if(op == 3){
cout << (hsh_counter[0] == n ? "DA" : "NE") << "\n";
}
else cout << answer << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
zamjene.cpp: In function 'int main()':
zamjene.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
zamjene.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |