#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void dualPivotQuickSortHelper(vector<int>& nums, int left, int right, long long& comp_count, long long& move_count) {
    if (left < right) {
        comp_count++;
        if (nums[left] > nums[right]) {
            swap(nums[left], nums[right]);
            move_count += 3;
        }
        
        int p = nums[left], q = nums[right];
        int l = left + 1, g = right - 1, k = l;

        while (k <= g) {
            comp_count++;
            if (nums[k] < p) {
                swap(nums[k], nums[l]);
                move_count += 3;
                l++;
            } else {
                comp_count++;
                if (nums[k] > q) {
                    while (k < g && (comp_count++, nums[g] > q)) {
                        g--;
                    }
                    swap(nums[k], nums[g]);
                    move_count += 3;
                    g--;
                    comp_count++;
                    if (nums[k] < p) {
                        swap(nums[k], nums[l]);
                        move_count += 3;
                        l++;
                    }
                }
            }
            k++;
        }
        l--;
        g++;

        swap(nums[left], nums[l]);
        move_count += 3;
        swap(nums[right], nums[g]);
        move_count += 3;

        dualPivotQuickSortHelper(nums, left, l - 1, comp_count, move_count);
        dualPivotQuickSortHelper(nums, l + 1, g - 1, comp_count, move_count);
        dualPivotQuickSortHelper(nums, g + 1, right, comp_count, move_count);
    }
}

/*
 * function   : implement dual pivot quick sort
 * param nums : the vector to be sorted
 * param comp_count : count of comparisons
 * param move_count : count of moves
 * return     : ---
 */
void DualPivotQuickSort(vector<int>& nums, long long& comp_count, long long& move_count) {
    if (nums.empty()) return;
    dualPivotQuickSortHelper(nums, 0, nums.size() - 1, comp_count, move_count);
}
