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>
#define mid (L+R)/2
using namespace std;
int i,ans,k;
int f[300001],sg[300001];
queue<int>q1[300001];
queue<int>q2[300001];
vector<int>s;
void upd(int u,int L,int R,int pos,int y){
if(R < pos || L > pos)return;
if(R == L){sg[u]=y;return;}
else{
upd(u*2,L,mid,pos,y);
upd(u*2+1,mid+1,R,pos,y);
sg[u]=sg[u*2]+sg[u*2+1];
}
}
int get(int u,int L,int R,int a,int b){
if(b < L || a > R)return 0;
if(a <= L && b >= R)return sg[u];
return(get(u*2,L,mid,a,b)+get(u*2+1,mid+1,R,a,b));
}
int count_swaps(std:: vector<int>s){
int n=s.size();
for(i=0;i<s.size();i++){
f[i+1]=s[i];
}
for(i=1;i<=n;i++){
upd(1,1,n,i,1);
}
for(i=1;i<=n;i++){
if(f[i] < 0){
k=-(f[i]);
if(q2[k].size() == 0){
q1[k].push(i);
continue;
}
ans+=get(1,1,n,q2[k].front(),i)-1;
upd(1,1,n,q2[k].front(),2);
upd(1,1,n,i,0);
q2[k].pop();
}
if(f[i] > 0){
k=f[i];
if(q1[k].size() == 0){
q2[k].push(i);
continue;
}
ans+=get(1,1,n,q1[k].front(),i)-2;
upd(1,1,n,q1[k].front(),2);
upd(1,1,n,i,0);
q1[k].pop();
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'int count_swaps(std::vector<int>)':
shoes.cpp:25:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<s.size();i++){
~^~~~~~~~~
# | 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... |