이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD=1e18;
ll hh[1000005];
int n,q;
ll arr[1000005];
ll brr[1000005];
int p[1000005];
int s[1000005];
ll ha[1000005];
ll hb[1000005];
int par(int i){
	if (i==p[i]) return i;
	else return p[i]=par(p[i]);
}
ll bad=0;
ll cloud=0;
map<ll,ll> m;
void add(int i){
	i=par(i);
	
	if (ha[i]!=hb[i]){
		bad++;
		ll temp=(ha[i]-hb[i]+MOD)%MOD;
		if (m.count(temp)) cloud+=s[i]*m[temp];
		m[MOD-temp]+=s[i];
	}
}
void rmv(int i){
	i=par(i);
	if (ha[i]!=hb[i]){
		bad--;
		ll temp=(ha[i]-hb[i]+MOD)%MOD;
		if (m.count(temp)) cloud-=s[i]*m[temp];
		m[MOD-temp]-=s[i];
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	rep(x,0,1000005) hh[x]=rng()%MOD;
	rep(x,0,1000005) p[x]=x;
	rep(x,0,1000005) s[x]=1;
	
	cin>>n>>q;
	rep(x,0,n) cin>>arr[x];
	rep(x,0,n) brr[x]=arr[x];
	
	sort(brr,brr+n);
	
	rep(x,0,n) arr[x]=hh[arr[x]];
	rep(x,0,n) brr[x]=hh[brr[x]];
	
	rep(x,0,n) ha[x]=arr[x];
	rep(x,0,n) hb[x]=brr[x];
	
	
	rep(x,0,n) add(x);
	
	int a,b;
	while (q--){
		cin>>a;
		
		if (a==1){
			cin>>a>>b;
			a--,b--;
			
			if (par(a)!=par(b)){
				rmv(a);
				rmv(b);
				ha[par(a)]=(ha[par(a)]-arr[a]+arr[b]+MOD)%MOD;
				ha[par(b)]=(ha[par(b)]-arr[b]+arr[a]+MOD)%MOD;
				add(a);
				add(b);
			}
			swap(arr[a],arr[b]);
		}
		else if (a==2){
			cin>>a>>b;
			a--,b--;
			
			a=par(a),b=par(b);
			
			if (a!=b){
				rmv(a);
				rmv(b);
				ha[a]=(ha[a]+ha[b])%MOD;
				hb[a]=(hb[a]+hb[b])%MOD;
				p[b]=a;
				s[a]+=s[b];
				add(a);
			}
		}
		else if (a==3){
			if (bad==0) cout<<"DA"<<endl;
			else cout<<"NE"<<endl;
		}
		else{
			cout<<cloud<<endl;
		}
	}
}
| # | 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... |