more code

This commit is contained in:
Barrett Ruth 2025-01-30 17:06:38 -05:00
parent 7aacde99f4
commit 742bf851a9
149 changed files with 7065 additions and 0 deletions

View file

@ -0,0 +1,188 @@
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
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>
using hashmap = 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>>;
template <class Key>
using hashset = gp_hash_table<
Key, null_type, 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 multiset = 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
template <typename... Args> void print(std::string const &str, Args &&...args) {
std::cout << std::vformat(str,
std::make_format_args(std::forward<Args>(args)...));
}
template <typename T> void print(T const &t) { std::cout << t; }
template <std::ranges::range T> void print(T const &t) {
if constexpr (std::is_same_v<T, string> ||
std::is_convertible_v<T, char const *>) {
std::cout << t << '\n';
} else {
for (auto const &e : t) {
std::cout << e << ' ';
}
std::cout << '\n';
}
}
template <typename... Args>
void println(std::string const &str, Args &&...args) {
print(str, std::forward<Args>(args)...);
cout << '\n';
}
template <typename T> void println(T const &t) { print("{}\n", t); }
template <std::ranges::range T> void println(T const &t) { cout << t << '\n'; }
void println() { std::cout << '\n'; }
template <typename T> T MAX() { return std::numeric_limits<T>::max(); }
template <typename T> T MIN() { return std::numeric_limits<T>::min(); }
#define ff first
#define ss second
#define eb emplace_back
#define ll long long
#define ld long double
#define vec vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (r).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
#ifdef LOCAL
#define dbg(x) cout << __LINE__ << ": " << #x << "=<" << (x) << ">\n";
#else
#define dbg(x)
#endif
constexpr static int MOD = 1e9 + 7;
void solve() {
string s;
cin >> s;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == '0')
continue;
int j = i;
while (j >= 1 && s[j] - 1 > s[j - 1]) {
--s[j];
swap(s[j], s[-1]);
--j;
}
}
println(s);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}

29
codeforces/991/buf Normal file
View file

@ -0,0 +1,29 @@
void solve() {
stGCD.clear();
int n, q; cin >> n >> q;
vector<int> a(n);
for (int &x : a) cin >> x;
vector<int> b;
for (int i = 1; i < n; i++)
b.push_back(abs(a[i - 1] - a[i]));
stGCD.resize(LOGN, vector<int>(b.size(), 1));
for (int i = 0; i < b.size(); i++)
stGCD[0][i] = b[i];
for (int i = 1; i < LOGN; i++)
for (int j = 0; j + (1 << (i - 1)) < b.size(); j++)
stGCD[i][j] = __gcd(stGCD[i - 1][j], stGCD[i - 1][j + (1 << (i - 1))]);
while (q--) {
int l, r; cin >> l >> r;
if (l == r) {
cout << 0 << " ";
continue;
}
l--; r -= 2;
int gcd = get_gcd(l, r);
cout << gcd << " ";
}
}

View file

@ -0,0 +1 @@
-std=c++20

188
codeforces/991/d.cc Normal file
View file

