From a95b53b39705019269be124e7f737884574457d2 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 16 Apr 2025 13:38:48 -0400 Subject: [PATCH] feat(codeforces/944): a-g --- codeforces/944/compile_flags.txt | 13 ++- codeforces/944/debug_flags.txt | 12 +++ codeforces/944/f.cc | 71 ++++++++++++++ codeforces/944/g.cc | 158 +++++++++++++++++++++++++++++++ codeforces/944/{ => io}/a.in | 0 codeforces/944/{ => io}/a.out | 3 +- codeforces/944/{ => io}/b.in | 0 codeforces/944/{ => io}/b.out | 0 codeforces/944/{ => io}/c.in | 0 codeforces/944/{ => io}/c.out | 0 codeforces/944/{ => io}/d.in | 0 codeforces/944/{ => io}/d.out | 0 codeforces/944/{ => io}/e.in | 0 codeforces/944/{ => io}/e.out | 2 +- codeforces/944/io/f.in | 8 ++ codeforces/944/io/f.out | 9 ++ codeforces/944/io/g.in | 9 ++ codeforces/944/io/g.out | 7 ++ codeforces/944/makefile | 89 +++-------------- codeforces/944/scripts/debug.sh | 29 ++++++ codeforces/944/scripts/run.sh | 29 ++++++ codeforces/944/scripts/utils.sh | 53 +++++++++++ 22 files changed, 409 insertions(+), 83 deletions(-) create mode 100644 codeforces/944/debug_flags.txt create mode 100644 codeforces/944/f.cc create mode 100644 codeforces/944/g.cc rename codeforces/944/{ => io}/a.in (100%) rename codeforces/944/{ => io}/a.out (59%) rename codeforces/944/{ => io}/b.in (100%) rename codeforces/944/{ => io}/b.out (100%) rename codeforces/944/{ => io}/c.in (100%) rename codeforces/944/{ => io}/c.out (100%) rename codeforces/944/{ => io}/d.in (100%) rename codeforces/944/{ => io}/d.out (100%) rename codeforces/944/{ => io}/e.in (100%) rename codeforces/944/{ => io}/e.out (77%) create mode 100644 codeforces/944/io/f.in create mode 100644 codeforces/944/io/f.out create mode 100644 codeforces/944/io/g.in create mode 100644 codeforces/944/io/g.out create mode 100644 codeforces/944/scripts/debug.sh create mode 100644 codeforces/944/scripts/run.sh create mode 100644 codeforces/944/scripts/utils.sh diff --git a/codeforces/944/compile_flags.txt b/codeforces/944/compile_flags.txt index 6d76ca1..af7fb72 100644 --- a/codeforces/944/compile_flags.txt +++ b/codeforces/944/compile_flags.txt @@ -1,13 +1,20 @@ --std=c++20 +-O2 -Wall -Wextra +-Wpedantic -Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 -Wnon-virtual-dtor -Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion -Wcast-align -Wunused -Woverloaded-virtual --Wpedantic -Wconversion -Wsign-conversion -Wmisleading-indentation @@ -15,10 +22,10 @@ -Wduplicated-branches -Wlogical-op -Wnull-dereference --Wuseless-cast -Wformat=2 -Wformat-overflow -Wformat-truncation -Wdouble-promotion -Wundef -DLOCAL +-std=c++20 diff --git a/codeforces/944/debug_flags.txt b/codeforces/944/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/codeforces/944/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/codeforces/944/f.cc b/codeforces/944/f.cc new file mode 100644 index 0000000..f709bfe --- /dev/null +++ b/codeforces/944/f.cc @@ -0,0 +1,71 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + u32 r; + cin >> r; + u64 ans = 0; + + for (u64 y = 1; y <= r; ++y) { + i64 hi_x, lo_x; + + { + i64 l = 0, h = r; + while (l <= h) { + i64 m = l + (h - l) / 2; + if (m * m + y * y < (u64)r * r) { + l = m + 1; + } else { + h = m - 1; + } + } + lo_x = l; + } + + { + i64 l = 0, h = r; + while (l <= h) { + i64 m = l + (h - l) / 2; + if (m * m + y * y < ((u64)r + 1) * (r + 1)) { + l = m + 1; + } else { + h = m - 1; + } + } + hi_x = h; + } + + ans += (hi_x - lo_x + 1) * 4; + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/944/g.cc b/codeforces/944/g.cc new file mode 100644 index 0000000..ca328c5 --- /dev/null +++ b/codeforces/944/g.cc @@ -0,0 +1,158 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multitree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (auto &e : a) + cin >> e; + hashtable> mask_to_values, mask_to_indices; + + for (size_t i = 0; i < n; ++i) { + u64 mask = a[i] >> 2; + mask_to_values[mask].emplace_back(a[i]); + mask_to_indices[mask].emplace_back(i); + } + + vector ans(n); + for (auto it = mask_to_values.begin(); it != mask_to_values.end(); ++it) { + sort(it->second.begin(), it->second.end()); + sort(mask_to_indices[it->first].begin(), mask_to_indices[it->first].end()); + for (u32 i = 0; i < mask_to_indices[it->first].size(); ++i) { + ans[mask_to_indices[it->first][i]] = it->second[i]; + } + } + + for (auto e : ans) + cout << e << ' '; + cout << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/944/a.in b/codeforces/944/io/a.in similarity index 100% rename from codeforces/944/a.in rename to codeforces/944/io/a.in diff --git a/codeforces/944/a.out b/codeforces/944/io/a.out similarity index 59% rename from codeforces/944/a.out rename to codeforces/944/io/a.out index 23a96c7..f562325 100644 --- a/codeforces/944/a.out +++ b/codeforces/944/io/a.out @@ -9,4 +9,5 @@ 0 0 9 9 -[code]: 0 \ No newline at end of file +[code]: 0 +[time]: 11.0121 ms \ No newline at end of file diff --git a/codeforces/944/b.in b/codeforces/944/io/b.in similarity index 100% rename from codeforces/944/b.in rename to codeforces/944/io/b.in diff --git a/codeforces/944/b.out b/codeforces/944/io/b.out similarity index 100% rename from codeforces/944/b.out rename to codeforces/944/io/b.out diff --git a/codeforces/944/c.in b/codeforces/944/io/c.in similarity index 100% rename from codeforces/944/c.in rename to codeforces/944/io/c.in diff --git a/codeforces/944/c.out b/codeforces/944/io/c.out similarity index 100% rename from codeforces/944/c.out rename to codeforces/944/io/c.out diff --git a/codeforces/944/d.in b/codeforces/944/io/d.in similarity index 100% rename from codeforces/944/d.in rename to codeforces/944/io/d.in diff --git a/codeforces/944/d.out b/codeforces/944/io/d.out similarity index 100% rename from codeforces/944/d.out rename to codeforces/944/io/d.out diff --git a/codeforces/944/e.in b/codeforces/944/io/e.in similarity index 100% rename from codeforces/944/e.in rename to codeforces/944/io/e.in diff --git a/codeforces/944/e.out b/codeforces/944/io/e.out similarity index 77% rename from codeforces/944/e.out rename to codeforces/944/io/e.out index 634f462..1124a0c 100644 --- a/codeforces/944/e.out +++ b/codeforces/944/io/e.out @@ -3,4 +3,4 @@ 99999999 1 5 4 -[code]: 0 \ No newline at end of file +[code]: 0 diff --git a/codeforces/944/io/f.in b/codeforces/944/io/f.in new file mode 100644 index 0000000..48b151b --- /dev/null +++ b/codeforces/944/io/f.in @@ -0,0 +1,8 @@ +6 +1 +2 +3 +4 +5 +1984 + diff --git a/codeforces/944/io/f.out b/codeforces/944/io/f.out new file mode 100644 index 0000000..e8efd9f --- /dev/null +++ b/codeforces/944/io/f.out @@ -0,0 +1,9 @@ +8 +16 +20 +24 +40 +12504 + +[code]: 0 +[time]: 3.93558 ms \ No newline at end of file diff --git a/codeforces/944/io/g.in b/codeforces/944/io/g.in new file mode 100644 index 0000000..b6c4589 --- /dev/null +++ b/codeforces/944/io/g.in @@ -0,0 +1,9 @@ +4 +4 +1 0 3 2 +5 +2 7 1 5 6 +8 +1 2 1 2 1 2 1 2 +4 +16 4 1 64 diff --git a/codeforces/944/io/g.out b/codeforces/944/io/g.out new file mode 100644 index 0000000..098d30e --- /dev/null +++ b/codeforces/944/io/g.out @@ -0,0 +1,7 @@ +0 1 2 3 +1 5 2 6 7 +1 1 1 1 2 2 2 2 +16 4 1 64 + +[code]: 0 +[time]: 12.9223 ms diff --git a/codeforces/944/makefile b/codeforces/944/makefile index e454976..e1e4b68 100644 --- a/codeforces/944/makefile +++ b/codeforces/944/makefile @@ -1,92 +1,25 @@ .PHONY: run debug clean setup init -CXX = g++ -# lefticus.gitbooks.io/cpp-best-practice -CXXFLAGS := -O2 -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 \ - -Wfloat-equal -Wlogical-op -Wshift-overflow=2 -Wnon-virtual-dtor \ - -Wold-style-cast -Wcast-equal -Wuseless-cast -Wno-sign-promotion \ - -Wcast-align -Wunused -Woverloaded-virtual -Wconversion \ - -Wsign-conversion -Wmisleading-indentation -Wduplicated-cond \ - -Wduplicated-branches -Wlogical-op -Wnull-dereference -Wformat=2 \ - -Wformat-overflow -Wformat-truncation -Wdouble-promotion -Wundef \ - -DLOCAL -std=c++20 - -# https://interrupt.memfault.com/blog/best-and-worst-gcc-clang-compiler-flags -DEBUG_CXX_FLAGS := -g3 -fsanitize=address,undefined \ - -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow \ - -fno-sanitize-recover=all -fstack-protector-all -fstack-usage \ - -fno-omit-frame-pointer -fno-inline -ffunction-sections - -DEBUG_CXXFLAGS += -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC - SRC = $(word 2,$(MAKECMDGOALS)) -BASE = $(basename $(SRC)) -INPUT = $(BASE).in -OUTPUT = $(BASE).out -RUN_BIN = $(BASE).run -DBG_BIN = $(BASE).debug - -TEMPLATE_DIR = ~/.config/cp-template .SILENT: -run: $(RUN_BIN) - $(call execute_binary,$(RUN_BIN)) +run: + sh scripts/run.sh $(SRC) -debug: $(DBG_BIN) - $(call execute_binary,$(DBG_BIN)) - -$(RUN_BIN): $(SRC) - test -f $@ && rm $@ || true - $(CXX) @compile_flags.txt $(SRC) -o $@ 2>$(OUTPUT); \ - CODE=$$?; \ - if [ $${CODE} -gt 0 ]; then \ - printf '\n[code]: %s' $${CODE} >>$(OUTPUT); \ - exit $${CODE}; \ - else \ - echo '' >$(OUTPUT); \ - fi - -$(DBG_BIN): $(SRC) - test -f $@ && rm $@ || true - $(CXX) @compile_flags.txt $(DEBUG_CXX_FLAGS) $(SRC) -o $@ 2>$(OUTPUT); \ - CODE=$$?; \ - if [ $${CODE} -gt 0 ]; then \ - printf '\n[code]: %s' $${CODE} >>$(OUTPUT); \ - exit $${CODE}; \ - else \ - echo '' >$(OUTPUT); \ - fi - -define execute_binary - timeout 2s ./$1 < $(INPUT) >$(OUTPUT) 2>&1; \ - CODE=$$?; \ - truncate -s $$(head -n 1000 $(OUTPUT) | wc -c) $(OUTPUT); \ - if [ $${CODE} -ge 124 ]; then \ - MSG=''; \ - case $${CODE} in \ - 124) MSG='TIMEOUT';; \ - 128) MSG='SIGILL';; \ - 130) MSG='SIGABRT';; \ - 131) MSG='SIGBUS';; \ - 136) MSG='SIGFPE';; \ - 135) MSG='SIGSEGV';; \ - 137) MSG='SIGPIPE';; \ - 139) MSG='SIGTERM';; \ - esac; \ - [ -n "$${MSG}" ] && printf '\n[code]: %s (%s)' "$${CODE}" "$${MSG}" >>$(OUTPUT); \ - else \ - printf '\n[code]: %s' $${CODE} >>$(OUTPUT); \ - fi -endef +debug: + sh scripts/debug.sh $(SRC) clean: - find . -type f -name "*.run" -o -name "*.debug" -o -name "*.su" | xargs rm -f + rm -rf build/* setup: - test -f compile_flags.txt || cp $(TEMPLATE_DIR)/compile_flags.txt . - test -f .clangd || cp $(TEMPLATE_DIR)/.clangd . - test -f .clang-format || cp $(TEMPLATE_DIR)/.clang-format . + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . init: make setup diff --git a/codeforces/944/scripts/debug.sh b/codeforces/944/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/944/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/944/scripts/run.sh b/codeforces/944/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/944/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/944/scripts/utils.sh b/codeforces/944/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/944/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +}