Skip to content

Commit 70e0e6b

Browse files
committed
template.cpp: Automatically dbg inputs. Add vecs, vec1 input shortcuts.
1 parent 9fb1a72 commit 70e0e6b

File tree

1 file changed

+83
-54
lines changed

1 file changed

+83
-54
lines changed

templates/template.cpp

+83-54
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ auto stepped_iota(ll start, ll end, ll step=1) {
267267
// just as an example of how to iterate in reverse order
268268
// for(auto& x : dat | views::reverse) {}
269269

270-
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
271-
#define FOR(...) GET_MACRO(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
270+
#define GET_MACRO_5(_1,_2,_3,_4,_5,NAME,...) NAME
271+
#define FOR(...) GET_MACRO_5(__VA_ARGS__, , FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
272272

273273
#pragma region // rangeint
274274
V<int> rangeint(int start, int end, int inc=1) {
@@ -388,47 +388,6 @@ inline namespace Helpers {
388388
void decrement(auto& ...args) {(increment_one(args, -1), ...);} // subtract one from each, for input. MODIFICATION IS THE GOAL
389389
}
390390

391-
inline namespace Input {
392-
tcT> constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
393-
394-
// re: read
395-
void re(auto &...args);
396-
tcTU> void re(pair<T,U>& p) { re(p.first,p.second); } // pairs
397-
tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; } // complex
398-
tcT> typename enable_if<needs_input_v<T>,void>::type re(T& i); // ex. vectors, arrays
399-
400-
template <class... Ts> void re(tuple<Ts...> &t) {
401-
apply([](Ts &...args) { re(args...); }, t);
402-
}
403-
404-
tcT> typename enable_if<needs_input_v<T>,void>::type re(T& i) {
405-
each(x,i) re(x); }
406-
void re(auto& ...args) { ((cin >> args), ...); } // read multiple
407-
408-
// rv: resize and read vectors
409-
void rv(size_t) {}
410-
tcTUU> void rv(size_t N, V<T>& t, U&... u);
411-
template<class...U> void rv(size_t, size_t N2, U&... u);
412-
tcTUU> void rv(size_t N, V<T>& t, U&... u) {
413-
t.resize(N); re(t);
414-
rv(N,u...); }
415-
template<class...U> void rv(size_t, size_t N2, U&... u) {
416-
rv(N2,u...); }
417-
void rv1(size_t) {}
418-
tcTUU> void rv1(size_t N, V<T>& t, U&... u) {
419-
t.resize(N); re(t); for(auto& x : t) decrement(x);
420-
rv1(N,u...); }
421-
422-
423-
// dumb shortcuts to read in ints
424-
#define ints(...) int __VA_ARGS__; re(__VA_ARGS__);
425-
#define int1(...) ints(__VA_ARGS__); decrement(__VA_ARGS__);
426-
#define chars(...) char __VA_ARGS__; re(__VA_ARGS__);
427-
#define lls(...) ll __VA_ARGS__; re(__VA_ARGS__);
428-
#define ll1(...) lls(__VA_ARGS__); decrement(__VA_ARGS__);
429-
#define strings(...) string __VA_ARGS__; re(__VA_ARGS__);
430-
}
431-
432391
inline namespace ToString {
433392
tcT> constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;
434393

@@ -545,8 +504,8 @@ inline namespace FileIO {
545504
}
546505

547506
#pragma region // debugging
548-
// * debug setup mostly stolen from tourist. https://codeforces.com/contest/1540/submission/120602670
549-
// * dcclyde added line numbers, colors, probably some other stuff.
507+
// * debug setup adapted from from tourist. https://codeforces.com/contest/1540/submission/120602670
508+
// * dcclyde added line numbers, colors, and some other stuff.
550509

551510
// for dbg printouts of C arrays.
552511
template<typename T>
@@ -777,15 +736,19 @@ void debug_out(Head H, Tail... T) {
777736
#define OUT_RESET "\033[0m"
778737
#define OUT_BOLD "\033[;1m"
779738
#define OUT_RED "\033[31" "m"
780-
#define OUT_CYAN "\033[36" BOLD_MAYBE "m"
781-
#define OUT_DEFAULT OUT_CYAN
782-
// #define OUT_GREEN "\033[32" << BOLD_MAYBE << "m"
783739
#define OUT_GREEN "\033[32" "m"
740+
// #define OUT_GREEN "\033[32" << BOLD_MAYBE << "m"
741+
#define OUT_YELLOW "\033[33" BOLD_MAYBE "m"
784742
#define OUT_BLUE "\033[34" BOLD_MAYBE "m"
743+
#define OUT_PURPLE "\033[35" BOLD_MAYBE "m"
744+
#define OUT_CYAN "\033[36" BOLD_MAYBE "m"
745+
#define OUT_BRIGHTBLUE "\033[38;5;39m"
785746
#define OUT_WHITE "\033[97" "m"
786747
#define OUT_MARK "\033[0;30;41;1m"
787-
#define OUT_YELLOW "\033[33" BOLD_MAYBE "m"
788-
#define OUT_PURPLE "\033[35" BOLD_MAYBE "m"
748+
#define OUT_LTBLUE "\033[96m"
749+
750+
#define OUT_DEFAULT OUT_CYAN
751+
#define OUT_INPUTCOLOR OUT_WHITE
789752

790753

791754
#define dbgc(...) ;
@@ -803,6 +766,8 @@ void debug_out(Head H, Tail... T) {
803766
#define dbgcB(...) ;
804767
#define dbgW(...) ;
805768
#define dbgcW(...) ;
769+
#define dbgInput(...) ;
770+
#define dbgcInput(...) ;
806771
#define dbgGeneral(...) ;
807772
#define dbgcGeneral(...) ;
808773
#define dbg_only(...) ;
@@ -855,6 +820,11 @@ void debug_out(Head H, Tail... T) {
855820
#undef dbgcW
856821
#define dbgcW(MSG, ...) dbgcbase(OUT_GREEN, OUT_WHITE, MSG, #__VA_ARGS__, __VA_ARGS__)
857822

823+
#undef dbgInput
824+
#define dbgInput(...) dbgcbase(OUT_GREEN, OUT_INPUTCOLOR, "", #__VA_ARGS__, __VA_ARGS__)
825+
#undef dbgcInput
826+
#define dbgcInput(MSG, ...) dbgcbase(OUT_GREEN, OUT_INPUTCOLOR, MSG, #__VA_ARGS__, __VA_ARGS__)
827+
858828
#undef dbgGeneral
859829
#define dbgGeneral(COLOR, ...) dbgcbase(OUT_GREEN, COLOR, "", #__VA_ARGS__, __VA_ARGS__)
860830
#undef dbgcGeneral
@@ -871,7 +841,68 @@ void debug_out(Head H, Tail... T) {
871841
#endif
872842
#pragma endregion
873843

874-
#define timebomb(a) dbg_only({static int _bomb = 0; if(++_bomb>=a) {dbgcBold("boom!", a);exit(1);}});
844+
inline namespace Input {
845+
tcT> constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
846+
847+
// re: read
848+
void re(auto &...args);
849+
tcTU> void re(pair<T,U>& p) { re(p.first,p.second); } // pairs
850+
tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; } // complex
851+
tcT> typename enable_if<needs_input_v<T>,void>::type re(T& i); // ex. vectors, arrays
852+
853+
template <class... Ts> void re(tuple<Ts...> &t) {
854+
apply([](Ts &...args) { re(args...); }, t);
855+
}
856+
857+
tcT> typename enable_if<needs_input_v<T>,void>::type re(T& i) {
858+
each(x,i) re(x); }
859+
void re(auto& ...args) { ((cin >> args), ...); } // read multiple
860+
861+
// rv: resize and read vectors
862+
void rv(size_t) {}
863+
tcTUU> void rv(size_t N, V<T>& t, U&... u);
864+
template<class...U> void rv(size_t, size_t N2, U&... u);
865+
tcTUU> void rv(size_t N, V<T>& t, U&... u) {
866+
t.resize(N); re(t);
867+
rv(N,u...); }
868+
template<class...U> void rv(size_t, size_t N2, U&... u) {
869+
rv(N2,u...); }
870+
void rv1(size_t) {}
871+
tcTUU> void rv1(size_t N, V<T>& t, U&... u) {
872+
t.resize(N); re(t); for(auto& x : t) decrement(x);
873+
rv1(N,u...); }
874+
875+
876+
// dumb shortcuts to read in ints
877+
#define readBase(TYPE, ...) TYPE __VA_ARGS__; re(__VA_ARGS__); dbgInput(__VA_ARGS__);
878+
#define readBase1(TYPE, ...) TYPE __VA_ARGS__; re(__VA_ARGS__); decrement(__VA_ARGS__); dbgInput(__VA_ARGS__);
879+
#define ints(...) readBase(int, __VA_ARGS__);
880+
#define int1(...) readBase1(int, __VA_ARGS__);
881+
#define chars(...) readBase(char, __VA_ARGS__);
882+
#define char1(...) readBase1(char, __VA_ARGS__);
883+
#define lls(...) readBase(ll, __VA_ARGS__);
884+
#define ll1(...) readBase1(ll, __VA_ARGS__);
885+
#define strings(...) readBase(string, __VA_ARGS__);
886+
887+
#define vecs_1(TYPE, SIZE, A) V<TYPE> A; rv(SIZE, A); dbgInput(SIZE, A);
888+
#define vecs_2(TYPE, SIZE, A, B) vecs_1(TYPE, SIZE, A); vecs_1(TYPE, SIZE, B);
889+
#define vecs_3(TYPE, SIZE, A, B, C) vecs_1(TYPE, SIZE, A); vecs_2(TYPE, SIZE, B, C);
890+
#define vecs_4(TYPE, SIZE, A, B, C, D) vecs_1(TYPE, SIZE, A); vecs_3(TYPE, SIZE, B, C, D);
891+
#define vecs_5(TYPE, SIZE, A, B, C, D, E) vecs_1(TYPE, SIZE, A); vecs_4(TYPE, SIZE, B, C, D, E);
892+
#define vec1_1(TYPE, SIZE, A) V<TYPE> A; rv1(SIZE, A); dbgInput(SIZE, A);
893+
#define vec1_2(TYPE, SIZE, A, B) vec1_1(TYPE, SIZE, A); vec1_1(TYPE, SIZE, B);
894+
#define vec1_3(TYPE, SIZE, A, B, C) vec1_1(TYPE, SIZE, A); vec1_2(TYPE, SIZE, B, C);
895+
#define vec1_4(TYPE, SIZE, A, B, C, D) vec1_1(TYPE, SIZE, A); vec1_3(TYPE, SIZE, B, C, D);
896+
#define vec1_5(TYPE, SIZE, A, B, C, D, E) vec1_1(TYPE, SIZE, A); vec1_4(TYPE, SIZE, B, C, D, E);
897+
898+
#define vecs(TYPE, SIZE, ...) GET_MACRO_5(__VA_ARGS__, vecs_5, vecs_4, vecs_3, vecs_2, vecs_1)(TYPE, SIZE, __VA_ARGS__);
899+
#define vec1(TYPE, SIZE, ...) GET_MACRO_5(__VA_ARGS__, vec1_5, vec1_4, vec1_3, vec1_2, vec1_1)(TYPE, SIZE, __VA_ARGS__);
900+
901+
// #define vecs(TYPE, SIZE, ...) V<TYPE> __VA_ARGS__; rv(SIZE, __VA_ARGS__); dbgInput(__VA_ARGS__);
902+
// #define vec1(TYPE, SIZE, ...) V<TYPE> __VA_ARGS__; rv1(SIZE, __VA_ARGS__); dbgInput(__VA_ARGS__);
903+
}
904+
905+
#define timebomb(a) dbg_only({static int _bomb=a;if(_bomb<=0) {dbgcBold("boom!", a);assert(false);exit(1);} dbgcR("timebomb", _bomb); --_bomb;});
875906

876907
#define YES ps("YES");
877908
#define NO ps("NO");
@@ -900,9 +931,7 @@ const int INF_i = 2'000'000'001; // 2e9 + 1
900931

901932
void solve() {
902933
lls(N);
903-
V<ll> dat;
904-
rv(N, dat);
905-
dbgR(N, dat);
934+
vecs(ll, N, A);
906935
el;
907936

908937
// ! Read the sample cases AND EXPLANATIONS before writing code for nontrivial problems!

0 commit comments

Comments
 (0)