This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct pairs{
ll l,r;
bool operator<(const pairs& p)const{
// l,r,p.l,p.r
return max(l,r)<max(p.l,p.r);
}
};
vector<pairs>bruh;
struct FEN{
vector<ll>fen;
void init(ll n){
fen.resize(n+5);
for(int i=0;i<=n+4;i++){
fen[i]=0;
}
}
void upd(ll n, ll m, ll val){
for(int i=n;i<=m;i+=i&(-i)){
fen[i]+=val;
// cout<<i;
}
}
ll sum(ll e){
ll su=0;
while(e>=1){
su+=fen[e];
e-=e&(-e);
}
// cout<<1;
return su;
}
}f;
vector<int>adj[1000005],adj2[1000005]; int ptr[1000005];
long long count_swaps(std::vector<int> s) {
vector<ll>make;
ll why=s.size();
for(int i=0;i<why;i++){
if(s[i]<0){
make.push_back(-(s[i]));
adj[-(s[i])].push_back(i+1);
}
else{
adj2[s[i]].push_back(i+1);
}
}
for(int i=0;i<=why+4;i++){
ptr[i]=0;
}
// for(auto u: adj[2]){
// cout<<u<<" ";
// }
ll siz=make.size();
for(int i=0;i<siz;i++){
bruh.push_back({adj[make[i]][ptr[make[i]]],adj2[make[i]][ptr[make[i]]]});
ptr[make[i]]++;
}
sort(bruh.begin(),bruh.end());
// for(int i=0;i<bruh.size();i++){
// for(int j=0;j<bruh.size()-1;j++){
// if(bruh[j+1]<bruh[j]){
// swap(bruh[j],bruh[j+1]);
// }
// }
// }
// merge_sort(0,bruh.size()-1);
vector<ll>real;
ll siz2=bruh.size();
for(int i=0;i<siz2;i++){
real.push_back(bruh[i].l);
real.push_back(bruh[i].r);
}
// for(auto u: real){
// cout<<u<<" ";
// }
f.init(300000);
ll ans=0;
ll siz3=real.size();
for(int i=0;i<siz3;i++){
ans+=f.sum(300000)-f.sum(real[i]);
// cout<<f.sum(200000)<<" "<<real[i]<<" ";
// cout<<f.sum(real[i])<<"\n";
f.upd(real[i],300000,1);
}
return ans;
// return 0;
}
// #ifndef ONLINE_JUDGE
// int main() {
// int n;
// cin>>n;
// vector<int> S(2 * n);
// for (int i = 0; i < 2 * n; i++){
// cin>>S[i];
// }
// long long result = count_swaps(S);
// cout<<result<<"\n";
// return 0;
// }
// #endif
# | 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... |