@ -0,0 +1,188 @@
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
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>
using hashmap = 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>>;
template <class Key>
using hashset = gp_hash_table<
Key, null_type, 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 multiset = 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
template <typename... Args> void print(std::string const &str, Args &&...args) {
std::cout << std::vformat(str,
std::make_format_args(std::forward<Args>(args)...));
}
template <typename T> void print(T const &t) { std::cout << t; }
template <std::ranges::range T> void print(T const &t) {
if constexpr (std::is_same_v<T, string> ||
std::is_convertible_v<T, char const *>) {
std::cout << t << '\n';
} else {
for (auto const &e : t) {
std::cout << e << ' ';
}
std::cout << '\n';
}
}
template <typename... Args>
void println(std::string const &str, Args &&...args) {
print(str, std::forward<Args>(args)...);
cout << '\n';
}
template <typename T> void println(T const &t) { print("{}\n", t); }
template <std::ranges::range T> void println(T const &t) { cout << t << '\n'; }
void println() { std::cout << '\n'; }
template <typename T> T MAX() { return std::numeric_limits<T>::max(); }
template <typename T> T MIN() { return std::numeric_limits<T>::min(); }
#define ff first
#define ss second
#define eb emplace_back
#define ll long long
#define ld long double
#define vec vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (r).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
#ifdef LOCAL
#define dbg(x) cout << __LINE__ << ": " << #x << "=<" << (x) << ">\n";
#else
#define dbg(x)
#endif
constexpr static int MOD = 1e9 + 7;
void solve() {
string s;
cin >> s;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == '0')
continue;
int j = i;
while (j >= 1 && s[j] - 1 > s[j - 1]) {
--s[j];
swap(s[j], s[j - 1]);
--j;
}
}
println(s);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}

7
codeforces/991/d.in Normal file
View file

@ -0,0 +1,7 @@
6
19
1709
11555
51476
9876543210
5891917899

6
codeforces/991/d.out Normal file
View file

@ -0,0 +1,6 @@
81
6710
33311
55431
9876543210
7875567711

50
codeforces/991/e.cc Normal file
View file

