Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created July 4, 2014 16:59
Show Gist options
  • Save ramntry/ed5dac976fd55a92b368 to your computer and use it in GitHub Desktop.
Save ramntry/ed5dac976fd55a92b368 to your computer and use it in GitHub Desktop.
CF template
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <limits>
#include <vector>
#include <string>
#include <deque>
#include <set>
#include <map>
using namespace std;
#ifdef ONLINE_JUDGE
# define debug(...) (void)0
# define dump_vector(VECTOR) (void)0
# define LL_FORMAT "%I64d"
# define ULL_FORMAT "%I64u"
#else
# define debug(...) fprintf(stderr, __VA_ARGS__)
# define dump_vector(VECTOR) dump_named_vector(VECTOR, #VECTOR)
# define LL_FORMAT "%lld"
# define ULL_FORMAT "%llu"
#endif
typedef long long ll;
typedef unsigned long long ull;
#define DEFINE_MIN_MAX_CONSTANTS(TYPE) \
static int const TYPE##_max = numeric_limits<TYPE>::max(); \
static int const TYPE##_min = numeric_limits<TYPE>::min()
DEFINE_MIN_MAX_CONSTANTS(int);
DEFINE_MIN_MAX_CONSTANTS(ll);
DEFINE_MIN_MAX_CONSTANTS(ull);
DEFINE_MIN_MAX_CONSTANTS(double);
template <typename> struct stdio_format_for_type {};
#define DEFINE_STDIO_FORMAT_FOR_TYPE(TYPE, FORMAT) \
template <> struct stdio_format_for_type<TYPE> { static char const *const value; }; \
char const *const stdio_format_for_type<TYPE>::value = FORMAT
DEFINE_STDIO_FORMAT_FOR_TYPE(int, "%d");
DEFINE_STDIO_FORMAT_FOR_TYPE(ll, LL_FORMAT);
DEFINE_STDIO_FORMAT_FOR_TYPE(ull, ULL_FORMAT);
DEFINE_STDIO_FORMAT_FOR_TYPE(double, "%lf");
template <typename E>
int read_back_vector(vector<E> &out, int size = int_max, char const *element_format = stdio_format_for_type<E>::value) {
E elem = E();
int i = 0;
for (; i < size; ++i) {
if (scanf(element_format, &elem) != 1) {
break;
}
out.push_back(elem);
}
return i;
}
template <typename E>
void read_in_vector(vector<E> &out, int size, char const *element_format = stdio_format_for_type<E>::value) {
out.resize(size);
for (int i = 0; i < size; ++i) {
assert(scanf(element_format, &out[i]) == 1);
}
}
#define read(TYPE, NAME) \
TYPE NAME = TYPE(); \
assert(scanf(stdio_format_for_type<TYPE>::value, &NAME) == 1)
#define read2(TYPE, NAME, TYPE2, NAME2) \
read(TYPE, NAME); \
read(TYPE2, NAME2)
#define read_vector(TYPE, NAME, SIZE) \
vector<TYPE> NAME; \
read_in_vector(NAME, SIZE)
template <typename E>
void dump_named_vector(vector<E> const &v, char const *name) {
int const size = v.size();
debug("[%2d] %10s = { ", size, name);
if (size) {
debug(stdio_format_for_type<E>::value, v[0]);
}
for (int i = 1; i < size; ++i) {
debug(", ");
debug(stdio_format_for_type<E>::value, v[i]);
}
debug(" }\n");
}
bool print_preceding_space;
template <typename T>
void print(T const &item, FILE *out = stdout) {
if (print_preceding_space) {
fprintf(out, " ");
}
fprintf(out, stdio_format_for_type<T>::value, item);
print_preceding_space = true;
}
template <typename T>
void println(T const &item, FILE *out = stdout) {
print(item, out);
fprintf(out, "\n");
print_preceding_space = false;
}
#define ALL(COLLECTION) COLLECTION.begin(), COLLECTION.end()
int main() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment