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 <cstdio>
#include <cassert>
#define pb push_back
using namespace std;
int n;
vector<int> tike[100005][2];
int dokle[100005][2];
int seg[600005];
int lazy[600005];
bool bio[200005];
void init(int l, int r, int ind){
if(l == r){
seg[ind] = l;
return;
}
int mid = (l + r) / 2;
init(l, mid, ind * 2);
init(mid + 1, r, ind * 2 + 1);
seg[ind] = seg[ind * 2] + seg[ind * 2 + 1];
}
void propagate(int l, int r, int ind){
if(lazy[ind] != 0){
seg[ind] += (r - l + 1)*lazy[ind];
int mid = (l + r) / 2;
if(l < r){
lazy[ind * 2] += lazy[ind];
lazy[ind * 2 + 1] += lazy[ind];
}
lazy[ind] = 0;
}
}
int query(int l,int r, int ind, int gde){
propagate(l,r,ind);
if(l == r)return seg[ind];
int mid = (l + r)/2;
if(gde <= mid)return query(l,mid,ind * 2, gde);
else return query(mid + 1, r, ind * 2 + 1, gde);
}
void update(int l, int r, int ind, int levo, int desno, int val){
propagate(l,r,ind);
if(l >= levo && r <= desno){
lazy[ind]+=val;
propagate(l,r,ind);
return;
}
if(desno < l || r < levo)return;
int mid = (l + r) / 2;
update(l,mid,ind * 2,levo,desno,val);
update(mid + 1,r,ind * 2 + 1,levo,desno,val);
propagate(l,mid,ind*2);
propagate(mid+1,r,ind*2+1);
seg[ind] = seg[ind*2] + seg[ind*2+1];
propagate(l,r,ind);
}
long long count_swaps(std::vector<int> s) {
n = s.size()/2;
for(int i=0; i<2*n; i++){
if(s[i] < 0)tike[abs(s[i])][0].pb(i);
else tike[s[i]][1].pb(i);
}
int l = 0;
int shift = 0;
init(0,2*n-1,1);
long long res = 0;
for(int i=0; i<n; i++){
while(bio[l])++l;
int koji = abs(s[l]);
int ind;
if(s[l] < 0)ind = 1;
else ind = 0;
int gde = tike[koji][ind][dokle[koji][ind]];
bio[gde] = 1;
dokle[koji][ind]++;
dokle[koji][ind^1]++;
int pom = query(0,2*n-1,1,gde);
//cout << pom << " " << gde << endl;
update(0,2*n-1,1,0,gde - 1,1);
//bio[l + 1] = 1;
res += (pom - 2*(i) - 1);
res += 1-ind;
l ++;
}
return res;
}
Compilation message (stderr)
shoes.cpp: In function 'void propagate(int, int, int)':
shoes.cpp:28:13: warning: unused variable 'mid' [-Wunused-variable]
int mid = (l + r) / 2;
^~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:66:9: warning: unused variable 'shift' [-Wunused-variable]
int shift = 0;
^~~~~
# | 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... |