//雪花飄飄北風嘯嘯
//天地一片蒼茫
#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;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15948 KB |
Output is correct |
2 |
Correct |
10 ms |
15992 KB |
Output is correct |
3 |
Correct |
9 ms |
15908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15992 KB |
Output is correct |
2 |
Correct |
10 ms |
16024 KB |
Output is correct |
3 |
Correct |
13 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15940 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15956 KB |
Output is correct |
2 |
Correct |
10 ms |
16032 KB |
Output is correct |
3 |
Correct |
14 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15984 KB |
Output is correct |
2 |
Correct |
10 ms |
15996 KB |
Output is correct |
3 |
Correct |
11 ms |
16100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16384 KB |
Output is correct |
2 |
Correct |
14 ms |
16460 KB |
Output is correct |
3 |
Correct |
14 ms |
16464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
19440 KB |
Output is correct |
2 |
Correct |
95 ms |
21720 KB |
Output is correct |
3 |
Correct |
130 ms |
23256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
982 ms |
42272 KB |
Output is correct |
2 |
Correct |
3968 ms |
106332 KB |
Output is correct |
3 |
Correct |
4152 ms |
122636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1890 ms |
70792 KB |
Output is correct |
2 |
Correct |
3607 ms |
100136 KB |
Output is correct |
3 |
Correct |
1336 ms |
95796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
917 ms |
53408 KB |
Output is correct |
2 |
Correct |
2530 ms |
84032 KB |
Output is correct |
3 |
Correct |
1503 ms |
95800 KB |
Output is correct |