Skip to content

Commit 03c7d30

Browse files
committed
Update
1 parent b070f2e commit 03c7d30

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@
4747
- 排列组合枚举
4848
- 动态维护连续相同数字区间
4949
- 二分查找
50+
- 计时器
51+
- 动态数组
5052
- 匹配算法
5153
- 匈牙利算法
5254
- KM算法
5355
- 带花树算法
5456
- 高精度
5557
- 大整数类
5658
- 分数类
59+
- 高精度定点数
5760
- 动态规划
5861
- 斯坦纳树(点权)
5962
- 斯坦纳树(边权)

XCPC算法模板(2024-03-29).pdf

-1020 KB
Binary file not shown.

XCPC算法模板(2024-06-07).pdf

1.01 MB
Binary file not shown.

算法/基础算法/动态数组.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <ranges>
2+
#include <cassert>
3+
#include <cstdio>
4+
template<typename T, int maxsize>
5+
class darray : public std::ranges::view_interface<darray<T, maxsize>> {
6+
private:
7+
T data[maxsize];
8+
int sz;
9+
public:
10+
darray() : sz(0) {}
11+
darray(const darray& rhs) noexcept : sz(rhs.sz) {
12+
copy(rhs.begin(), rhs.begin() + sz, data);
13+
}
14+
darray& operator= (const darray& rhs) noexcept {
15+
sz = rhs.sz;
16+
copy(rhs.begin(), rhs.begin() + sz, data);
17+
return *this;
18+
}
19+
void push_back(T val) noexcept {
20+
assert(sz < maxsize);
21+
data[sz++] = std::move(val);
22+
}
23+
void expand() noexcept {
24+
assert(sz < maxsize);
25+
sz += 1;
26+
}
27+
void erase(T val) noexcept {
28+
for (int i = 0; i < sz; ++i) {
29+
if (data[i] == val) {
30+
data[i] = std::move(data[sz - 1]);
31+
sz -= 1;
32+
return;
33+
}
34+
}
35+
}
36+
void replace(T old_val, T new_val) noexcept {
37+
for (int i = 0; i < sz; ++i) {
38+
if (data[i] == old_val) {
39+
data[i] = new_val;
40+
return;
41+
}
42+
}
43+
}
44+
void set(T val) noexcept {
45+
data[0] = val;
46+
sz = 1;
47+
}
48+
bool contains(T val) const noexcept {
49+
for (int i = 0; i < sz; ++i) {
50+
if (data[i] == val) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
void pop_back() {
57+
assert(sz > 0);
58+
sz -= 1;
59+
}
60+
void clear() noexcept {
61+
sz = 0;
62+
}
63+
auto* begin() noexcept {
64+
return data;
65+
}
66+
auto* end() noexcept {
67+
return data + sz;
68+
}
69+
const auto* begin() const noexcept {
70+
return data;
71+
}
72+
const auto* end() const noexcept {
73+
return data + sz;
74+
}
75+
};
76+
int main() {
77+
darray<int, 100> arr;
78+
for (int i = 0; i < 50; ++i) {
79+
arr.push_back(i);
80+
}
81+
for (int i = 1; i < 50; ++i) {
82+
arr[i] += arr[i - 1];
83+
}
84+
std::printf("%d %d %d\n", (int)arr.size(), arr.front(), arr[30]);
85+
return 0;
86+
}

算法/基础算法/计时器.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <chrono>
3+
#include <set>
4+
struct Timer {
5+
std::chrono::steady_clock::time_point start;
6+
Timer() : start(std::chrono::steady_clock::now()) {}
7+
~Timer() {
8+
auto finish = std::chrono::steady_clock::now();
9+
auto runtime = std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count();
10+
std::cerr << runtime / 1e6 << "s" << std::endl;
11+
}
12+
};
13+
int main() {
14+
Timer timer;
15+
std::set<int> S;
16+
for (int i = 0; i < 1e6; ++i) {
17+
S.insert(i);
18+
}
19+
return 0;
20+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <array>
4+
class fixed { // 高精度定点数
5+
private:
6+
static constexpr int num_blocks = 40;
7+
static constexpr int offset = (num_blocks / 2) * 64;
8+
std::array<uint64_t, num_blocks> data = {};
9+
public:
10+
fixed(double value) {
11+
uint64_t bits = *(uint64_t*)&value;
12+
uint64_t fraction = bits & 0xfffffffffffff;
13+
uint64_t exponent = (bits & 0x7ff0000000000000) >> 52;
14+
uint64_t sign = bits >> 63;
15+
if (exponent != 0) {
16+
exponent -= 1023;
17+
fraction |= (1ull << 52);
18+
}
19+
else {
20+
exponent = -1022;
21+
}
22+
exponent += offset - 52;
23+
data[exponent / 64] |= (fraction << (exponent % 64));
24+
fraction <<= 11;
25+
data[(exponent + 52) / 64] |= (fraction >> (63 - (exponent + 52) % 64));
26+
if (sign) {
27+
for (int i = 0; i < num_blocks; ++i) {
28+
data[i] = ~data[i];
29+
}
30+
for (int i = 0; i < num_blocks; ++i) {
31+
data[i] += 1;
32+
if (data[i]) {
33+
break;
34+
}
35+
}
36+
}
37+
}
38+
explicit operator double() const {
39+
decltype(this->data) data = this->data;
40+
uint64_t sign = 0;
41+
if (data[num_blocks - 1] & (1ull << 63)) {
42+
sign = 1;
43+
for (int i = 0; i < num_blocks; ++i) {
44+
data[i] = ~data[i];
45+
}
46+
for (int i = 0; i < num_blocks; ++i) {
47+
data[i] += 1;
48+
if (data[i]) {
49+
break;
50+
}
51+
}
52+
}
53+
int64_t top = -1;
54+
for (int i = num_blocks - 1; i >= 0; --i) {
55+
if (data[i]) {
56+
for (int j = 63; j >= 0; --j) {
57+
if (data[i] & (1ull << j)) {
58+
top = i * 64 + j;
59+
goto finish;
60+
}
61+
}
62+
}
63+
}
64+
if (top == -1) {
65+
return 0;
66+
}
67+
finish:;
68+
int64_t exponent = top - offset + 1023;
69+
uint64_t fraction = 0;
70+
if (exponent >= 2047) {
71+
exponent = 2047;
72+
}
73+
else {
74+
for (int i = 1; i <= 52; ++i) {
75+
auto block = (top - i) / 64;
76+
auto num = (top - i) % 64;
77+
if (data[block] & (1ull << num)) {
78+
fraction |= (1ull << (52 - i));
79+
}
80+
}
81+
uint64_t carry = 0;
82+
if (data[(top - 53) / 64] & (1ull << ((top - 53) % 64))) {
83+
for (int i = 54; i <= 54 + 64; ++i) {
84+
auto block = (top - i) / 64;
85+
auto num = (top - i) % 64;
86+
if (data[block] & (1ull << num)) {
87+
carry = 1;
88+
break;
89+
}
90+
}
91+
if (!carry) {
92+
auto block = (top - 53) / 64;
93+
for (int i = 0; i < block; ++i) if (data[i]) {
94+
carry = 1;
95+
break;
96+
}
97+
}
98+
if (!carry) {
99+
if (fraction & 1) {
100+
carry = 1;
101+
}
102+
}
103+
}
104+
fraction += carry;
105+
if (fraction > 0xfffffffffffff) {
106+
fraction = 0;
107+
exponent += 1;
108+
}
109+
if (exponent <= 0) {
110+
fraction |= (1ull << 52);
111+
fraction >>= -exponent + 1;
112+
exponent = 0;
113+
}
114+
}
115+
fraction &= 0xfffffffffffff;
116+
uint64_t ret = (sign << 63) | (exponent << 52) | fraction;
117+
return *(double*)&ret;
118+
}
119+
void operator+= (const fixed& rhs) {
120+
uint64_t overflow = 0;
121+
for (int i = 0; i < num_blocks; ++i) {
122+
uint64_t value = rhs.data[i] + overflow;
123+
if (value < overflow) {
124+
overflow = 1;
125+
}
126+
else {
127+
overflow = 0;
128+
}
129+
data[i] += value;
130+
if (data[i] < value) {
131+
overflow = 1;
132+
}
133+
}
134+
}
135+
fixed operator+ (const fixed& rhs) const {
136+
fixed ret = *this;
137+
ret += rhs;
138+
return ret;
139+
}
140+
void debug() const {
141+
for (int i = 0; i < num_blocks; ++i) {
142+
for (int j = 0; j < 64; ++j) {
143+
if (data[i] & (1ull << j)) {
144+
printf("bit: %d\n", i * 64 + j - offset);
145+
}
146+
}
147+
}
148+
}
149+
};
150+
int main() {
151+
return 0;
152+
}

0 commit comments

Comments
 (0)