#include "triangles.h"
#include <vector>
#include <bits/stdc++.h>
#include <set>
using namespace std;

bool isIn(Triangle t, int h) {
    Triangle t3 = {t.i, t.j, t.k};
    Triangle t1 = {t.i, t.k, h};
    Triangle t0 = {t.j, t.k, h};
    Triangle t2 = {t.i, t.j, h};
    if (compare({t1, t2, t0}, {t3}) == 0) {
        return true;
    }
    return false;
}

bool isConv(Triangle t, int i) {
    Triangle t1 = {t.i, t.k, i};
    Triangle t2 = {t.j, t.k, i};
    Triangle t3 = {t.i, t.j, i};
    if (compare({t, t1}, {t2, t3}) == 0) {
        return true;
    }
    return false;
}

std::vector<std::vector<int>> solve(int n) {
    vector<int> hull = {0, 1, 2};
    vector<vector<int> > ans;
    set<vector<int> > z;
    for (int h = 3; h < n; h++) {
        int idx = -1;
        int last = -1;
        vector<int> used(n);
        for (int l = 0; l < hull.size(); l++) {
            if (hull.size() == 2) {
                break;
            }
            Triangle t = {hull[(l + hull.size() - 1) % hull.size()], h, hull[(l + 1) % hull.size()]};
            if (isIn(t, hull[l])) {
                if (idx == -1) {
                    idx = l;
                }
                last = l;
                used[l] = -1;
                ans.push_back({hull[(idx + hull.size() - 1) % hull.size()], hull[l], hull[(l + 1) % hull.size()]});
                ans.push_back({hull[l], h, hull[(l + 1) % hull.size()]});
            }
        }
        if (idx != -1) {
            ans.push_back({hull[(idx + hull.size() - 1) % hull.size()], h, hull[(idx) % hull.size()]});
            vector<int> hh = {hull[(idx - 1 + hull.size()) % hull.size()], h, hull[(last + 1) % hull.size()]};
            sort(hh.begin(), hh.end());
            z.insert(hh);
        }
        vector<int> hull1;
        idx = -1;
        last = -1;
        for (int i = 0; i < hull.size(); i++) {
            if (used[i] == -1) {
                last = i;
                if (idx == -1) {
                    hull1.push_back(h);
                    idx = i;
                }
            } else {
                hull1.push_back(hull[i]);
            }
        }
        swap(hull, hull1);
        if (idx == -1) {
            bool flag = true;
            for (int i = 2; i < hull.size(); i++) {
                Triangle t = {hull[0], hull[i - 1], hull[i]};
                if (isIn(t, h)) {
                    Triangle t1 = {t.i, t.k, h};
                    Triangle t2 = {t.j, t.k, h};
                    Triangle t3 = {t.i, t.j, h};
                    ans.push_back({t.i, t.k, h});
                    ans.push_back({t.j, t.k, h});
                    ans.push_back({t.i, t.j, h});
                    z.insert( {hull[0], hull[i - 1], hull[i]});
                    flag = false;
                    break;
                }
            }
            if (flag) {
                for (int i = 0; i < hull.size(); i++) {
                    flag = true;
                    Triangle t = {hull[i], hull[(i + 1) % hull.size()], h};
                    for (int j = 0; j < hull.size(); j++) {
                        if (j == i || j == (i + 1) % hull.size()) {
                            continue;
                        }
                        if (!isConv(t, hull[j])) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        idx = (i + 2) % hull.size();
                        break;
                    }
                }
            }
            hull1.clear();
            for (int i = 0; i < hull.size(); i++) {
                if (i == idx) {
                    hull1.push_back(h);
                }
                hull1.push_back(hull[i]);
            }
            swap(hull, hull1);
        }
    }
    for (int i = 2; i < hull.size(); i++) {
        ans.push_back({hull[0], hull[i - 1], hull[i]});
    }
    set<vector<int> > ans1;
    for (auto u : ans) {
        auto u1 = u;
        sort(u1.begin(), u1.end());
        if (z.count(u1) == 0) {
            ans1.insert(u);
        }
    }
    vector<vector<int> > ans2;
    for (auto i : ans1) {
        ans2.push_back(i);
    }
    return ans2;
}
