/*
-
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
gp_hash_table<ll,int> cnt;
const int m=1e6+1,N=1e6+1;
const ll mod=8473968574039; //just a random large number
vector<int> par(N),siz(N,1),p,q;
vector<ll> pwr(1,1),hsh(N);
int n,qq;
ll ans4;
int ans3;
ll mul(ll a,ll b)
{
return a*b;
}
ll add(ll a,ll b)
{
return a+b;
a+=b;
if(a>=mod)
a-=mod;
return a;
}
ll sub(ll a,ll b)
{
return a-b;
a-=b;
if(a<0)
a+=mod;
return a;
}
int find(int tr)
{
if(par[tr]==tr)
return tr;
return par[tr]=find(par[tr]);
}
void query(int a,int b,int t)
{
int pa=p[a],pb=p[b];
if(t==1)
swap(p[a],p[b]);
a=find(a);
b=find(b);
if(a==b)
return;
cnt[hsh[a]]-=siz[a];
if(hsh[a]!=0)
ans4-=(ll)siz[a]*cnt[sub(0,hsh[a])],ans3--;
if(hsh[b]!=0)
ans4-=(ll)siz[b]*cnt[sub(0,hsh[b])],ans3--;
cnt[hsh[b]]-=siz[b];
if(t==1)
{
hsh[a]=sub(hsh[a],pwr[pa]);
hsh[a]=add(hsh[a],pwr[pb]);
hsh[b]=sub(hsh[b],pwr[pb]);
hsh[b]=add(hsh[b],pwr[pa]);
if(hsh[b]!=0)
cnt[hsh[b]]+=siz[b],ans4+=(ll)siz[b]*cnt[sub(0,hsh[b])],ans3++;
}
else
{
hsh[a]=add(hsh[a],hsh[b]);
siz[a]+=siz[b];
par[b]=a;
}
if(hsh[a]!=0)
cnt[hsh[a]]+=siz[a],ans4+=(ll)siz[a]*cnt[sub(0,hsh[a])],ans3++;
}
int main()
{
for(int i=0;i<N;i++)
par[i]=i,pwr.pb(mul(pwr.back(),m));
scanf("%i %i",&n,&qq);
p.resize(n);
for(int i=0;i<n;i++)
scanf("%i",&p[i]);
q=p;
sort(all(q));
for(int i=0;i<n;i++){
hsh[i]=add(hsh[i],pwr[p[i]]),hsh[i]=sub(hsh[i],pwr[q[i]]),cnt[hsh[i]]++;
if(hsh[i]!=0)
ans4+=cnt[sub(0,hsh[i])],ans3++;
}
while(qq--)
{
int t,a,b;
scanf("%i",&t);
if(t==1||t==2)
scanf("%i %i",&a,&b),query(a-1,b-1,t);
if(t==3)
printf(ans3==0?"DA\n":"NE\n");
if(t==4)
printf("%lld\n",ans4);
}
return 0;
}
Compilation message
zamjene.cpp: In function 'int main()':
zamjene.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&qq);
~~~~~^~~~~~~~~~~~~~~~
zamjene.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&p[i]);
~~~~~^~~~~~~~~~~~
zamjene.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&t);
~~~~~^~~~~~~~~
zamjene.cpp:117:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&a,&b),query(a-1,b-1,t);
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24420 KB |
Output is correct |
2 |
Correct |
31 ms |
24420 KB |
Output is correct |
3 |
Correct |
31 ms |
24288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
24288 KB |
Output is correct |
2 |
Correct |
30 ms |
24292 KB |
Output is correct |
3 |
Correct |
30 ms |
24288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
24544 KB |
Output is correct |
2 |
Correct |
31 ms |
24292 KB |
Output is correct |
3 |
Correct |
31 ms |
24416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24288 KB |
Output is correct |
2 |
Correct |
31 ms |
24388 KB |
Output is correct |
3 |
Correct |
31 ms |
24380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
24416 KB |
Output is correct |
2 |
Correct |
31 ms |
24288 KB |
Output is correct |
3 |
Correct |
32 ms |
24288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
24288 KB |
Output is correct |
2 |
Correct |
36 ms |
24288 KB |
Output is correct |
3 |
Correct |
35 ms |
24288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
25176 KB |
Output is correct |
2 |
Correct |
102 ms |
28032 KB |
Output is correct |
3 |
Correct |
125 ms |
37204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
743 ms |
55120 KB |
Output is correct |
2 |
Correct |
1719 ms |
182824 KB |
Output is correct |
3 |
Correct |
2113 ms |
330760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1159 ms |
114756 KB |
Output is correct |
2 |
Correct |
1530 ms |
113092 KB |
Output is correct |
3 |
Correct |
1081 ms |
113580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
795 ms |
41324 KB |
Output is correct |
2 |
Correct |
1191 ms |
114164 KB |
Output is correct |
3 |
Correct |
1061 ms |
113456 KB |
Output is correct |