Template

template.cpp

// #define USACO cow
#include <bits/stdc++.h>
using namespace std;

#define STR_(x) #x
#define STR(x) STR_(x)

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define elif else if
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll INF = LLONG_MAX - 10;

ll max(int x, ll y) { return std::max((ll) x, y); }
ll max(ll y, int x) { return std::max((ll) x, y); }
ll min(int x, ll y) { return std::min((ll) x, y); }
ll min(ll y, int x) { return std::min((ll) x, y); }

struct Coordinate
{
    ll x, y;

    Coordinate() = default;
    Coordinate(ll x, ll y) : x(x), y(y) {}

    // Lexicographic comparison (x, then y), identical to std::pair
    bool operator==(const Coordinate& o) const { return x == o.x && y == o.y; }
    bool operator!=(const Coordinate& o) const { return !(*this == o); }
    bool operator< (const Coordinate& o) const { return x < o.x || (x == o.x && y < o.y); }
    bool operator> (const Coordinate& o) const { return o < *this; }
    bool operator<=(const Coordinate& o) const { return !(o < *this); }
    bool operator>=(const Coordinate& o) const { return !(*this < o); }

    struct CompareByX
    {
        bool operator()(const Coordinate& a, const Coordinate& b) const
        {
            return a.x < b.x || (a.x == b.x && a.y < b.y);
        }
        bool operator()(const Coordinate* a, const Coordinate* b) const
        {
            return (*this)(*a, *b);
        }
    };

    struct CompareByY
    {
        bool operator()(const Coordinate& a, const Coordinate& b) const
        {
            return a.y < b.y || (a.y == b.y && a.x < b.x);
        }
        bool operator()(const Coordinate* a, const Coordinate* b) const
        {
            return (*this)(*a, *b);
        }
    };

    template <class Cmp>
    struct Reversed
    {
        template <class T>
        bool operator()(const T& a, const T& b) const
        {
            return Cmp()(b, a);
        }
    };

    using GreaterByX = Reversed<CompareByX>;
    using GreaterByY = Reversed<CompareByY>;
};
 
struct Rect
{
    ll x1, y1, x2, y2;
 
    friend ostream& operator<<(ostream& os, const Rect& rect)
    {
        return os << rect.x1 << ' ' << rect.y1 << ' ' << rect.x2 << ' ' << rect.y2;
    }
 
    friend istream& operator>>(istream& is, Rect& rect)
    {
        return is >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2;
    }
};
 
template<typename Grid>
void cartesianPrint(const Grid& grid, int rows, int cols)
{
    if (rows < 0 || cols < 0)
    {
        throw invalid_argument("rows and cols must be non-negative");
    }

    vector<int> colWidths(cols);

    for (int c = 0; c < cols; ++c)
    {
        colWidths[c] = static_cast<int>(to_string(c).size());

        for (int r = 0; r < rows; ++r)
        {
            ll value = grid[r][c];
            int width = static_cast<int>(to_string(value).size());
            colWidths[c] = max(colWidths[c], width);
        }
    }

    int rowLabelWidth = max(3, static_cast<int>(to_string(rows - 1).size()));

    // ---- Column labels (TOP) ----
    cerr << string(rowLabelWidth, ' ') << "   ";

    for (int c = 0; c < cols; ++c)
    {
        cerr << setw(colWidths[c]) << c;

        if (c < cols - 1)
        {
            cerr << ' ';
        }
    }

    cerr << '\n';

    cerr << string(rowLabelWidth, ' ') << " +";

    for (int c = 0; c < cols; ++c)
    {
        cerr << string(colWidths[c] + 1, '-');
    }

    cerr << '\n';

    // ---- Rows ----
    for (int r = 0; r < rows; ++r)
    {
        cerr << setw(rowLabelWidth) << r << " | ";

        for (int c = 0; c < cols; ++c)
        {
            cerr << setw(colWidths[c]) << grid[r][c];

            if (c < cols - 1)
            {
                cerr << ' ';
            }
        }

        cerr << '\n';
    }
}
 
const vector<pair<int, int>> dirs = {
    {1, 0}, {0, 1}, {-1, 0}, {0, -1},
    {1, 1}, {-1, 1}, {-1, -1}, {1, -1}
};
const vector<pair<int, int>> dirs4 = {
    {1, 0}, {0, 1}, {-1, 0}, {0, -1}
};
const int MOD = 1000000007;
constexpr double epsilon = 0.00001;

