Submission #143797

#TimeUsernameProblemLanguageResultExecution timeMemory
143797NightlightArranging Shoes (IOI19_shoes)C++14
100 / 100
223 ms137500 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

queue<int> pass[200005];
int tree[200005], MAXS;
long long ans;

void update(int u, int idx){
  idx++;
  for(int i = idx; i <= MAXS; i += i & -i){
    tree[i] += u;
  }
}

int getans(int idx){
  idx++;
  int res = 0;
  for(int i = idx; i > 0; i -= i & -i)
    res += tree[i];
  return res;
}

long long count_swaps(vector<int> s) {
  MAXS = s.size();
	for(int R = 0; R < MAXS; R++){
    if(!pass[100000-s[R]].empty()){
      int L = pass[100000-s[R]].front();
//      cout << R << " " << L << "\n";
      pass[100000-s[R]].pop();
      if(s[R] > 0){
        ans += R - L - 1; 
      }else ans += R - L;

      ans -= getans(R) - getans(L);
      update(-1, L);
    }else{
      pass[100000 + s[R]].push(R);
      update(1, R);
    }
  }
  return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...