Submission #537331

#TimeUsernameProblemLanguageResultExecution timeMemory
537331ERHArranging Shoes (IOI19_shoes)C++14
50 / 100
31 ms16288 KiB
#include <iostream>
#include <vector>
using namespace std;
vector<int> lista[200001];
long long int temp[200001];
long long count_swaps(vector<int> v){
  long long int total=0, z=v.size();
  for(int i=1; i<200001; ++i){
    for(int j=i; j<200001; j+=j&-j){
      temp[j]++;
    }
  }
  for(int i=0; i<z; ++i){
    int a=v[i];
    if(a<0){
      if(!lista[-a+z].empty()){
        long long int temp1=0, temp2=0;
        for(int j=i+1; j>0; j-=j&-j){
          temp1+=temp[j];
        }
        temp1+=temp[0];
        for(int j=lista[-a+z].front()+1; j>0; j-=j&-j){
          temp2+=temp[j];
        }
        temp2+=temp[0];
        total+=temp1-temp2;
        for(int j=lista[-a+z].front()+1; j<200001; j+=j&-j){
          temp[j]++;
        }
        for(int j=i+1; j<200001; j+=j&-j){
          temp[j]--;
        }
        lista[-a+z].erase(lista[-a+z].begin());
      } else {
        lista[a+z].push_back(i);
      }
    } else {
      if(!lista[-a+z].empty()){
        long long int temp1=0, temp2=0;
        for(int j=i+1; j>0; j-=j&-j){
          temp1+=temp[j];
        }
        temp1+=temp[0];
        for(int j=lista[-a+z].front()+1; j>0; j-=j&-j){
          temp2+=temp[j];
        }
        temp2+=temp[0];
        total+=temp1-temp2;
        total--;
        for(int j=lista[-a+z].front()+1; j<200001; j+=j&-j){
          temp[j]++;
        }
        for(int j=i+1; j<200001; j+=j&-j){
          temp[j]--;
        }
        lista[-a+z].erase(lista[-a+z].begin());
      } else {
        lista[a+z].push_back(i);
      }
    }
  }
  return total;
}
#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...