class UnionFind
{
private:
    std::vector<int> parent;
    std::vector<int> size;
    int components;

public:
    explicit UnionFind(int n)
        : parent(n), size(n, 1), components(n)
    {
        std::iota(parent.begin(), parent.end(), 0);
    }

    // Path halving: each node on the path is redirected to its grandparent.
    int find(int x)
    {
        while (parent[x] != x)
        {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    // Union by size: the smaller tree is attached under the larger tree.
    // Returns true if a merge occurred, false if x and y were already connected.
    bool unionSets(int x, int y)
    {
        int rootX = find(x);
        int rootY = find(y);

        if (rootX == rootY)
            return false;

        if (size[rootX] < size[rootY])
            std::swap(rootX, rootY);

        parent[rootY] = rootX;
        size[rootX] += size[rootY];
        --components;
        return true;
    }

    bool connected(int x, int y)
    {
        return find(x) == find(y);
    }

    int numSets() const
    {
        return components;
    }

    int setSize(int x)
    {
        return size[find(x)];
    }
};

namespace FastIO {
    const int BUFSZ = 1 << 20;            // 1 MB 缓冲区
    char ibuf[BUFSZ], *ip = ibuf, *iend = ibuf;
    char obuf[BUFSZ];
    int op = 0;

    // ================= 输入部分 =================

    // 读取一个字节;读到文件末尾时返回 EOF
    inline int gc() {
        if (ip == iend) {
            iend = ibuf + fread(ibuf, 1, BUFSZ, stdin);
            ip = ibuf;
            if (ip == iend) return EOF;
        }
        return (unsigned char)*ip++;      // 转为 unsigned,避免 0xFF 与 EOF 混淆
    }

    // 读取整数(int / long long / __int128 均可),成功返回 true
    template <typename T>
    inline bool read(T &x) {
        int c = gc();
        while (c != '-' && (c < '0' || c > '9')) {
            if (c == EOF) return false;
            c = gc();
        }
        bool neg = false;
        if (c == '-') { neg = true; c = gc(); }
        x = 0;
        if (neg) for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 - (c - '0');
        else     for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + (c - '0');
        // 负数按负方向累加,因此 LLONG_MIN 等边界值也不会溢出
        return true;
    }

    // 一次读取多个整数:read(a, b, c);
    template <typename T, typename... Args>
    inline bool read(T &x, Args &...args) {
        return read(x) && read(args...);
    }

    // 读取一个非空白字符(跳过空格、换行等)
    inline char readChar() {
        int c = gc();
        while (c != EOF && c <= ' ') c = gc();
        return (char)c;
    }

    // 读取一个单词到 char 数组,返回长度;s 从下标 0 开始存储
    inline int readStr(char *s) {
        int c = gc(), n = 0;
        while (c != EOF && c <= ' ') c = gc();
        while (c != EOF && c > ' ') { s[n++] = (char)c; c = gc(); }
        s[n] = '\0';
        return n;
    }

    // 读取一个单词到 std::string
    inline bool readStr(string &s) {
        s.clear();
        int c = gc();
        while (c != EOF && c <= ' ') c = gc();
        if (c == EOF) return false;
        while (c != EOF && c > ' ') { s.push_back((char)c); c = gc(); }
        return true;
    }

    // 读取一整行(可含空格),自动去掉 '\r',返回长度;到文件末尾且无内容时返回 -1
    inline int readLine(char *s) {
        int c = gc(), n = 0;
        if (c == EOF) return -1;
        while (c != EOF && c != '\n') {
            if (c != '\r') s[n++] = (char)c;
            c = gc();
        }
        s[n] = '\0';
        return n;
    }

    inline bool readLine(string &s) {
        s.clear();
        int c = gc();
        if (c == EOF) return false;
        while (c != EOF && c != '\n') {
            if (c != '\r') s.push_back((char)c);
            c = gc();
        }
        return true;
    }

    // 读取浮点数(简单实现,不支持科学计数法)
    inline bool readDouble(double &x) {
        int c = gc();
        while (c != '-' && c != '.' && (c < '0' || c > '9')) {
            if (c == EOF) return false;
            c = gc();
        }
        bool neg = false;
        if (c == '-') { neg = true; c = gc(); }
        x = 0;
        for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + (c - '0');
        if (c == '.') {
            double base = 0.1;
            for (c = gc(); c >= '0' && c <= '9'; c = gc()) {
                x += (c - '0') * base;
                base *= 0.1;
            }
        }
        if (neg) x = -x;
        return true;
    }

    // ================= 输出部分 =================

    inline void flush() {
        fwrite(obuf, 1, op, stdout);
        op = 0;
    }

    inline void pc(char c) {
        if (op == BUFSZ) flush();
        obuf[op++] = c;
    }

    // 输出整数(int / long long / __int128 均可)
    template <typename T>
    inline void write(T x) {
        char s[45];
        int n = 0;
        if (x < 0) {
            pc('-');
            do { s[n++] = (char)('0' - x % 10); x /= 10; } while (x);  // 负方向处理,避免溢出
        } else {
            do { s[n++] = (char)('0' + x % 10); x /= 10; } while (x);
        }
        while (n) pc(s[--n]);
    }

    inline void write(const char *s) { while (*s) pc(*s++); }
    inline void write(const string &s) { for (char c : s) pc(c); }
    inline void write(char c) { pc(c); }

    // 输出整数并跟随一个分隔符:writeln(x) 换行,writesp(x) 空格
    template <typename T> inline void writeln(T x) { write(x); pc('\n'); }
    template <typename T> inline void writesp(T x) { write(x); pc(' '); }

    // 程序正常结束时自动 flush
    struct AutoFlush { ~AutoFlush() { flush(); } } autoFlush;
}
using namespace FastIO;


int main()
{
    void solve();

    #if defined(USACO) && !defined(LOCAL)
        freopen(STR(USACO) ".in", "r", stdin);
        freopen(STR(USACO) ".out", "w", stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}
/*------------------------------------------------------------------------*/



void solve()
{
    
}

Makefile

CXX      = g++
CXXFLAGS = -O2 -DLOCAL

release: main

main: main.cpp
	$(CXX) $(CXXFLAGS) main.cpp -o main

# Debug info, no optimization (for gdb); separate binary so main stays release.
debug: main_debug

main_debug: main.cpp
	$(CXX) -g -DLOCAL main.cpp -o main_debug

# Run every X.in that has a matching X.out, then print X-pass / X-fail.
run: main
	@tmp=$$(mktemp); found=0; \
	for in in $$(ls *.in 2>/dev/null | sort -V); do \
		t=$${in%.in}; [ -f "$$t.out" ] || continue; found=1; \
		if (./main < "$$in" > "$$tmp"; exit $$?) 2>/dev/null && diff -bBq "$$tmp" "$$t.out" >/dev/null; \
		then printf '\033[32m%s-pass\033[0m\n' "$$t"; \
		else printf '\033[31m%s-fail\033[0m\n' "$$t"; fi; \
	done; \
	rm -f "$$tmp"; \
	[ $$found = 1 ] || echo "no .in/.out pairs found"

# Copy main.cpp to the system clipboard (macOS, Linux, Windows via Git Bash/WSL).
submit:
	@if command -v pbcopy >/dev/null 2>&1; then \
		pbcopy < main.cpp; \
	elif [ -n "$$WAYLAND_DISPLAY" ] && command -v wl-copy >/dev/null 2>&1; then \
		wl-copy < main.cpp; \
	elif command -v xclip >/dev/null 2>&1; then \
		xclip -selection clipboard < main.cpp >/dev/null 2>&1; \
	elif command -v xsel >/dev/null 2>&1; then \
		xsel --clipboard --input < main.cpp; \
	elif command -v clip.exe >/dev/null 2>&1; then \
		clip.exe < main.cpp; \
	elif command -v clip >/dev/null 2>&1; then \
		clip < main.cpp; \
	else \
		echo "No clipboard tool found (install xclip, xsel, or wl-clipboard on Linux)."; \
		exit 1; \
	fi; \
	echo "main.cpp copied to clipboard"

# Remove build outputs (.exe for Windows, .dSYM bundles from macOS clang -g).
clean:
	rm -rf main main_debug *.exe *.o *.dSYM

.PHONY: release debug run submit clean