Submission #540680

#TimeUsernameProblemLanguageResultExecution timeMemory
540680TurkhuuArranging Shoes (IOI19_shoes)C++17
100 / 100
156 ms139428 KiB
#include "shoes.h"
#include <bits/stdc++.h>
 
using namespace std;
 
//template by Geothermal
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define dbg(x...) cerr << " [" << #x << "] = ["; _print(x);
 
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef tuple<int, int, int> t3i;
typedef tuple<int, int, int, int> t4i;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<PI> VPI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<VL> VVL;
typedef vector<VS> VVS;
typedef vector<VC> VVC;
typedef vector<VPI> VVPI;
 
#define FOOR(i, a, b) for(int i = a; i <= b; i++)
#define ROOF(i, a, b) for(int i = a; i >= b; i--)
#define FORR(i, a, b) FOOR(i, a, b - 1)
#define ROFF(i, a, b) ROOF(i, a - 1, b)
#define FOR(i, a) FORR(i, 0, a)
#define ROF(i, a) ROFF(i, a, 0)
 
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define lla(a) (a).rbegin(), (a).rend()
#define bk back()
#define fr front()
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define LB lower_bound
#define UB upper_bound
#define MINE min_element
#define MAXE max_element
#define f first
#define s second
#define MP make_pair
#define MT make_tuple
 
template<typename T> using PQ = priority_queue<T>;
template<typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
 
template<typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, true : false; }
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, true : false; }
 
const vector<int> dx = {0, 0, -1, 1};
const vector<int> dy = {-1, 1, 0, 0};
const ll infll = 10000000000000000;
const int inf = 2000000000;
const int mod = 1000000007;
const int mod99 = 998244353;
 
template<typename T>
struct fenwick{
  int n;
  vector<T> bit;
  fenwick(int _n) : n(_n), bit(n, 0){}
  fenwick(vector<T> a, int _n) : fenwick(_n){
    for(int i = 0; i < n; i++){
      add(i, a[i]);
    }
  }
  void add(int i, T x){
    while(i < n){
      bit[i] += x;
      i |= (i + 1);
    }
  }
  T sum(int i){
    T s = 0;
    while(i >= 0){
      s += bit[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }
  T sum(int l, int r){
    return sum(r) - sum(l - 1);
  }
};
 
ll count_swaps(vector<int> a){
  int n = sz(a) / 2;
  vector<deque<int>> l(n + 1);
  vector<deque<int>> r(n + 1);
  FOR(i, 2 * n){
    (a[i] < 0 ? l : r)[abs(a[i])].pb(i);
  }
  ll ans = 0;
  fenwick<int> fw(2 * n);
  FOR(i, 2 * n){
    if(fw.sum(i, i) > 0){
      continue;
    }
    int k = abs(a[i]);
    int li = l[k].fr; l[k].ppf;
    int ri = r[k].fr; r[k].ppf;
    ans += abs(ri - li) - (a[i] < 0 ? 1 : 0) - fw.sum(min(li, ri), max(li, ri));
    fw.add(li, 1);
    fw.add(ri, 1);
  }
  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...