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 "shoes.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
long long res[200010];
int vis[200011], dp[200010];
vector <int> a1[100002], b[100002];
const int maxn=2e3+7;
vector<int> a;
vector<int> t(4*maxn);
vector<int> add(4*maxn,0);
void push(int v)
{
if(add[v])
{
add[v*2]+=add[v];
add[v*2+1]+=add[v];
add[v]=0;
}
}
void build(int v,int tl,int tr)
{
if(tl==tr)
{
t[v]=a[tl];
}
else
{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
}
}
int get(int v,int tl,int tr,int pos)
{
if(tl==tr)
{
t[v]+=add[v];
add[v]=0;
return t[v];
}
else
{
int tm=(tl+tr)/2;
push(v);
if(pos<=tm)
{
return get(v*2,tl,tm,pos);
}
else
{
return get(v*2+1,tm+1,tr,pos);
}
}
}
void Add(int v,int tl,int tr,int l,int r,int val)
{
if(l>r)
{
return;
}
if(tl==l && tr==r)
{
add[v]+=val;
return;
}
else
{
int tm=(tl+tr)/2;
Add(v*2,tl,tm,l,min(r,tm),val);
Add(v*2+1,tm+1,tr,max(tm+1,l),r,val);
}
}
long long count_swaps(std::vector<int> s) {
long long sum = 0;
for(int i = 0 ; i < s.size(); i++){
if(s[i]>0)
a1[s[i]].push_back(i);
else
b[abs(s[i])].push_back(i);
}
for(int i = 0 ; i <100001;i++){
for(int j = 0 ; j < a1[i].size();j++){
dp[a1[i][j]]=b[i][j];
dp[b[i][j]]=a1[i][j];
}
}
int n = s.size()+1;
for(int i = 0 ; i < s.size(); i++){
if(vis[i]==1)
continue;
int j=i+1;
sum += dp[i] - i - 1 + get(1,0,n-1,dp[i]) - get(1,0,n-1,i+1);
Add(1,0,n,i,dp[i],1);
cout << sum<<endl;
if(s[i] > 0)
sum++;
vis[dp[i]]=1;
}
return sum;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 0 ; i < s.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:89:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int j = 0 ; j < a1[i].size();j++){
| ~~^~~~~~~~~~~~~~
shoes.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 0 ; i < s.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:99:13: warning: unused variable 'j' [-Wunused-variable]
99 | int j=i+1;
| ^
# | 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... |