@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())
#define FOR(i, x) for (int i = 0; i < (x).size(); ++i)
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << (x) << '\n'
#else
#define dbg(x)
#endif
void solve() {
string a, b, c;
cin >> a >> b >> c;
if (a.size() < b.size()) {
swap(a, b);
}
vector<int> dp(b.size() + 1, 0);
for (int j = 1; j <= b.size(); ++j) {
dp[j] = dp[j - 1] + (b[j - 1] != c[j - 1]);
}
for (int i = 1; i <= a.size(); ++i) {
dp[0] += (a[i - 1] != c[i - 1]);
for (int j = 1; j <= b.size(); ++j) {
int k = i + j - 1;
dp[j] = min(dp[j] + (a[i - 1] != c[k]), dp[j - 1] + (b[j - 1] != c[k]));
}
}
cout << dp[b.size()] << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

22
codeforces/991/e.in Normal file
View file

@ -0,0 +1,22 @@
7
a
b
cb
ab
cd
acbd
ab
ba
aabb
xxx
yyy
xyxyxy
a
bcd
decf
codes
horse
codeforces
egg
annie
egaegaeg

0
codeforces/991/e.out Normal file
View file

219
codeforces/991/f.cc Normal file
View file

@ -0,0 +1,219 @@
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
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>
using hashmap = 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>>;
template <class Key>
using hashset = gp_hash_table<
Key, null_type, 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 multiset = 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
template <typename... Args> void print(std::string const &str, Args &&...args) {
std::cout << std::vformat(str,
std::make_format_args(std::forward<Args>(args)...));
}
template <typename T> void print(T const &t) { std::cout << t; }
template <std::ranges::range T> void print(T const &t) {
if constexpr (std::is_same_v<T, string> ||
std::is_convertible_v<T, char const *>) {
std::cout << t << '\n';
} else {
for (auto const &e : t) {
std::cout << e << ' ';
}
std::cout << '\n';
}
}
template <typename... Args>
void println(std::string const &str, Args &&...args) {
print(str, std::forward<Args>(args)...);
cout << '\n';
}
template <typename T> void println(T const &t) { print("{}\n", t); }
template <std::ranges::range T> void println(T const &t) { cout << t << '\n'; }
void println() { std::cout << '\n'; }
template <typename T> T MAX() { return std::numeric_limits<T>::max(); }
template <typename T> T MIN() { return std::numeric_limits<T>::min(); }
#define ff first
#define ss second
#define eb emplace_back
#define ll long long
#define ld long double
#define vec vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (r).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
#ifdef LOCAL
#define dbg(x) cout << __LINE__ << ": " << #x << "=<" << (x) << ">\n";
#else
#define dbg(x)
#endif
constexpr static int MOD = 1e9 + 7;
template <typename T, typename F> struct sparse_table {
sparse_table(std::vector<T> const &ts, F const &f)
: f(f), st(floor(__lg(max(1, static_cast<int>(ts.size())))) + 1,
std::vector<std::invoke_result_t<F, T, T>>(ts.size())) {
for (size_t i = 0; i < ts.size(); ++i) {
st[0][i] = ts[i];
}
for (size_t j = 1; j < st.size(); ++j) {
for (size_t i = 0; i + (1 << (j - 1)) < ts.size(); ++i) {
st[j][i] = f(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
}
}
[[nodiscard]] T query(int const l, int const r) const noexcept {
int k = floor(__lg(r - l + 1));
return f(st[k][l], st[k][r - (1 << k) + 1]);
}
F f;
std::vector<std::vector<std::invoke_result_t<F, T, T>>> st;
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (auto &e : a)
cin >> e;
vector<int> diffs(a.size() - 1, 0);
for (int i = 0; i < diffs.size(); i++)
diffs[i] = abs(a[i] - a[i + 1]);
auto gcd = [](int a, int b) { return __gcd(a, b); };
sparse_table<int, decltype(gcd)> st(diffs, gcd);
while (q--) {
int l, r;
cin >> l >> r;
if (l == r) {
cout << 0 << ' ';
} else {
--l;
r -= 2;
cout << st.query(l, r) << ' ';
}
}
cout << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}

18
codeforces/991/f.in Normal file
View file

@ -0,0 +1,18 @@
4
1 1
1
1 1
5 5
5 14 2 6 3
4 5
1 4
2 4
3 5
1 1
1 1
7
1 1
3 2
1 7 8
2 3
1 2

4
codeforces/991/f.out Normal file
View file

@ -0,0 +1,4 @@
0
3 1 4 1 0
0
1 6

82
codeforces/991/g.cc Normal file
View file

@ -0,0 +1,82 @@
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())
#define FOR(i, x) for (int i = 0; i < (x).size(); ++i)
template <typename T> T max() { return numeric_limits<T>::max(); }
template <typename T> T min() { return numeric_limits<T>::min(); }
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << (x) << '\n'
#else
#define dbg(x)
#endif
void solve() {
int n;
cin >> n;
unordered_map<int, vector<int>> graph;
while (--n) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
auto ans = min<int>();
auto is_leaf = [&graph](int u) {
return (u != 1 && graph[u].size() == 1) || (u == 1) && graph[u].empty();
};
auto dfs = [&](auto &dfs, int u, int parent) -> int {
if (u == parent || is_leaf(u)) {
return 0;
}
int first = 0, second = 0;
for (auto v : graph[u]) {
if (v == parent)
continue;
auto through = dfs(dfs, v, u);
if (through > first) {
second = through;
swap(first, second);
} else if (through > second) {
second = through;
}
}
int remove_u_and_one_child = sz(graph[u]) - (u != 1) - (first != 0) + first;
int remove_through_u =
sz(graph[u]) - (u != 1) - (first != 0) - (second != 0) + first + second;
int no_remove_u = 1 + first;
ans = max(ans, max(remove_through_u, no_remove_u));
return remove_u_and_one_child;
};
std::cout << max(ans, dfs(dfs, 1, -1)) << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

29
codeforces/991/g.in Normal file
View file

@ -0,0 +1,29 @@
6
2
1 2
5
1 2
2 3
3 4
3 5
4
1 2
2 3
3 4
5
2 1
3 1
4 1
5 4
6
2 1
3 1
4 1
5 3
6 3
6
2 1
3 2
4 2
5 3
6 4

6
codeforces/991/g.out Normal file
View file

@ -0,0 +1,6 @@
1
3
2
3
4
3

0
codeforces/991/x.out Normal file
View file