This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll mod=1e9+7;
const ll inf=1e18*4;
const ld pai=acos(-1);
ll n,q;
ll a[1000009],b[1000009];
ll p[1000009];
ll hasha[1000009],hashb[1000009],sz[1000009];
ll loser,ans;
map<ll,ll>mp;
ll H=1234577;
ll po[1000009];
void show(){
cout<<loser<<" "<<ans<<endl;
for(int i=0;i<n;i++)cout<<hasha[i]<<" "<<hashb[i]<<endl;
cout<<"--------------"<<endl;
}
ll get(ll node){
if(p[node]==node)return node;
return p[node]=get(p[node]);
}
void fill(){
po[0]=1;
for(ll i=1;i<=1e6;i++){
po[i]=po[i-1]*H;
po[i]%=mod;
}
}
ll HASH(ll x){return po[x];}
void in(ll node){
mp[hasha[node]-hashb[node]]+=sz[get(node)];
mp[0]=0;
ans+=mp[-hasha[node]+hashb[node]]*sz[get(node)];
loser+=(hasha[node]!=hashb[node]);
}
void out(ll node){
mp[hasha[node]-hashb[node]]-=sz[get(node)];
mp[0]=0;
ans-=mp[-hasha[node]+hashb[node]]*sz[get(node)];
loser-=(hasha[node]!=hashb[node]);
}
void add(ll node,ll x){
hasha[node]+=HASH(a[x]);
hasha[node]%=mod;
}
void remove(ll node,ll x){
hasha[node]-=HASH(a[x]);
hasha[node]+=mod;
hasha[node]%=mod;
}
void merge(ll a,ll b){
a=get(a);
b=get(b);
if(a==b)return ;
//cout<<a<<" "<<b<<endl;
out(a);
out(b);
p[b]=a;
sz[a]+=sz[b];
hasha[a]+=hasha[b];
hashb[a]+=hashb[b];
hasha[a]%=mod;
hashb[a]%=mod;
in(a);
}
void xxx(int x,int y){
swap(a[x],a[y]);
}
int main(){
cin>>n>>q;
for(int i=0;i<n;i++)cin>>a[i],b[i]=a[i];
sort(b,b+n);
fill();
for(int i=0;i<n;i++){
p[i]=i;
sz[i]=1;
hasha[i]=HASH(a[i]);
hashb[i]=HASH(b[i]);
in(i);
}
//show();
while(q--){
int t;cin>>t;
if(t==1){
ll a,b;
cin>>a>>b;
a--,b--;
if(get(a)==get(b))C;
out(get(a));
out(get(b));
add(get(a),b);
remove(get(a),a);
add(get(b),a);
remove(get(b),b);
in(get(a));
in(get(b));
xxx(a,b);
}
if(t==2){
ll a,b;
cin>>a>>b;
a--,b--;
merge(a,b);
}
if(t==3){
if(loser)cout<<"NE"<<endl;
else cout<<"DA"<<endl;
}
if(t==4){
cout<<ans<<endl;
}
//show();
}
}
# | 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... |