feat(codeforces/944): a-g

This commit is contained in:
Barrett Ruth 2025-04-16 13:38:48 -04:00
parent c11932a0fb
commit a95b53b397
22 changed files with 409 additions and 83 deletions

View file

@ -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

View file

@ -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

71
codeforces/944/f.cc Normal file
View file

@ -0,0 +1,71 @@
#include <bits/stdc++.h> // {{{
// 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;
}
// }}}

158
codeforces/944/g.cc Normal file
View file

@ -0,0 +1,158 @@
#include <bits/stdc++.h> // {{{
// 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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
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 <class T, class D = void>
struct custom_hash {};
template <class T>
inline void hash_combine(u64 &seed, T const &v) {
custom_hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4);
};
template <class T>
struct custom_hash<T,
typename std::enable_if<std::is_integral<T>::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 <class T>
struct custom_hash<T, std::void_t<decltype(std::begin(std::declval<T>()))>> {
u64 operator()(T const &a) const {
u64 value = FIXED_RANDOM;
for (auto &x : a)
hash_combine(value, x);
return value;
}
};
template <class... T>
struct custom_hash<std::tuple<T...>> {
u64 operator()(const std::tuple<T...> &a) const {
u64 value = FIXED_RANDOM;
std::apply(
[&value](T const &...args) {
(hash_combine(value, args), ...);
},
a);
return value;
}
};
template <class T, class U>
struct custom_hash<std::pair<T, U>> {
u64 operator()(std::pair<T, U> 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 <class Key, class Value = null_type>
using hashtable = gp_hash_table<
Key, Value, hashing::custom_hash<Key>, std::equal_to<Key>,
direct_mask_range_hashing<>, linear_probe_fn<>,
hash_standard_resize_policy<hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>, true>>;
#endif
#ifdef PB_DS_TREE_POLICY_HPP
template <typename T>
using multitree = tree<T, null_type, std::less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
template <class Key, class Value = null_type>
using rbtree = tree<Key, Value, std::less<Key>, rb_tree_tag,
tree_order_statistics_node_update>;
#endif
void solve() {
u32 n;
cin >> n;
vector<u32> a(n);
for (auto &e : a)
cin >> e;
hashtable<u64, vector<u32>> 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<u32> 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;
}
// }}}

View file

@ -10,3 +10,4 @@
9 9
[code]: 0
[time]: 11.0121 ms

8
codeforces/944/io/f.in Normal file
View file

@ -0,0 +1,8 @@
6
1
2
3
4
5
1984

9
codeforces/944/io/f.out Normal file
View file

@ -0,0 +1,9 @@
8
16
20
24
40
12504
[code]: 0
[time]: 3.93558 ms

9
codeforces/944/io/g.in Normal file
View file

@ -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

7
codeforces/944/io/g.out Normal file
View file

@ -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

View file

@ -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

View file

@ -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 $?

View file

@ -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 $?

View file

@ -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
}