├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── STYLING_ASSISTANCE.md ├── STYLING_GUIDE.md ├── TEMPLATE.CPP ├── c++ ├── Graph │ ├── HLD │ │ ├── README.md │ │ └── ZJOI2008_count.cpp │ ├── Searching │ │ └── README.md │ ├── bcc.cpp │ ├── biGraphMatching.cpp │ ├── binaryCheck.cpp │ ├── connectivity │ │ └── README.md │ ├── cutBridge.cpp │ ├── cutBridge_lrj.cpp │ ├── dijkstra.cpp │ ├── dinic.cpp │ ├── divide.cpp │ ├── eulerRoute.cpp │ ├── floyd.cpp │ ├── konig.cpp │ ├── mcf.cpp │ ├── spfa.cpp │ └── toposort.cpp ├── Math │ ├── ext_gcd │ │ ├── README.md │ │ └── ext_gcd.cpp │ ├── gcd │ │ ├── README.md │ │ ├── gcd_non_recursive.cpp │ │ └── gcd_recursive.cpp │ ├── matrix │ │ └── README.md │ ├── prime │ │ ├── README.md │ │ ├── euler_primelist_generating.cpp │ │ └── prime_validating.cpp │ └── quick_pow │ │ ├── README.md │ │ └── quick_pow.cpp ├── README.md ├── String │ ├── acAutomaton.cpp │ ├── eval.cpp │ ├── kmp.cpp │ ├── manacher.cpp │ └── sa.cpp └── dataStructure │ ├── binaryHeap.cpp │ ├── bit2d.cpp │ ├── dfsSeqAndTemporalSegTree.cpp │ ├── heap │ └── README.md │ ├── lca.cpp │ ├── leftTree.cpp │ ├── segment_tree │ ├── README.md │ ├── segment_tree_point_modify_seg_query.cpp │ ├── segment_tree_seg_modify_seg_query.cpp │ └── segment_tree_with_pools.cpp │ ├── size_balanced_tree │ ├── README.md │ └── size_balanced_tree.cpp │ ├── st.cpp │ ├── treap.cpp │ └── treapMerge.cpp ├── cpplint.py ├── generate_cpp_list.sh └── test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Test files 31 | tee 32 | file.list 33 | files.log 34 | a 35 | *.cpp~ 36 | *.md~ 37 | *.in 38 | *.out 39 | *.res 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | compiler: 3 | - gcc 4 | script: 5 | - g++ test.cpp -o tee 6 | - ./tee 7 | - cat files.log -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | First, note that this library is under **SATA**, so please add only your **own** codes and make sure that you agree with the terms in that license. 4 | 5 | ## Methods 6 | 7 | :star2: Add what you think should be added into [#2](https://github.com/24OI/Standard_Code_Library/issues/2). 8 | 9 | :star2: Create a PR with your code(which SHOULD **pass the specific tests**, **follow the [STYLING_GUIDE.md](STYLING_GUIDE.md)**, **have meaningful comments and commit messages**). 10 | 11 | :star2: If you have different ideas on implementing something, just create a PR with your own ideas and name it with things like `segmentTree_pointers.cpp` 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The Star And Thank Author License (SATA) 2 | 3 | Copyright (c) <2014-2016> <24OI>(ccfnoi2016@126.com) 4 | 5 | Project Url: https://github.com/24OI/Standard_Code_Library 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | And wait, the most important, you shall star/+1/like the project(s) in project url 18 | section above first, and then thank the author(s) in Copyright section. 19 | 20 | Here are some suggested ways: 21 | 22 | - Email the authors a thank-you letter, and make friends with him/her/them. 23 | - Report bugs or issues. 24 | - Tell friends what a wonderful project this is. 25 | - And, sure, you can just express thanks in your mind without telling the world. 26 | 27 | Contributors of this project by forking have the option to add his/her name and 28 | forked project url at copyright and project url sections, but shall not delete 29 | or modify anything else in these two sections. 30 | 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 33 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 34 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 37 | THE SOFTWARE. 38 | 39 | IN MEMORY OF 24OI. 40 | 41 | OI, a never vanished dream. 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Standard_Code_Library for OI and ICPC [![Build Status](https://img.shields.io/travis/24OI/Standard_Code_Library.svg?style=flat-square)](https://github.com/24OI/Standard_Code_Library) 2 | 3 | Standard_Code_Library, a.k.a, Templates. [![Join the chat at https://gitter.im/24OI/Standard_Code_Library](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/24OI/Standard_Code_Library?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | This library is especially for OIers and ICPCers in their journey exploring the area of competitive programming. 6 | 7 | **Note that we only have C++ codes at the moment, add your favorite languages if you want!** 8 | 9 | ## What's Included? 10 | 11 | :triangular_flag_on_post: Nothing much at the moment... 12 | 13 | Check out [#2](https://github.com/24OI/Standard_Code_Library/issues/2) for more details on current status. 14 | 15 | ## LICENSE? 16 | 17 | Note that we are using SATA License so just **Star And Thank Author**. 18 | 19 | Hope you enjoy learning through this repo. 20 | 21 | It is strongly recommended to mention the original author while using the codes in this repo. 22 | 23 | ## Contributing? 24 | 25 | See [CONTRIBUTING.md](CONTRIBUTING.md). 26 | 27 | You know, there are always many ways to implement some algorithms or data structures. 28 | 29 | Yours are always welcome. 30 | 31 | ## Contributors? 32 | 33 | :+1: See [Contributors Page](https://github.com/24OI/Standard_Code_Library/graphs/contributors) collected by Github. 34 | -------------------------------------------------------------------------------- /STYLING_ASSISTANCE.md: -------------------------------------------------------------------------------- 1 | # STYLING ASSISTANCE FOR 24OI SCL 2 | 这篇文档将提供一些正则表达式替换组合,协助您的代码通过cpplint的检查,减少您的工作量。以ST作为示例 3 | 4 | 1. `\<(if|while|for)(?: {2,}|)\(`→`\1 (` 5 | 4. `\)(?: {2,}|)\{`→`) {` 6 | 5. `([;,])([\+\-a-zA-Z\_\&\*\!\(\-\+)])`→`\1 \2` 7 | 6. `\t`→   8 | 7. `([^ ]|[\da-zA-Z\_])(\|\||\&\&|\+\+|\-\-|/\*|\*/|<<|>>|\%\=|//|[\+\-\*\/\!><\=\|\&\^]\=?)([^ ]|[\da-zA-Z\_\(])(?!$)`→`\1 \2 \3` 9 | 这个组合并不是十分完美,对于头文件引用和模板声明的语句会错误的处理,使用这个组合之后注意手动修改。 10 | 8. `(\)|[\da-zA-Z\_])(?: {2,}|)\{`→`\1 {` 11 | 9. `\}(?: {2,}|)([\da-zA-Z\_])`→`} \1` 12 | 10. `(\+\+|\-\-) {2,}([\da-zA-Z\_])`→`\1\2` 13 | 11. `([\da-zA-Z\_]) {2,}(\+\+|\-\-)`→`\1\2` 14 | 12. `% {2,}([ulfdI\.])`→`%\1` 15 | 12.  $→`(empty)` 16 | 13. `\!(?: {2,})([\da-zA-Z\_\(])`->`!\1` 17 | 18 | **建议按照顺序进行替换**,可以写成python脚本进行自动替换,这些组合不能为你解决所有的问题,但是可以大幅减轻代码规范化的工作量,如果您的代码风格很糟糕(像我一样),这些组合是十分适合您的,但仍需要您参照cpplint给出的意见进行少量的手动修改 19 | *** 20 | 欢迎丰富并纠正这些组合! -------------------------------------------------------------------------------- /STYLING_GUIDE.md: -------------------------------------------------------------------------------- 1 | # Styling Guide 2 | 3 | **Note that we were talking about C++** 4 | 5 | ## Main 6 | 7 | We might be following [Google's Styling Guide](https://github.com/google/styleguide). 8 | 9 | The official cppguide is [on Google's Github Site](http://google.github.io/styleguide/cppguide.html). 10 | 11 | We can also follow a [zh-google-styleguide](http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/) if you find it hard to remember all the English guidelines. 12 | 13 | A tool called [cpplint](https://pypi.python.org/pypi/cpplint) can be used to help your check your codes. Also at [cpplint.py](https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py). 14 | 15 | Something else [for emacs](https://raw.githubusercontent.com/google/styleguide/gh-pages/google-c-style.el). 16 | 17 | You might want to take a look at [STYLING_ASSISTANCE.md](STYLING_ASSISTANCE.md) 18 | 19 | Here are something you might forget: 20 | 21 | - [ ] 将函数变量尽可能置于最小作用域内, 并在变量声明时进行初始化 22 | - [ ] 注意别在循环犯大量构造和析构的低级错误 23 | - [ ] 在类中使用特定的声明顺序:`public:`在`private:`之前, 成员函数在数据成员 (变量) 前 24 | - [ ] 倾向编写简短, 凝练的函数 25 | - [ ] [所有按引用传递的参数必须加上 const](http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/others/) 26 | - [ ] 只在定义移动构造函数与移动赋值操作时使用右值引用. 不要使用 `std::forward` 27 | - [ ] 我们不允许使用缺省函数参数,少数极端情况除外。尽可能改用函数重载 28 | - [ ] 我们不使用 C++ 异常 29 | - [ ] 只在记录日志时使用流(`string`之类的除外) 30 | - [ ] 对于迭代器和其他模板对象使用前缀形式`(++i)`的自增, 自减运算符 31 | - [ ] 我们强烈建议你在任何可能的情况下都要使用`const`. 此外有时改用 C++11 推出的`constexpr`更好。在 C++11 里,用`constexpr`来定义真正的常量,或实现常量初始化 32 | - [ ] 使用断言来指出变量为非负数, 而不是使用无符号型 33 | - [ ] 代码应该对 64 位和 32 位系统友好. 处理打印, 比较, 结构体对齐时应切记 34 | - [ ] **使用宏时要非常谨慎, 尽量以内联函数, 枚举和常量代替之** 35 | - [ ] 整数用 0, 实数用 0.0, 指针用`nullptr`或`NULL`, 字符 (串) 用`'\0'` 36 | - [ ] 用 auto 绕过烦琐的类型名,只要可读性好就继续用,别用在局部变量之外的地方 37 | - [ ] 函数命名,变量命名,文件命名要有描述性;少用缩写。尽可能给有描述性的命名。文件名要全部小写, 可以包含下划线 (_) 。类型名称的每个单词首字母均大写。变量名一律小写, 单词之间用下划线连接. 类的成员变量以下划线结尾, 但结构体的就不用。在全局或类里的常量名称前加`k`: kDaysInAWeek. 且除去开头的`k`之外每个单词开头字母均大写。常规函数使用大小写混合, 取值和设值函数则要求与变量名匹配。 38 | - [ ] 注释虽然写起来很痛苦, 但对保证代码可读性至关重要。注释固然很重要, 但最好的代码本身应该是自文档化。使用`//`或`/* */`, 统一就好。在每一个文件开头加入版权公告, 然后是文件内容描述。(参考[TEMPLATE.CPP](TEMPLATE.CPP))每个类的定义都要附带一份注释, 描述类的功能和用法.函数声明处注释描述函数功能; 定义处描述函数实现.通常变量名本身足以很好说明变量用途. 某些情况下, 也需要额外的注释说明.对于代码中巧妙的, 晦涩的, 有趣的, 重要的地方加以注释.注意 永远不要 用自然语言翻译代码作为注释. 要假设读代码的人 C++ 水平比你高, 即便他/她可能不知道你的用意.对那些临时的, 短期的解决方案, 或已经够好但仍不完美的代码使用`TODO`注释.`TODO`注释要使用全大写的字符串`TODO`, 在随后的圆括号里写上你的大名, 邮件地址, 或其它身份标识. 冒号是可选的. 39 | - [ ] 每一行代码字符数不超过 80.如果一行注释包含了超过 80 字符的命令或 URL, 出于复制粘贴的方便允许该行超过 80 字符.尽量不使用非 ASCII 字符, 使用时必须使用`UTF-8` 编码.**只使用空格, 每次缩进 2 个空格.** 40 | - [ ] 返回类型和函数名在同一行, 参数也尽量放在同一行,如果放不下就对形参分行。**左圆括号总是和函数名在同一行;**函数名和左圆括号间没有空格;圆括号与参数间没有空格;左大括号总在最后一个参数同一行的末尾处;右大括号总是单独位于函数最后一行,或者与左大括号同一行。右大括号和左大括号间总是有一个空格;函数声明和定义中的所有形参必须有命名且一致;所有形参应尽可能对齐;**缺省缩进为 2 个空格;换行后的参数保持 4 个空格的缩进;**如果有些参数没有用到, 在函数定义处将参数名注释起来:要么一行写完函数调用,要么在圆括号里对参数分行,要么参数另起一行且缩进四格。如果没有其它顾虑的话,尽可能精简行数,比如把多个参数适当地放在同一行里。倾向于不在圆括号内使用空格. 关键字 if 和 else 另起一行. 41 | - [ ] 如果语句中某个`if-else`分支使用了大括号的话, 其它分支也必须使用.`switch`语句可以使用大括号分段,以表明`cases`之间不是连在一起的。在单语句循环里,括号可用可不用。空循环体应使用`{}`或 `continue`.句点或箭头前后不要有空格. 指针/地址操作符`(*, &)`之后不能有空格.如果一个布尔表达式超过 标准行宽, 断行方式要统一一下.`return`表达式里时没必要都用圆括号。**指针/地址操作符与变量名紧邻** 42 | - [ ] **规则有特例** 43 | - [ ] **运用常识和判断力, 并保持一致** 44 | 45 | ## Recommended Sublime Settings 46 | 47 | Open a `.cpp` file 48 | 49 | GOTO `Preferences`-->`Settings-More`-->`Syntax Specific-User` 50 | 51 | You shall be editing `c++.sublime-settings` as below: 52 | 53 | ```json 54 | { 55 | "tab_size": 2, 56 | "translate_tabs_to_spaces": true, 57 | "line_numbers": true, 58 | "word_wrap": true, 59 | "wrap_width": 80, 60 | "match_brackets": true, 61 | } 62 | ``` 63 | 64 | ## Other Notes(Not forced) 65 | 66 | It might be a good idea to add empty lines into where you find necessary to make things clear. 67 | 68 | -------------------------------------------------------------------------------- /TEMPLATE.CPP: -------------------------------------------------------------------------------- 1 | /** 2 | * @name TEMPLATE.CPP 3 | * @subject Segment Tree 4 | * @author Ir1d(sirius.caffrey@gmail.com) 5 | * @copyright SATA(The Star And Thank Author License) 6 | * @comment This is a segment tree implemented with pointers.(kidding) 7 | */ 8 | 9 | // I used DocBlockr to make this comment! 10 | // Simply type '/**' in the first line 11 | 12 | #include // NOLINT 13 | #include // NOLINT 14 | #include // NOLINT 15 | #include // NOLINT 16 | #define x1 x11 17 | #define y1 y11 18 | 19 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 20 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 21 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 22 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 23 | 24 | #ifdef WIN32 25 | #define LLD "%I64d" 26 | #define LLU "%I64u" 27 | #else 28 | #define LLD "%lld" 29 | #define LLU "%llu" 30 | #endif 31 | 32 | typedef long long LL;// NOLINT 33 | typedef long double real; 34 | 35 | const double INF = 1e100; 36 | const int oo = ~0u >> 2; 37 | const double pi = acos(-1.0); 38 | const double EPS = 1e-8; 39 | const int MAXN = 100033; 40 | 41 | int main() { 42 | #ifdef LOCAL 43 | freopen("a.in", "r", stdin); 44 | freopen("a.out", "w", stdout); 45 | #endif 46 | 47 | #ifdef LOCAL 48 | fclose(stdin); 49 | fclose(stdout); 50 | #endif 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /c++/Graph/HLD/README.md: -------------------------------------------------------------------------------- 1 | # HLD(树链剖分) 2 | 3 | Solving questions which require information or modification on trees. 4 | 5 | This piece of code is not a strict SCL component because it doesn't have essential standard interfaces. So you may have too read and understand it before you can modify it. -------------------------------------------------------------------------------- /c++/Graph/HLD/ZJOI2008_count.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name ZJOI2008_count.cpp 3 | * @subject HLD 4 | * @author CJSoft (egwcyh@qq.com) 5 | * @Copyright DWTFABG 6 | * @comment HLD including editing single node and Sum/Max query between two single nodes 7 | * @tests http://codevs.cn/problem/2460/ 8 | */ 9 | 10 | /** 11 | * Sorry, but there's no standard interface. 12 | * But it's easy to modify its function. 13 | * For future instruction about this piece of HLD code, 14 | * redirect to https://github.com/cjsoft/noip/blob/master/notes/hld.md 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #define CLEAR(x) memset(x, 0, sizeof(x)) 22 | const int MXN = 30007; 23 | typedef long long ll; //NOLINT 24 | int n; 25 | struct edge { 26 | int to; 27 | explicit edge(int a) { 28 | to = a; 29 | } 30 | }; 31 | std::vector v[MXN]; 32 | int nodeweight[MXN]; 33 | int c_sub_nodes[MXN]; 34 | int fa[MXN]; 35 | int h_son[MXN]; 36 | int head[MXN]; 37 | int dep[MXN]; 38 | int locator[MXN]; 39 | int revlocator[MXN]; 40 | int tot = 1; 41 | void dfs_1(int root, int from, int depth) { 42 | if (!from) { 43 | fa[root] = 0; 44 | } else { 45 | fa[root] = from; 46 | } 47 | dep[root] = depth; 48 | c_sub_nodes[root] = 1; 49 | h_son[root] = 0; 50 | for (int i = 0; i < v[root].size(); ++i) { 51 | if (v[root][i] == from) continue; 52 | dfs_1(v[root][i], root, depth + 1); 53 | c_sub_nodes[root] += c_sub_nodes[v[root][i]]; 54 | if (c_sub_nodes[v[root][i]] >= c_sub_nodes[h_son[root]]) { 55 | h_son[root] = v[root][i]; 56 | } 57 | } 58 | } 59 | void dfs_2(int root, int hd) { 60 | locator[root] = tot; 61 | revlocator[tot++] = root; 62 | head[root] = hd; 63 | if (h_son[root]) { 64 | dfs_2(h_son[root], hd); 65 | } 66 | for (int i = 0; i < v[root].size(); ++i) { 67 | if (v[root][i] == h_son[root] || v[root][i] == fa[root]) continue; 68 | dfs_2(v[root][i], v[root][i]); 69 | } 70 | } 71 | struct RMQ_QJH_XDS { 72 | ll stree_rmq[MXN * 4 + 7]; 73 | ll stree_qjh[MXN * 4 + 7]; 74 | RMQ_QJH_XDS() { 75 | std::fill(stree_rmq, stree_rmq + MXN * 4 + 7, - 1e9); 76 | std::fill(stree_qjh, stree_qjh + MXN * 4 + 7, 0); 77 | } 78 | inline int lson(int root) { 79 | return root << 1; 80 | } 81 | inline int rson(int root) { 82 | return root << 1 | 1; 83 | } 84 | void build(int root, int l, int r) { 85 | if (l == r) { 86 | stree_rmq[root] = nodeweight[revlocator[l]]; 87 | stree_qjh[root] = nodeweight[revlocator[l]]; 88 | return; 89 | } 90 | int m = (l + r) >> 1; 91 | build(lson(root), l, m); 92 | build(rson(root), m + 1, r); 93 | stree_rmq[root] = std::max( 94 | stree_rmq[lson(root)], 95 | stree_rmq[rson(root)]); 96 | stree_qjh[root] = stree_qjh[lson(root)] + stree_qjh[rson(root)]; 97 | } 98 | ll rmq_query(int root, int l, int r, int queryl, int queryr) { 99 | if (queryl > queryr) std::swap(queryl, queryr); 100 | if (queryl > r || queryr < l) return - 1e9; 101 | if (queryl <= l && queryr >= r) return stree_rmq[root]; 102 | int m = (l + r) >> 1; 103 | return std::max(rmq_query(lson(root), l, m, queryl, queryr), 104 | rmq_query(rson(root), m + 1, r, queryl, queryr)); 105 | } 106 | ll qjh_query(int root, int l, int r, int queryl, int queryr) { 107 | if (queryl > queryr) std::swap(queryl, queryr); 108 | if (queryl > r || queryr < l) return 0; 109 | if (queryl <= l && queryr >= r) return stree_qjh[root]; 110 | int m = (l + r) >> 1; 111 | return qjh_query(lson(root), l, m, queryl, queryr) + 112 | qjh_query(rson(root), m + 1, r, queryl, queryr); 113 | } 114 | void edit(int root, int l, int r, int pos, ll new_data) { 115 | if (l == r) { 116 | stree_qjh[root] = new_data; 117 | stree_rmq[root] = new_data; 118 | return; 119 | } 120 | if (pos > ((l + r) >> 1)) { 121 | edit(rson(root), ((l + r) >> 1) + 1, r, pos, new_data); 122 | } else { 123 | edit(lson(root), l, (l + r) >> 1, pos, new_data); 124 | } 125 | stree_rmq[root] = std::max(stree_rmq[lson(root)], 126 | stree_rmq[rson(root)]); 127 | stree_qjh[root] = stree_qjh[lson(root)] + 128 | stree_qjh[rson(root)]; 129 | } 130 | } slpf; 131 | ll tree_rmq_query(int l, int r) { 132 | ll resu = - 1e9; 133 | while (head[l] != head[r]) { 134 | if (dep[head[l]] < dep[head[r]]) std::swap(l, r); 135 | resu = std::max(resu, 136 | slpf.rmq_query(1, 1, tot - 1, locator[head[l]], locator[l])); 137 | l = fa[head[l]]; 138 | } 139 | if (locator[l] > locator[r]) std::swap(l, r); 140 | resu = std::max(resu, 141 | slpf.rmq_query(1, 1, tot - 1, locator[l], locator[r])); 142 | return resu; 143 | } 144 | ll tree_qjh_query(int l, int r) { 145 | ll resu = 0; 146 | while (head[l] != head[r]) { 147 | if (dep[head[l]] < dep[head[r]]) std::swap(l, r); 148 | resu += slpf.qjh_query(1, 1, tot - 1, locator[head[l]], locator[l]); 149 | l = fa[head[l]]; 150 | } 151 | if (locator[l] > locator[r]) std::swap(l, r); 152 | resu += slpf.qjh_query(1, 1, tot - 1, locator[l], locator[r]); 153 | return resu; 154 | } 155 | int main() { 156 | CLEAR(nodeweight); 157 | CLEAR(c_sub_nodes); 158 | CLEAR(fa); 159 | CLEAR(h_son); 160 | CLEAR(head); 161 | CLEAR(dep); 162 | CLEAR(locator); 163 | CLEAR(revlocator); 164 | scanf("%d", &n); 165 | int tempa, tempb; 166 | for (int i = 0; i < n - 1; ++i) { 167 | scanf("%d %d", &tempa, &tempb); 168 | v[tempa].push_back(tempb); 169 | v[tempb].push_back(tempa); 170 | } 171 | for (int i = 1; i <= n; ++i) { 172 | scanf("%d", &nodeweight[i]); 173 | } 174 | head[1] = 1; 175 | dfs_1(1, 0, 1); 176 | dfs_2(1, 1); 177 | slpf.build(1, 1, tot - 1); 178 | int q; 179 | char tc[11]; 180 | scanf("%d", &q); 181 | for (int i = 0; i < q; ++i) { 182 | CLEAR(tc); 183 | scanf("%s %d %d", tc, &tempa, &tempb); 184 | if (tc[1] == 'M') { 185 | ll asd = tree_rmq_query(tempa, tempb); 186 | printf("%lld\n", asd); 187 | } else if (tc[1] == 'S') { 188 | printf("%lld\n", tree_qjh_query(tempa, tempb)); 189 | } else { 190 | slpf.edit(1, 1, tot - 1, locator[tempa], tempb); 191 | nodeweight[tempa] = tempb; 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /c++/Graph/Searching/README.md: -------------------------------------------------------------------------------- 1 | # Searching -------------------------------------------------------------------------------- /c++/Graph/bcc.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1184 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #include // NOLINT 12 | #include // NOLINT 13 | #define x1 x11 14 | #define y1 y11 15 | 16 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 17 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 18 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 19 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 20 | 21 | #ifdef WIN32 22 | #define LLD "%I64d" 23 | #define LLU "%I64u" 24 | #else 25 | #define LLD "%lld" 26 | #define LLU "%llu" 27 | #endif 28 | 29 | typedef long long LL;// NOLINT 30 | typedef long double real; 31 | 32 | const double INF = 1e100; 33 | const int oo = ~0u >> 2; 34 | const double pi = acos(-1.0); 35 | const double EPS = 1e-8; 36 | const int MAXN = 100033; 37 | 38 | int n, m, u, v; 39 | struct node { 40 | int s, t, x; 41 | } e[MAXN << 1]; 42 | int head[MAXN], cnt = 1; 43 | inline void add(int s, int t) { 44 | e[++cnt].t = t; e[cnt].s = s; e[cnt].x = head[s]; head[s] = cnt; 45 | e[++cnt].t = s; e[cnt].s = t; e[cnt].x = head[t]; head[t] = cnt; 46 | } 47 | int dfn[MAXN], cut[MAXN], belong[MAXN << 1], idx, bcc, low[MAXN]; 48 | std::vector res[MAXN]; 49 | typedef std::pair PII; 50 | typedef std::pair P; 51 | P sta[MAXN], tee; 52 | int top; 53 | void dfs(int u, int fa) { 54 | low[u] = dfn[u] = ++idx; 55 | int ch = 0, v; 56 | for (int i = head[u]; i; i = e[i].x) { 57 | v = e[i].t; 58 | if (!dfn[v]) { 59 | ++ch; 60 | sta[top++] = P(PII(e[i].s, e[i].t), i); 61 | dfs(v, u); 62 | if (low[v] < low[u]) low[u] = low[v]; 63 | if (low[v] >= dfn[u]) { 64 | cut[u] = 1; 65 | ++bcc; res[bcc].clear(); 66 | for (;;) { 67 | tee = sta[--top]; 68 | belong[tee.second] = bcc; 69 | // if (belong[tee.first.first] != bcc) { 70 | // res[bcc].push_back(tee.first.first); 71 | // belong[tee.first] = bcc; 72 | // } 73 | // if (belong[tee.first.second] != bcc) { 74 | // res[bcc].push_back(tee.first.second); 75 | // belong[tee.second] = bcc; 76 | // } 77 | if (tee.first.first == u && tee.first.second == v) break; 78 | } 79 | } 80 | } else if (dfn[v] < dfn[u] && v != fa) { 81 | sta[top++] = P(PII(e[i].s, e[i].t), i); 82 | if (dfn[v] < low[u]) low[u] = dfn[v]; 83 | } 84 | } 85 | if (fa < 0 && ch == 1) cut[u] = 0; 86 | } 87 | int mn[MAXN]; 88 | void solve() { 89 | memset(dfn, 0, sizeof dfn); 90 | memset(cut, 0, sizeof cut); 91 | memset(belong, 0, sizeof belong); 92 | idx = top = 0; 93 | g(i, 1, n) if (!dfn[i]) dfs(i, -1); 94 | } 95 | int main() { 96 | scanf("%d%d", &n, &m); 97 | f(i, 0, m) { 98 | scanf("%d%d", &u, &v); 99 | add(u, v); 100 | } 101 | solve(); 102 | printf("%d\n", bcc); 103 | memset(mn, 0x1f, sizeof mn); 104 | g(i, 2, cnt) printf("%d ", belong[i]); puts(""); 105 | for (int i = 2; i <= cnt; i += 2) { 106 | mn[belong[i]] = std::min(mn[belong[i]], i / 2); 107 | } 108 | // printf("%d\n", cnt); 109 | for (int i = 2; i <= cnt; i += 2) printf("%d ", mn[belong[i]]); puts(""); 110 | // g(i, 1, n) printf("%d ", belong[i]); puts(""); 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /c++/Graph/biGraphMatching.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://uoj.ac/problem/78 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #include // NOLINT 12 | #define x1 x11 13 | #define y1 y11 14 | 15 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 16 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 17 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 18 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 19 | 20 | #ifdef WIN32 21 | #define LLD "%I64d" 22 | #define LLU "%I64u" 23 | #else 24 | #define LLD "%lld" 25 | #define LLU "%llu" 26 | #endif 27 | 28 | typedef long long LL;// NOLINT 29 | typedef long double real; 30 | 31 | const double INF = 1e100; 32 | const int oo = ~0u >> 2; 33 | const double pi = acos(-1.0); 34 | const double EPS = 1e-8; 35 | const int MAXN = 100033; 36 | 37 | int n, m; 38 | struct node { 39 | int t, w, x; 40 | } e[MAXN * 50]; 41 | int head[MAXN], cnt = 1; 42 | inline void add(int s, int t, int w) { 43 | // printf("```%d %d %d\n", s, t, w); 44 | e[++cnt].t = t; e[cnt].w = w; e[cnt].x = head[s]; head[s] = cnt; 45 | e[++cnt].t = s; e[cnt].w = 0; e[cnt].x = head[t]; head[t] = cnt; 46 | } 47 | std::queue Q; 48 | int dis[MAXN]; 49 | int S, T; 50 | bool bfs() { 51 | memset(dis, -1, sizeof dis); 52 | dis[S] = 0; Q.push(S); 53 | int u; 54 | while (!Q.empty()) { 55 | u = Q.front(); Q.pop(); 56 | for (int i = head[u]; i; i = e[i].x) { 57 | if (e[i].w && dis[e[i].t] == -1) { 58 | dis[e[i].t] = dis[u] + 1; 59 | Q.push(e[i].t); 60 | } 61 | } 62 | } 63 | return dis[T] != -1; 64 | } 65 | int dfs(int x, int fl) { 66 | if (x == T) return fl; 67 | int w, used = 0; 68 | for (int i = head[x]; i; i = e[i].x) { 69 | if (e[i].w && dis[e[i].t] == dis[x] + 1) { 70 | w = dfs(e[i].t, std::min(fl - used, e[i].w)); 71 | e[i].w -= w; e[i ^ 1].w += w; used += w; 72 | if (used == fl) return fl; 73 | } 74 | } 75 | if (!used) dis[x] = -1; 76 | return used; 77 | } 78 | int dinic() { 79 | int res = 0; 80 | while (bfs()) res += dfs(S, oo); 81 | return res; 82 | } 83 | int x, y; 84 | int q; 85 | int res[MAXN]; 86 | int main() { 87 | scanf("%d%d%d", &n, &m, &q); 88 | S = n + m + 1; T = n + m + 2; 89 | f(i, 0, q) { 90 | scanf("%d%d", &x, &y); 91 | add(x, y + n, 1); 92 | } 93 | g(i, 1, n) add(S, i, 1); 94 | g(i, 1, m) add(i + n, T, 1); 95 | printf("%d\n", dinic()); 96 | g(cur, 1, n) for (int i = head[cur]; i; i = e[i].x) { 97 | // printf("[%d %d\n", cur, e[i].t); 98 | if (e[i].t != S && e[i].w == 0) { 99 | res[cur] = e[i].t - n; 100 | // res[e[i].t] = i; 101 | } 102 | } 103 | g(i, 1, n) printf("%d ", res[i]); puts(""); 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /c++/Graph/binaryCheck.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1121 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | int T; 36 | int n, m; 37 | int u, v; 38 | struct node { 39 | int t, x; 40 | } e[MAXN]; 41 | int head[MAXN], cnt = 1; 42 | inline void add(int s, int t) { 43 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; 44 | e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; 45 | } 46 | int col[MAXN]; 47 | bool bipartite(int u) { 48 | for (int i = head[u]; i; i = e[i].x) { 49 | int v = e[i].t; 50 | if (col[v] == col[u]) return 0; 51 | if (!col[v]) { 52 | col[v] = 3 - col[u]; 53 | if (!bipartite(v)) return 0; 54 | } 55 | } 56 | return 1; 57 | } 58 | bool solve() { 59 | g(i, 1, n) if (!col[i]) { 60 | col[i] = 1; 61 | if (!bipartite(i)) return 0; 62 | } 63 | return 1; 64 | } 65 | int main() { 66 | scanf("%d", &T); 67 | while (T--) { 68 | memset(head, 0, sizeof head); 69 | cnt = 1; 70 | memset(col, 0, sizeof col); 71 | scanf("%d%d", &n, &m); 72 | f(i, 0, m) { 73 | scanf("%d%d", &u, &v); 74 | add(u, v); 75 | } 76 | if (solve()) { 77 | puts("Correct"); 78 | } else { 79 | puts("Wrong"); 80 | } 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /c++/Graph/connectivity/README.md: -------------------------------------------------------------------------------- 1 | # Connectivity 2 | 3 | Talking about Connectivity, we think of scc and bcc. 4 | 5 | For scc: 6 | - Check [scc_tarjan.cpp](scc_tarjan.cpp) 7 | For bcc: 8 | - -------------------------------------------------------------------------------- /c++/Graph/cutBridge.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1183 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #include // NOLINT 12 | #define x1 x11 13 | #define y1 y11 14 | 15 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 16 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 17 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 18 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 19 | 20 | #ifdef WIN32 21 | #define LLD "%I64d" 22 | #define LLU "%I64u" 23 | #else 24 | #define LLD "%lld" 25 | #define LLU "%llu" 26 | #endif 27 | 28 | typedef long long LL;// NOLINT 29 | typedef long double real; 30 | 31 | const double INF = 1e100; 32 | const int oo = ~0u >> 2; 33 | const double pi = acos(-1.0); 34 | const double EPS = 1e-8; 35 | const int MAXN = 100033; 36 | 37 | struct edge { 38 | int s, t, x; 39 | } e[MAXN << 1]; 40 | int head[MAXN], cnt = 1; 41 | inline void add(int s, int t) { 42 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; e[cnt].s = s; 43 | e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; e[cnt].s = t; 44 | } 45 | int low[MAXN], dfn[MAXN], idx; 46 | int cut[MAXN], bri[MAXN]; 47 | typedef std::pair PII; 48 | std::set S; 49 | // void dfs(int u, int fa) { 50 | // // printf("dfs: %d\n", u); 51 | // low[u] = dfn[u] = ++idx; 52 | // int ch = 0, v; 53 | // for (int i = head[u]; i; i = e[i].x) { 54 | // v = e[i].t; 55 | // if (!dfn[v]) { 56 | // ++ch; 57 | // dfs(v, u); 58 | // if (low[v] < low[u]) low[u] = low[v]; 59 | // if (low[v] >= dfn[u]) cut[u] = 1; 60 | // if (low[v] > dfn[u]) { 61 | // // printf("``%d %d:%d %d\n", v, u, low[v], dfn[u]); 62 | // if (u < v) S.insert(PII(u, v)); 63 | // else S.insert(PII(v, u)); 64 | // } 65 | // } else if (dfn[v] < dfn[u] && v != fa) { 66 | // if (dfn[v] < low[u]) low[u] = dfn[v]; 67 | // } 68 | // } 69 | // if (fa < 0 && ch == 1) cut[u] = 0; 70 | // } 71 | int n, m; 72 | int u, v; 73 | bool c; 74 | int vis[MAXN]; 75 | void get(int u, int fa, int dep, int n) { 76 | vis[u] = 1; dfn[u] = low[u] = dep; 77 | int ch = 0; 78 | for (int i = head[u]; i; i = e[i].x) { 79 | if (e[i].t != fa && vis[e[i].t] == 1) { 80 | if (dfn[e[i].t] < low[u]) low[u] = dfn[e[i].t]; 81 | } 82 | if (vis[e[i].t] == 0) { 83 | get(e[i].t, u, dep + 1, n); 84 | ++ch; 85 | if (low[e[i].t] < low[u]) low[u] = low[e[i].t]; 86 | if ((fa == -1 && ch > 1) || (fa != -1 && low[e[i].t] >= dfn[u])) { 87 | cut[u] = 1; 88 | } 89 | if (low[e[i].t] > dfn[u]) { 90 | if (u < e[i].t) { 91 | S.insert(PII(u, e[i].t)); 92 | } else { 93 | S.insert(PII(e[i].t, u)); 94 | } 95 | } 96 | } 97 | } 98 | vis[u] = 2; 99 | } 100 | int main() { 101 | scanf("%d%d", &n, &m); 102 | f(i, 0, m) { 103 | scanf("%d%d", &u, &v); 104 | add(u, v); 105 | } 106 | get(1, -1, 0, n); 107 | // dfs(1, -1); 108 | g(i, 1, n) if (cut[i]) { 109 | printf("%d ", i); 110 | c = 1; 111 | } 112 | if (c) { 113 | puts(""); 114 | } else { 115 | puts("Null"); 116 | } 117 | for (std::set::iterator it = S.begin(); it != S.end(); ++it) { 118 | printf("%d %d\n", it->first, it->second); 119 | } 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /c++/Graph/cutBridge_lrj.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1183 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #include // NOLINT 12 | #define x1 x11 13 | #define y1 y11 14 | 15 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 16 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 17 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 18 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 19 | 20 | #ifdef WIN32 21 | #define LLD "%I64d" 22 | #define LLU "%I64u" 23 | #else 24 | #define LLD "%lld" 25 | #define LLU "%llu" 26 | #endif 27 | 28 | typedef long long LL;// NOLINT 29 | typedef long double real; 30 | 31 | const double INF = 1e100; 32 | const int oo = ~0u >> 2; 33 | const double pi = acos(-1.0); 34 | const double EPS = 1e-8; 35 | const int MAXN = 100033; 36 | 37 | struct edge { 38 | int s, t, x; 39 | } e[MAXN << 1]; 40 | int head[MAXN], cnt = 1; 41 | inline void add(int s, int t) { 42 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; e[cnt].s = s; 43 | e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; e[cnt].s = t; 44 | } 45 | int low[MAXN], dfn[MAXN], idx; 46 | int cut[MAXN], bri[MAXN]; 47 | typedef std::pair PII; 48 | std::set S; 49 | void dfs(int u, int fa) { 50 | // printf("dfs: %d\n", u); 51 | low[u] = dfn[u] = ++idx; 52 | int ch = 0, v; 53 | for (int i = head[u]; i; i = e[i].x) { 54 | v = e[i].t; 55 | if (!dfn[v]) { 56 | ++ch; 57 | dfs(v, u); 58 | if (low[v] < low[u]) low[u] = low[v]; 59 | if (low[v] >= dfn[u]) cut[u] = 1; 60 | if (low[v] > dfn[u]) { 61 | // printf("``%d %d:%d %d\n", v, u, low[v], dfn[u]); 62 | if (u < v) { 63 | S.insert(PII(u, v)); 64 | } else { 65 | S.insert(PII(v, u)); 66 | } 67 | } 68 | } else if (dfn[v] < dfn[u] && v != fa) { 69 | if (dfn[v] < low[u]) low[u] = dfn[v]; 70 | } 71 | } 72 | if (fa < 0 && ch == 1) cut[u] = 0; 73 | } 74 | int n, m; 75 | int u, v; 76 | bool c; 77 | // int vis[MAXN]; 78 | // void get(int u, int fa, int dep, int n) { 79 | // vis[u] = 1; dfn[u] = low[u] = dep; 80 | // int ch = 0; 81 | // for (int i = head[u]; i; i = e[i].x) { 82 | // if (e[i].t != fa && vis[e[i].t] == 1) { 83 | // if (dfn[e[i].t] < low[u]) low[u] = dfn[e[i].t]; 84 | // } 85 | // if (vis[e[i].t] == 0) { 86 | // get(e[i].t, u, dep + 1, n); 87 | // ++ch; 88 | // if (low[e[i].t] < low[u]) low[u] = low[e[i].t]; 89 | // if ((fa == -1 && ch > 1) || (fa != -1 && low[e[i].t] >= dfn[u])) cut[u] = 1; NOLINT 90 | // if (low[e[i].t] > dfn[u]) { 91 | // if (u < e[i].t) S.insert(PII(u, e[i].t)); 92 | // else S.insert(PII(e[i].t, u)); 93 | // } 94 | // } 95 | // } 96 | // vis[u] = 2; 97 | // } 98 | int main() { 99 | scanf("%d%d", &n, &m); 100 | f(i, 0, m) { 101 | scanf("%d%d", &u, &v); 102 | add(u, v); 103 | } 104 | // get(1, -1, 0, n); 105 | dfs(1, -1); 106 | g(i, 1, n) if (cut[i]) { 107 | printf("%d ", i); 108 | c = 1; 109 | } 110 | if (c) { 111 | puts(""); 112 | } else { 113 | puts("Null"); 114 | } 115 | for (std::set::iterator it = S.begin(); it != S.end(); ++it) { 116 | printf("%d %d\n", it->first, it->second); 117 | } 118 | // g(i, 1, n) printf("%d ", dfn[i]); puts(""); 119 | // g(i, 1, n) printf("%d ", low[i]); puts(""); 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /c++/Graph/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name dijkstra.cpp 3 | * @subject Dijkstra algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright SATA 6 | * @comment This is a sssp algorithm. 7 | * @tests http://hihocoder.com/problemset/problem/1081 8 | * @warning using 0x1f to memset for inf 9 | */ 10 | #include // NOLINT 11 | #include // NOLINT 12 | #include // NOLINT 13 | #include // NOLINT 14 | #define x1 x11 15 | #define y1 y11 16 | 17 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 18 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 19 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 20 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 21 | 22 | #ifdef WIN32 23 | #define LLD "%I64d" 24 | #define LLU "%I64u" 25 | #else 26 | #define LLD "%lld" 27 | #define LLU "%llu" 28 | #endif 29 | 30 | typedef long long LL;// NOLINT 31 | typedef long double real; 32 | 33 | const double INF = 1e100; 34 | const int oo = ~0u >> 2; 35 | const double pi = acos(-1.0); 36 | const double EPS = 1e-8; 37 | const int MAXN = 100033; 38 | 39 | struct node { 40 | int t, w, x; 41 | } e[MAXN]; 42 | int head[MAXN], cnt = 1; 43 | inline void add(int s, int t, int w) { 44 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; e[cnt].w = w; 45 | e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; e[cnt].w = w; 46 | } 47 | int n, m, s, t; 48 | typedef std::pair PII;// NOLINT 49 | std::priority_queue, std::greater > Q;// NOLINT 50 | int dis[MAXN], vis[MAXN]; 51 | void dij() { 52 | while (!Q.empty()) Q.pop(); 53 | memset(dis, 0x1f, sizeof dis); 54 | memset(vis, 0, sizeof vis); 55 | dis[s] = 0; 56 | Q.push(PII(0, s)); 57 | PII tee; 58 | int u; 59 | while (!Q.empty()) { 60 | tee = Q.top(); Q.pop(); 61 | u = tee.second; 62 | if (vis[u]) continue; 63 | vis[u] = 1; 64 | for (int i = head[u]; i; i = e[i].x) { 65 | if (dis[e[i].t] > dis[u] + e[i].w) { 66 | dis[e[i].t] = dis[u] + e[i].w; 67 | Q.push(PII(dis[e[i].t], e[i].t)); 68 | } 69 | } 70 | } 71 | printf("%d\n", dis[t]); 72 | } 73 | int u, v, w; 74 | int main() { 75 | scanf("%d%d%d%d", &n, &m, &s, &t); 76 | f(i, 0, m) { 77 | scanf("%d%d%d", &u, &v, &w); 78 | add(u, v, w); 79 | } 80 | dij(); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /c++/Graph/dinic.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://poj.org/problem?id=1273 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | struct node { 37 | int t, w, x; 38 | } e[MAXN * 20]; 39 | int head[MAXN], cnt = 1; 40 | inline void add(int s, int t, int w) { 41 | e[++cnt].t = t; e[cnt].w = w; e[cnt].x = head[s]; head[s] = cnt; 42 | e[++cnt].t = s; e[cnt].w = 0; e[cnt].x = head[t]; head[t] = cnt; 43 | } 44 | std::queue Q; 45 | int dis[MAXN]; 46 | int n, m; 47 | int u, v, w; 48 | bool bfs() { 49 | while (!Q.empty()) Q.pop(); 50 | int x; 51 | Q.push(1); 52 | memset(dis, -1, sizeof dis); 53 | dis[1] = 0; 54 | while (!Q.empty()) { 55 | u = Q.front(); Q.pop(); 56 | for (int i = head[u]; i; i = e[i].x) { 57 | if (e[i].w && dis[e[i].t] == -1) { 58 | dis[e[i].t] = dis[u] + 1; 59 | Q.push(e[i].t); 60 | } 61 | } 62 | } 63 | return dis[n] != -1; 64 | } 65 | int dfs(int x, int fl) { 66 | if (x == n) return fl; 67 | int w, used = 0; 68 | for (int i = head[x]; i; i = e[i].x) { 69 | if (e[i].w && dis[e[i].t] == dis[x] + 1) { 70 | w = dfs(e[i].t, std::min(fl - used, e[i].w)); 71 | e[i].w -= w; e[i ^ 1].w += w; used += w; 72 | if (used == fl) return fl; 73 | } 74 | } 75 | if (!used) dis[x] = -1; 76 | return used; 77 | } 78 | void dinic() { 79 | int ans = 0; 80 | while (bfs()) ans += dfs(1, oo); 81 | printf("%d\n", ans); 82 | } 83 | int main() { 84 | while (~scanf("%d%d", &m, &n)) { 85 | memset(head, 0, sizeof head); cnt = 1; 86 | f(i, 0, m) { 87 | scanf("%d%d%d", &u, &v, &w); 88 | add(u, v, w); 89 | } 90 | dinic(); 91 | } 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /c++/Graph/divide.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://poj.org/problem?id=1741 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | int n, k; 36 | int sum, ans, root; 37 | int dep[MAXN], d[MAXN], f[MAXN], sz[MAXN]; 38 | bool vis[MAXN]; 39 | struct node { 40 | int t, w, x; 41 | } e[MAXN << 1]; 42 | int head[MAXN], cnt = 1; 43 | inline void add(int s, int t, int w) { 44 | e[++cnt].t = t; e[cnt].w = w; e[cnt].x = head[s]; head[s] = cnt; 45 | e[++cnt].t = s; e[cnt].w = w; e[cnt].x = head[t]; head[t] = cnt; 46 | } 47 | void getRoot(int x, int fa) { 48 | sz[x] = 1; f[x] = 0; 49 | for (int i = head[x]; i; i = e[i].x) { 50 | if (e[i].t == fa || vis[e[i].t]) continue; 51 | getRoot(e[i].t, x); 52 | sz[x] += sz[e[i].t]; 53 | f[x] = std::max(f[x], sz[e[i].t]); 54 | } 55 | f[x] = std::max(f[x], sum - sz[x]); 56 | if (f[x] < f[root]) root = x; 57 | } 58 | void getDep(int x, int fa) { 59 | dep[++dep[0]] = d[x]; 60 | for (int i = head[x]; i; i = e[i].x) { 61 | if (e[i].t == fa || vis[e[i].t]) continue; 62 | d[e[i].t] = d[x] + e[i].w; 63 | getDep(e[i].t, x); 64 | } 65 | } 66 | int calc(int x, int now) { 67 | d[x] = now; dep[0] = 0; 68 | getDep(x, 0); 69 | std::sort(dep + 1, dep + dep[0] + 1); 70 | int t = 0, l, r; 71 | for (l = 1, r = dep[0]; l < r;) { 72 | if (dep[l] + dep[r] <= k) { 73 | t += r - l; ++l; 74 | } else { 75 | --r; 76 | } 77 | } 78 | return t; 79 | } 80 | void work(int x) { 81 | ans += calc(x, 0); 82 | vis[x] = 1; 83 | for (int i = head[x]; i; i = e[i].x) { 84 | if (vis[e[i].t]) continue; 85 | ans -= calc(e[i].t, e[i].w); 86 | sum = sz[e[i].t]; 87 | root = 0; 88 | getRoot(e[i].t, 0); 89 | work(root); 90 | } 91 | } 92 | int s, t, w; 93 | int main() { 94 | while (233) { 95 | ans = 0; root = 0; cnt = 0; 96 | scanf("%d%d", &n, &k); 97 | if (!n) break; 98 | memset(vis, 0, sizeof vis); 99 | memset(head, 0, sizeof head); 100 | f(i, 1, n) { 101 | scanf("%d%d%d", &s, &t, &w); 102 | add(s, t, w); 103 | } 104 | sum = n; f[0] = ~0u >> 2; 105 | getRoot(1, 0); 106 | work(root); 107 | printf("%d\n", ans); 108 | } 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /c++/Graph/eulerRoute.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://uoj.ac/problem/117 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | struct node { 36 | int t, w, x; 37 | } e[MAXN << 2]; 38 | int head[MAXN], cnt = 1; 39 | int out[MAXN], in[MAXN]; 40 | inline void add(int s, int t, int w) { 41 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; e[cnt].w = w; 42 | ++out[s]; ++in[t]; 43 | } 44 | bool vis[MAXN << 2]; 45 | int sta[MAXN << 2], top; 46 | void dfs(int x) { 47 | for (int i = head[x]; i; i = head[x]) { 48 | while (i && vis[abs(e[i].w)]) i = e[i].x; 49 | if (!i) return; 50 | vis[abs(e[i].w)] = 1; 51 | head[x] = e[i].x; 52 | dfs(e[i].t); 53 | sta[top++] = e[i].w; 54 | } 55 | } 56 | int n, m, tp; 57 | int x, y; 58 | int main() { 59 | scanf("%d%d%d", &tp, &n, &m); 60 | g(i, 1, m) { 61 | scanf("%d%d", &x, &y); 62 | add(x, y, i); 63 | if (tp == 1) add(y, x, -i); 64 | } 65 | g(i, 1, n) { 66 | if (in[i] != out[i] || (tp == 1 && in[i] % 2)) { 67 | puts("NO"); 68 | return 0; 69 | } 70 | } 71 | dfs(e[cnt].t); 72 | g(i, 1, m) if (!vis[i]) { 73 | puts("NO"); 74 | return 0; 75 | } 76 | dfs(e[cnt].t); 77 | puts("YES"); 78 | gd(i, top - 1, 0) printf("%d ", sta[i]); 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /c++/Graph/floyd.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1089 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | int d[133][133]; 37 | int n, m, u, v, w; 38 | int main() { 39 | scanf("%d%d", &n, &m); 40 | memset(d, 0x1f, sizeof d); 41 | f(i, 0, m) { 42 | scanf("%d%d%d", &u, &v, &w); 43 | d[u][v] = std::min(d[u][v], w); 44 | d[v][u] = std::min(d[v][u], w); 45 | } 46 | g(i, 1, n) d[i][i] = 0; 47 | g(k, 1, n) g(i, 1, n) g(j, 1, n) { 48 | d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]); 49 | } 50 | g(i, 1, n) { 51 | g(j, 1, n) printf("%d ", d[i][j]); puts(""); 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /c++/Graph/konig.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1127 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #include // NOLINT 12 | #define x1 x11 13 | #define y1 y11 14 | 15 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 16 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 17 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 18 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 19 | 20 | #ifdef WIN32 21 | #define LLD "%I64d" 22 | #define LLU "%I64u" 23 | #else 24 | #define LLD "%lld" 25 | #define LLU "%llu" 26 | #endif 27 | 28 | typedef long long LL;// NOLINT 29 | typedef long double real; 30 | 31 | const double INF = 1e100; 32 | const int oo = ~0u >> 2; 33 | const double pi = acos(-1.0); 34 | const double EPS = 1e-8; 35 | const int MAXN = 100033; 36 | 37 | int n, m; 38 | int u, v; 39 | struct node { 40 | int w, t, x; 41 | } e[MAXN << 1]; 42 | int head[MAXN], cnt = 1; 43 | inline void add(int s, int t, int w) { 44 | // printf("``%d %d\n", s, t); 45 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; e[cnt].w = w; 46 | e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; e[cnt].w = 0; 47 | } 48 | std::queue Q; 49 | int dis[MAXN]; 50 | int S, T; 51 | bool bfs() { 52 | memset(dis, -1, sizeof dis); 53 | Q.push(S); dis[S] = 0; 54 | int u; 55 | while (!Q.empty()) { 56 | u = Q.front(); Q.pop(); 57 | for (int i = head[u]; i; i = e[i].x) { 58 | if (e[i].w && dis[e[i].t] == -1) { 59 | dis[e[i].t] = dis[u] + 1; 60 | Q.push(e[i].t); 61 | } 62 | } 63 | } 64 | return dis[T] != -1; 65 | } 66 | int dfs(int x, int fl) { 67 | if (x == T) return fl; 68 | int w, used = 0; 69 | for (int i = head[x]; i; i = e[i].x) { 70 | if (e[i].w && dis[e[i].t] == dis[x] + 1) { 71 | w = dfs(e[i].t, std::min(e[i].w, fl - used)); 72 | e[i].w -= w; e[i ^ 1].w += w; used += w; 73 | if (used == fl) return fl; 74 | } 75 | } 76 | if (!used) dis[x] = -1; 77 | return used; 78 | } 79 | int dinic() { 80 | int res = 0; 81 | while (bfs()) res += dfs(S, oo); 82 | return res; 83 | } 84 | int is[MAXN]; 85 | void init(int s) { 86 | // printf("??%d\n", s); 87 | while (!Q.empty()) Q.pop(); 88 | // memset(is, -1, sizeof is); 89 | Q.push(s); is[s] = 0; 90 | int u; 91 | while (!Q.empty()) { 92 | u = Q.front(); Q.pop(); 93 | for (int i = head[u]; i; i = e[i].x) { 94 | if (e[i].w && is[e[i].t] == -1) { 95 | is[e[i].t] = is[u] ^ 1; 96 | Q.push(e[i].t); 97 | } 98 | } 99 | } 100 | } 101 | int main() { 102 | scanf("%d%d", &n, &m); 103 | S = n + n + 2; T = S + 1; 104 | f(i, 0, m) { 105 | scanf("%d%d", &u, &v); 106 | add(u, v, 1); 107 | add(v, u, 1); 108 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11 109 | // undirected 110 | } 111 | memset(is, -1, sizeof is); 112 | g(i, 1, n) if (is[i] == -1) init(i); 113 | g(i, 1, n) { 114 | if (is[i]) { 115 | add(S, i, 1); 116 | add(i, S, 1); 117 | } else { 118 | add(i, T, 1); 119 | add(T, i, 1); 120 | } 121 | } 122 | int res = dinic(); 123 | printf("%d\n%d\n", res, n - res); 124 | return 0; 125 | } 126 | /* 127 | 3 7 128 | 1 8 129 | 5 9 130 | 1 10 131 | S 1 132 | S 2 133 | S 3 134 | S 4 135 | S 5 136 | 6 T 137 | 7 T 138 | 8 T 139 | 9 T 140 | 10 T 141 | */ 142 | -------------------------------------------------------------------------------- /c++/Graph/mcf.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://www.lydsy.com/JudgeOnline/problemset.php?search=1834 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | int n, m, k; 37 | int dis[MAXN]; 38 | bool inq[MAXN]; 39 | struct node { 40 | int s, w, t, c, q, x; 41 | } e[MAXN]; 42 | int head[MAXN], cnt = 1; 43 | inline void add(int s, int t, int w, int c) { 44 | e[++cnt].s = s; e[cnt].t = t; e[cnt].w = w; e[cnt].q = c; e[cnt].x = head[s]; head[s] = cnt;// NOLINT 45 | e[++cnt].s = t; e[cnt].t = s; e[cnt].w = 0; e[cnt].q = -c; e[cnt].x = head[t]; head[t] = cnt;// NOLINT 46 | } 47 | inline void add2(int s, int t, int w, int c) { 48 | e[++cnt].s = s; e[cnt].t = t; e[cnt].w = w; e[cnt].c = c; e[cnt].x = head[s]; head[s] = cnt;// NOLINT 49 | e[++cnt].s = t; e[cnt].t = s; e[cnt].w = 0; e[cnt].c = -c; e[cnt].x = head[t]; head[t] = cnt;// NOLINT 50 | } 51 | void upd() { 52 | int t = cnt; 53 | for (int i = 2; i <= t; i += 2) add2(e[i].s, e[i].t, k, e[i].q); 54 | } 55 | std::queue Q; 56 | int S, T; 57 | bool bfs() { 58 | memset(dis, -1, sizeof dis); 59 | while (!Q.empty()) Q.pop(); 60 | int u; 61 | Q.push(S); dis[S] = 0; 62 | while (!Q.empty()) { 63 | u = Q.front(); Q.pop(); 64 | for (int i = head[u]; i; i = e[i].x) { 65 | if (e[i].w && dis[e[i].t] == -1) { 66 | dis[e[i].t] = dis[u] + 1; 67 | Q.push(e[i].t); 68 | } 69 | } 70 | } 71 | return dis[T] != -1; 72 | } 73 | int dfs(int x, int fl) { 74 | if (x == n) return fl; 75 | int w, used = 0; 76 | for (int i = head[x]; i; i = e[i].x) { 77 | if (e[i].w && dis[e[i].t] == dis[x] + 1) { 78 | w = dfs(e[i].t, std::min(e[i].w, fl - used)); 79 | e[i].w -= w; e[i ^ 1].w += w; used += w; 80 | if (used == fl) return fl; 81 | } 82 | } 83 | if (!used) dis[x] = -1; 84 | return used; 85 | } 86 | int ans; 87 | void dinic() { 88 | while (bfs()) ans += dfs(S, oo); 89 | } 90 | int a[MAXN], pre[MAXN]; 91 | bool spfa() { 92 | memset(dis, 0x1f, sizeof dis); 93 | memset(inq, 0, sizeof inq); 94 | int mx = dis[0]; a[0] = mx; 95 | dis[0] = 0; 96 | while (!Q.empty()) Q.pop(); 97 | Q.push(0); inq[0] = 1; 98 | int u; 99 | while (!Q.empty()) { 100 | u = Q.front(); Q.pop(); 101 | for (int i = head[u]; i; i = e[i].x) { 102 | if (e[i].w && dis[e[i].t] > dis[u] + e[i].c) { 103 | dis[e[i].t] = dis[u] + e[i].c; 104 | a[e[i].t] = std::min(a[u], e[i].w); 105 | pre[e[i].t] = i; 106 | if (!inq[e[i].t]) { 107 | Q.push(e[i].t); 108 | inq[e[i].t] = 1; 109 | } 110 | } 111 | } 112 | inq[u] = 0; 113 | } 114 | return dis[n] != mx; 115 | } 116 | void mcf() { 117 | ans += dis[n] * a[n]; 118 | int u = n; 119 | while (u) { 120 | e[ pre[u] ].w -= a[n]; 121 | e[ pre[u] ^ 1 ].w += a[n]; 122 | u = e[ pre[u] ].s; 123 | } 124 | } 125 | int u, v, w, c; 126 | int main() { 127 | scanf("%d%d%d", &n, &m, &k); 128 | g(i, 1, m) { 129 | scanf("%d%d%d%d", &u, &v, &w, &c); 130 | add(u, v, w, c); 131 | } 132 | S = 1; T = n; 133 | dinic(); 134 | printf("%d ", ans); 135 | ans = 0; 136 | upd(); 137 | add(0, 1, k, 0); 138 | while (spfa()) mcf(); 139 | printf("%d\n", ans); 140 | return 0; 141 | } 142 | -------------------------------------------------------------------------------- /c++/Graph/spfa.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1093 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | struct edeg { 37 | int t, w, x; 38 | } e[MAXN * 20]; 39 | int head[MAXN], cnt = 1; 40 | inline void add(int s, int t, int w) { 41 | e[++cnt].t = t; e[cnt].w = w; e[cnt].x = head[s]; head[s] = cnt; 42 | e[++cnt].t = s; e[cnt].w = w; e[cnt].x = head[t]; head[t] = cnt; 43 | } 44 | int n, m; 45 | LL ans; 46 | int dp[MAXN], ti[MAXN]; 47 | bool vis[MAXN]; 48 | std::queue Q; 49 | int u, v, s, t, w; 50 | void spfa() { 51 | memset(dp, 0x1f, sizeof dp); 52 | Q.push(s); dp[s] = 0; 53 | int x; 54 | while (!Q.empty()) { 55 | x = Q.front(); Q.pop(); 56 | for (int i = head[x]; i; i = e[i].x) { 57 | if (dp[e[i].t] > dp[x] + e[i].w) { 58 | dp[e[i].t] = dp[x] + e[i].w; 59 | if (++ti[e[i].t] > n) { 60 | exit(0); 61 | } 62 | if (!vis[e[i].t]) { 63 | vis[e[i].t] = 1; 64 | Q.push(e[i].t); 65 | } 66 | } 67 | } 68 | vis[x] = 0; 69 | } 70 | printf("%d\n", dp[t]); 71 | } 72 | int main() { 73 | scanf("%d%d%d%d", &n, &m, &s, &t); 74 | f(i, 0, m) { 75 | scanf("%d%d%d", &u, &v, &w); 76 | add(u, v, w); 77 | } 78 | spfa(); 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /c++/Graph/toposort.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1174 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | int T; 37 | int n, m; 38 | struct node { 39 | int t, x; 40 | } e[MAXN * 10]; 41 | int head[MAXN], cnt = 1; 42 | int u, v; 43 | inline void add(int s, int t) { 44 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; 45 | // e[++cnt].t = s; e[cnt].x = head[t]; head[t] = cnt; 46 | } 47 | int in[MAXN], done; 48 | std::queue Q; 49 | int main() { 50 | scanf("%d", &T); 51 | while (T--) { 52 | memset(head, 0, sizeof head); cnt = 1; done = 0; 53 | memset(in, 0, sizeof in); 54 | scanf("%d%d", &n, &m); 55 | f(i, 0, m) { 56 | scanf("%d%d", &u, &v); 57 | add(u, v); 58 | ++in[v]; 59 | } 60 | g(i, 1, n) if (!in[i]) { 61 | Q.push(i); 62 | } 63 | while (!Q.empty()) { 64 | u = Q.front(); Q.pop(); 65 | ++done; 66 | for (int i = head[u]; i; i = e[i].x) { 67 | if (--in[ e[i].t ] == 0) Q.push(e[i].t); 68 | } 69 | } 70 | // printf("%d\n", done); 71 | if (done == n) { 72 | puts("Correct"); 73 | } else { 74 | puts("Wrong"); 75 | } 76 | } 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /c++/Math/ext_gcd/README.md: -------------------------------------------------------------------------------- 1 | # ext_gcd 2 | 3 | This ext_gcd algorithm is an extended gcd algorithm. 4 | 5 | It deals with equations like `ax mod b = c`. 6 | 7 | To solve that, we solve `ax - by = c`, where y is an integer. 8 | 9 | Can be implemented like [ext_gcd.cpp](ext_gcd.cpp) way. 10 | 11 | Tutorials on the [test](http://poj.org/problem?id=1061) can be found [here](http://www.cnblogs.com/rainydays/archive/2013/07/19/3201618.html). -------------------------------------------------------------------------------- /c++/Math/ext_gcd/ext_gcd.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name ext_gcd.cpp 3 | * @subject extended_GCD algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a extended_GCD algorithm. 7 | * @tests http://poj.org/problem?id=1061 8 | * @warning Added NOLINT on line 12 and 28 in order to ignore 9 | * strange tastes on (int64 && long long) and (reference and pointer). 10 | */ 11 | #include 12 | 13 | typedef long long LL;// NOLINT 14 | 15 | LL x, y, m, n, L; 16 | // Using variables the same as the problem statement. 17 | LL a, b, c, d, xx, yy; 18 | 19 | /** 20 | * ext_gcd 21 | * @param a [LL] 22 | * @param b [LL] 23 | * @param xx [LL &] 24 | * @param yy [LL &] 25 | * @return gcd of a and b [LL] 26 | * @comment this function solves things like 'a * xx + b * yy = gcd(a,b)' 27 | * and returns gcd(a,b); 28 | */ 29 | LL ext_gcd(LL a, LL b, LL &xx, LL &yy) {// NOLINT 30 | if (b) { 31 | c = ext_gcd(b, a % b, yy, xx); 32 | yy -= xx * (a / b); 33 | // You know what, the '()' here is essential. 34 | // Or you may want to use 'yy -= a / b * xx;' instead. 35 | // The reason here is that a might not be divisable by b 36 | // and from our math work we know what we want here 37 | // is 'a div b'. 38 | return c; 39 | } 40 | xx = 1; yy = 0; 41 | return a; 42 | } 43 | 44 | int main() { 45 | scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L); 46 | d = ext_gcd(n - m, L, xx, yy); 47 | if ((x - y) % d) { 48 | puts("Impossible"); 49 | } else { 50 | xx = xx * ((x - y) / d); 51 | yy = L / d; 52 | xx = (xx % yy + yy) % yy; 53 | printf("%lld\n", xx); 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /c++/Math/gcd/README.md: -------------------------------------------------------------------------------- 1 | # gcd 2 | 3 | This gcd algorithm solves things called the greatest-common-divisor. 4 | 5 | Can be implemented in both [recursive](gcd_recursize.cpp) and [non_recursive](gcd_non_recursize.cpp) way. -------------------------------------------------------------------------------- /c++/Math/gcd/gcd_non_recursive.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name gcd_non_recursive.cpp 3 | * @subject GCD algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a non_recursive GCD algorithm. 7 | * @tests http://codevs.cn/problem/1212/ 8 | */ 9 | 10 | #include 11 | 12 | int a, b, c; 13 | /** 14 | * gcd 15 | * @param a [int] 16 | * @param b [int] 17 | * @return greatest_common_divisor of a and b [int] 18 | */ 19 | 20 | int gcd(int a, int b) { 21 | while (b) { 22 | c = a % b; 23 | a = b; b = c; 24 | } 25 | return a; 26 | } 27 | 28 | int main() { 29 | scanf("%d%d", &a, &b); 30 | printf("%d\n", gcd(a, b)); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /c++/Math/gcd/gcd_recursive.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name gcd_recursive.cpp 3 | * @subject GCD algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a recursive GCD algorithm. 7 | * @tests http://codevs.cn/problem/1212/ 8 | */ 9 | #include 10 | 11 | int a, b; 12 | /** 13 | * gcd 14 | * @param a [int] 15 | * @param b [int] 16 | * @return greatest_common_divisor of a and b [int] 17 | */ 18 | int gcd(int a, int b) { 19 | if (b)return gcd(b, a % b); 20 | return a; 21 | } 22 | 23 | int main() { 24 | scanf("%d%d", &a, &b); 25 | printf("%d\n", gcd(a, b)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c++/Math/matrix/README.md: -------------------------------------------------------------------------------- 1 | # Matrix -------------------------------------------------------------------------------- /c++/Math/prime/README.md: -------------------------------------------------------------------------------- 1 | # Prime 2 | 3 | This collection of prime-related algorithms provides you with ways to solve these problems: 4 | 5 | 1. Is it a prime? 6 | 2. I need a primelist. 7 | 8 | Check this [euler_primelist_generating.cpp](euler_primelist_generating.cpp). 9 | 10 | Check this [prime_validating.cpp](prime_validating.cpp).(Includes Miller_Rabin algorithm) 11 | -------------------------------------------------------------------------------- /c++/Math/prime/euler_primelist_generating.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name euler_primelist_generating.cpp 3 | * @subject euler_primelist 4 | * @author CJSoft (egwcyh@qq.com) 5 | * @Copyright DWTFABG 6 | * @comment returns a vector containing primes from 2 to upperBound 7 | * @tests http://codevs.cn/problem/2741/ 8 | */ 9 | 10 | /** 11 | * Need memory.h or cstring for memset func. 12 | * Need vector for whatever... 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | std::vector genPrime(int upperBound) { 20 | bool *isPrime = new bool[upperBound+7]; 21 | int *prime = new int[upperBound+7]; 22 | memset(isPrime, true, upperBound+7); 23 | memset(prime, 0, (upperBound+7)*4); 24 | int cursor = 0; 25 | for (int i = 2; i <= upperBound; ++i) { 26 | if (isPrime[i])prime[cursor++] = i; 27 | for (int j = 0; (j < cursor) && (i*prime[j] <= upperBound); ++j) { 28 | isPrime[i*prime[j]] = false; 29 | if (i%prime[j] == 0)break; 30 | } 31 | } 32 | std::vector vec_rtn(prime, prime+cursor); 33 | return vec_rtn; 34 | } 35 | int n; 36 | int main() { 37 | std::vector p = genPrime(1000); 38 | scanf("%d", &n); 39 | for (int i = 0; i < p.size(); ++i) { 40 | if (p[i] > n)return 0; 41 | printf("%d ", p[i]); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /c++/Math/prime/prime_validating.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name prime_validating.cpp 3 | * @subject prime_validating algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment These are prime_validating algorithms, including brutal and Miler_Rabin. 7 | * @tests http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1106 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | 17 | int n, m, t; 18 | bool flag; 19 | bool check_brutal(int t) { 20 | m = sqrt(t); 21 | flag = 1; 22 | g(i, 2, m) { 23 | if (t % i == 0) { 24 | return 0; 25 | } 26 | } 27 | return 1; 28 | } 29 | 30 | typedef long long LL;// NOLINT 31 | 32 | int pow(int a, int b, int c) { 33 | int res = 1, bas = a; 34 | while (b) { 35 | if (b & 1)res = (LL)res * bas % c; 36 | // Transform to long long in case of overflow. 37 | bas = bas * bas % c; 38 | b >>= 1; 39 | } 40 | return res; 41 | } 42 | 43 | /*bool check_miller_rabin(int t) { 44 | if (t == 1)return 0; 45 | if (t == 2)return 1; 46 | if (t == 3)return 1; 47 | int s = 12; 48 | srand(time(0)); 49 | // s is a chosen number according to this algorithm, 50 | // it might change as your dataset changes. 51 | int tee = 0; 52 | g(i, 1, s) { 53 | tee = rand() % (t - 2) + 2; 54 | if (pow(tee, t - 1, t) != 1)return 0; 55 | } 56 | return 1; 57 | }*/ 58 | 59 | int main() { 60 | scanf("%d", &n); 61 | while (n--) { 62 | scanf("%d", &t); 63 | if (check_brutal(t)) { 64 | puts("Yes"); 65 | } else { 66 | puts("No"); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /c++/Math/quick_pow/README.md: -------------------------------------------------------------------------------- 1 | # Quick_Pow 2 | 3 | Quick_Pow allows you to get (a ^ n) % b quickly. 4 | 5 | See [quick_pow.cpp](quick_pow.cpp) for details. 6 | -------------------------------------------------------------------------------- /c++/Math/quick_pow/quick_pow.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name quick_pow.cpp 3 | * @subject quick_pow algorithm 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is an algorithm for powing quickly. 7 | * @tests https://vijos.org/p/1841 (NOIP2013 TG D1T1) 8 | * @warning Added NOLINT on line 23 to ignore long long stuff in cpplint. 9 | */ 10 | 11 | #include 12 | 13 | int n, m, k, x; 14 | 15 | /** 16 | * pow 17 | * @param a [int] 18 | * @param b [int] 19 | * @param c [int] 20 | * @return a ^ b % c [int] 21 | */ 22 | 23 | typedef long long LL;// NOLINT 24 | 25 | int pow(int a, int b, int c) { 26 | int res = 1, bas = a; 27 | while (b) { 28 | if (b & 1)res = (LL)res * bas % c; 29 | // Transform to long long in case of overflow. 30 | bas = bas * bas % c; 31 | b >>= 1; 32 | } 33 | return res; 34 | } 35 | 36 | int main() { 37 | scanf("%d%d%d%d", &n, &m, &k, &x); 38 | printf("%d", (x + (LL)m * pow(10, k, n) % n) % n); 39 | } 40 | -------------------------------------------------------------------------------- /c++/README.md: -------------------------------------------------------------------------------- 1 | # Standard_Code_Library for OI and ICPC [![Build Status](https://img.shields.io/travis/24OI/Standard_Code_Library.svg?style=flat-square)](https://github.com/24OI/Standard_Code_Library) 2 | 3 | Standard_Code_Library, a.k.a, Templates. [![Join the chat at https://gitter.im/24OI/Standard_Code_Library](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/24OI/Standard_Code_Library?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | This library is especially for OIers and ICPCers in their journey exploring the area of competitive programming. 6 | 7 | **Note that we only have C++ codes at the moment, add your favorite languages if you want!** 8 | 9 | ## What's Included? 10 | 11 | :triangular_flag_on_post: Nothing much at the moment... 12 | 13 | Check out [#2](https://github.com/24OI/Standard_Code_Library/issues/2) for more details on current status. 14 | 15 | ## Using? 16 | 17 | Note that we are using DWTFABG License so just **DO WHAT THE FUCK AND BE GOOD** 18 | 19 | Hope you enjoy learning through this repo. 20 | 21 | It is strongly recommended to mention the original author while using the codes in this repo. 22 | 23 | ## Contributing? 24 | 25 | See [CONTRIBUTING.md](CONTRIBUTING.md). 26 | 27 | You know, there are always many ways to implement some algorithms or data structures. 28 | 29 | Yours are always welcome. 30 | 31 | ## Contributors? 32 | 33 | :+1: See [Contributors Page](https://github.com/24OI/Standard_Code_Library/graphs/contributors) collected by Github. 34 | -------------------------------------------------------------------------------- /c++/String/acAutomaton.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://acm.hdu.edu.cn/showproblem.php?pid=2222 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 1000033; 35 | 36 | int T; 37 | int n; 38 | char buf[MAXN]; 39 | struct node { 40 | node *fail, *ch[26]; 41 | int cnt; 42 | } pool[MAXN], *tail; 43 | inline void init() { 44 | tail = pool; 45 | } 46 | inline node* newnode() { 47 | node* ans = tail++; 48 | ans->fail = NULL; ans->cnt = 0; 49 | memset(ans->ch, 0, sizeof ans->ch); 50 | return ans; 51 | } 52 | void insert(node* root, const char* str) { 53 | node* p = root; 54 | int idx; 55 | f(i, 0, strlen(str)) { 56 | idx = str[i] - 'a'; 57 | if (p->ch[idx] == NULL) p->ch[idx] = newnode(); 58 | p = p->ch[idx]; 59 | } 60 | ++p->cnt; 61 | } 62 | std::queue Q; 63 | void getFail(node* root) { 64 | node* p = root; 65 | while (!Q.empty()) Q.pop(); 66 | root->fail = NULL; 67 | Q.push(root); 68 | while (!Q.empty()) { 69 | node* tee = Q.front(); Q.pop(); 70 | node* p = NULL; 71 | f(i, 0, 26) { 72 | if (tee->ch[i]) { 73 | p = tee->fail; 74 | while (p) { 75 | if (p->ch[i]) { 76 | tee->ch[i]->fail = p->ch[i]; 77 | break; 78 | } 79 | p = p->fail; 80 | } 81 | if (p == NULL) tee->ch[i]->fail = root; 82 | Q.push(tee->ch[i]); 83 | } 84 | } 85 | } 86 | } 87 | int query(node* root, const char* str) { 88 | node* p = root; 89 | int idx, ans = 0; 90 | f(i, 0, strlen(str)) { 91 | idx = str[i] - 'a'; 92 | while (p->ch[idx] == NULL && p != root) p = p->fail; 93 | p = p->ch[idx]; 94 | if (p == NULL) p = root; 95 | node* tee = p; 96 | while (tee != root && tee->cnt != -1) { 97 | ans += tee->cnt; tee->cnt = -1; 98 | tee = tee->fail; 99 | } 100 | } 101 | return ans; 102 | } 103 | void solve() { 104 | scanf("%d", &n); 105 | node* root = newnode(); 106 | f(i, 0, n) { 107 | scanf("%s", buf); 108 | insert(root, buf); 109 | } 110 | getFail(root); 111 | scanf("%s", buf); 112 | printf("%d\n", query(root, buf)); 113 | } 114 | int main() { 115 | scanf("%d", &T); 116 | while (T--) { 117 | init(); 118 | solve(); 119 | } 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /c++/String/eval.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://tyvj.cn/p/3006 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 1000033; 34 | 35 | int priv[333]; 36 | double val[333]; 37 | double calc(double a, double b, double op) { 38 | if (op == '+') return a + b; 39 | if (op == '-') return a - b; 40 | if (op == '*') return a * b; 41 | if (op == '/') return a / b; 42 | if (op == '^') return exp(log(a) * b); 43 | } 44 | char buf[MAXN]; 45 | double num[MAXN]; 46 | char oper[MAXN]; 47 | int top, cur; 48 | double solve() { 49 | priv['+'] = priv['-'] = 3; 50 | priv['*'] = priv['/'] = 2; 51 | priv['^'] = 1; priv['('] = 10; 52 | double x, y, t = 0; 53 | char last = 0; 54 | int len = strlen(buf); 55 | f(i, 0, len) { 56 | if (buf[i] >= 'a' && buf[i] <= 'z') { 57 | num[top++] = (val[buf[i]]); 58 | } else if (buf[i] >= '0' && buf[i] <= '9') { 59 | num[top++] = (atof(buf + i)); 60 | for (; i + 1 < len && buf[i + 1] >= '0' && buf[i + 1] <= '9'; ++i);// NOLINT 61 | if (i + 1 < len && buf[i + 1] == '.') { 62 | for (i++; i + 1 < len && buf[i + 1] >= 0 && buf[i + 1] <= '9'; ++i);// NOLINT 63 | } 64 | } else if (buf[i] == '(') { 65 | oper[cur++] = (buf[i]); 66 | } else if (buf[i] == ')') { 67 | while (oper[cur - 1] != '(') { 68 | y = num[--top]; 69 | x = num[--top]; 70 | char op = oper[--cur]; 71 | x = calc(x, y, op); 72 | x = fmod(x, 10000.0); 73 | // if (x > 10000) x = int(x) % 10000; 74 | num[top++] = (x); 75 | } 76 | --cur; 77 | } else if (buf[i] == '-' && (last == 0 || last == '(')) { 78 | num[top++] = (0.0); 79 | oper[cur++] = ('-'); 80 | } else if (priv[buf[i]] > 0) { 81 | while (cur > 0 && priv[buf[i]] >= priv[oper[cur - 1]]) { 82 | y = num[--top]; 83 | x = num[--top]; 84 | char op = oper[--cur]; 85 | x = calc(x, y, op); 86 | x = fmod(x, 10000.0); 87 | // if (x > 10000) x = int(x) % 10000; 88 | num[top++] = (x); 89 | } 90 | oper[cur++] = (buf[i]); 91 | } else { 92 | continue; 93 | } 94 | last = buf[i]; 95 | } 96 | while (cur > 0) { 97 | y = num[--top]; 98 | x = num[--top]; 99 | char op = oper[--cur]; 100 | x = calc(x, y, op); 101 | x = fmod(x, 10000.0); 102 | // if (x > 10000) x = int(x) % 10000; 103 | num[top++] = (x); 104 | } 105 | return num[top - 1]; 106 | } 107 | int main() { 108 | scanf("%s", buf); 109 | printf("%d\n", int(solve()) % 10000);// NOLINT 110 | } 111 | -------------------------------------------------------------------------------- /c++/String/kmp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1015 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 1000033; 34 | 35 | char p[MAXN], t[MAXN]; 36 | int d[MAXN]; 37 | int T; 38 | int n, m, ans; 39 | void get() { 40 | d[0] = d[1] = 0; 41 | int j = 0; 42 | f(i, 1, n) { 43 | j = d[i]; 44 | while (j && p[i] != p[j]) j = d[j]; 45 | if (p[i] == p[j]) { 46 | d[i + 1] = j + 1; 47 | } else { 48 | d[i + 1] = 0; 49 | } 50 | } 51 | } 52 | void kmp() { 53 | get(); 54 | int j = 0; 55 | f(i, 0, n) { 56 | while (j && t[i] != p[j]) j = d[j]; 57 | if (t[i] == p[j]) ++j; 58 | if (j == m) ++ans; 59 | } 60 | } 61 | int main() { 62 | scanf("%d", &T); 63 | while (T--) { 64 | scanf("%s%s", p, t); 65 | n = strlen(t); m = strlen(p); 66 | ans = 0; 67 | kmp(); 68 | printf("%d\n", ans); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /c++/String/manacher.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1032 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 1000033; 35 | 36 | char buf[MAXN]; 37 | char s[MAXN * 2]; 38 | int n; 39 | int p[MAXN * 2]; 40 | int mx, id; 41 | int T; 42 | void get() { 43 | mx = 0; 44 | f(i, 1, n) { 45 | if (mx > i) { 46 | p[i] = std::min(p[2 * id - i], mx - i); 47 | } else { 48 | p[i] = 1; 49 | } 50 | while (s[i + p[i]] == s[i - p[i]]) ++p[i]; 51 | if (i + p[i] > mx) { 52 | mx = i + p[i]; id = i; 53 | } 54 | } 55 | } 56 | int main() { 57 | scanf("%d", &T); 58 | while (T--) { 59 | scanf("%s", buf); 60 | n = strlen(buf); 61 | s[0] = '$'; s[1] = '#'; 62 | f(i, 0, n) { 63 | s[i * 2 + 2] = buf[i]; 64 | s[i * 2 + 3] = '#'; 65 | } 66 | n = n * 2 + 2; 67 | s[n] = '\0'; 68 | get(); 69 | mx = 0; 70 | f(i, 0, n) if (mx < p[i]) mx = p[i]; 71 | printf("%d\n", mx - 1); 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /c++/String/sa.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://uoj.ac/problem/35 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | char s[MAXN]; 36 | int sa[MAXN], rank[MAXN], height[MAXN]; 37 | int t1[MAXN], t2[MAXN], r[MAXN], c[MAXN]; 38 | void hz(int *r, int n, int m) { 39 | int *x = t1, *y = t2, *t, p = 0; 40 | f(i, 0, m) c[i] = 0; 41 | f(i, 0, n) c[ x[i] = r[i] ]++; 42 | f(i, 1, m) c[i] += c[i - 1]; 43 | gd(i, n - 1, 0) sa[ --c[ x[i] ] ] = i; 44 | for (int j = 1; j <= n && p < n; j <<= 1, m = p) { 45 | f(i, n - j, n) y[p++] = i; 46 | f(i, 0, n) if (sa[i] >= j) y[p++] = sa[i] - j; 47 | f(i, 0, m) c[i] = 0; 48 | f(i, 0, n) c[ x[ y[i] ] ]++; 49 | f(i, 1, m) c[i] += c[i - 1]; 50 | gd(i, n - 1, 0) sa[ --c[ x[ y[i] ] ] ] = y[i]; 51 | t = x; x = y; y = t; 52 | p = 1; x[ sa[0] ] = 0; 53 | f(i, 1, n) { 54 | x[ sa[i] ] = y[ sa[i] ] == y[ sa[i - 1] ] && y[ sa[i] + j ] == y[ sa[i - 1] + j ] ? p - 1 : p++;// NOLINT 55 | } 56 | } 57 | } 58 | void getHeight(int n) { 59 | g(i, 1, n) rank[ sa[i] ] = i; 60 | int k = 0, j; 61 | f(i, 0, n) { 62 | j = rank[ sa[i] - 1 ]; 63 | for (k?k--:0; s[i + k] == s[j + k]; ++k);// NOLINT 64 | height[ rank[i] ] = k; 65 | } 66 | } 67 | int mn; 68 | void solve() { 69 | int n = strlen(s); 70 | f(i, 0, n) r[i] = s[i] - 'a' + 1; 71 | hz(r, n + 1, 27); 72 | getHeight(n); 73 | g(i, 1, n) printf("%d ", sa[i] + 1); puts(""); 74 | g(i, 2, n) printf("%d ", height[i]); puts(""); 75 | } 76 | int main() { 77 | while (~scanf("%s", s) && s[0] != '.') { 78 | solve(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /c++/dataStructure/binaryHeap.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA(The Star And Thank Author License) 4 | * http://poj.org/problem?id=3253 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #define x1 x11 10 | #define y1 y11 11 | 12 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 13 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 14 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 15 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 16 | 17 | #ifdef WIN32 18 | #define LLD "%I64d" 19 | #define LLU "%I64u" 20 | #else 21 | #define LLD "%lld" 22 | #define LLU "%llu" 23 | #endif 24 | 25 | typedef long long LL;// NOLINT 26 | typedef long double real; 27 | 28 | const double INF = 1e100; 29 | const int oo = ~0u >> 2; 30 | const double pi = acos(-1.0); 31 | const double EPS = 1e-8; 32 | const int MAXN = 100033; 33 | 34 | LL h[MAXN]; 35 | int hs; 36 | void swim(int p) { 37 | int q = p >> 1; 38 | LL a = h[p]; 39 | while (q && a < h[q]) { 40 | h[p] = h[q]; p = q; q = p >> 1; 41 | } 42 | h[p] = a; 43 | } 44 | void sink(int p) { 45 | int q = p << 1; 46 | LL a = h[p]; 47 | while (q <= hs) { 48 | if (q < hs && h[q + 1] < h[q]) ++q; 49 | if (h[q] >= a) break; 50 | h[p] = h[q]; p = q; q = p << 1; 51 | } 52 | h[p] = a; 53 | } 54 | void insert(LL a) { 55 | h[++hs] = a; swim(hs); 56 | } 57 | LL getMin() { 58 | LL r = h[1]; h[1] = h[hs--]; 59 | sink(1); 60 | return r; 61 | } 62 | void build() { 63 | fd(i, hs / 2, 0) sink(i); 64 | } 65 | void decreaseKey(int p, LL a) { 66 | h[p] = a; swim(p); 67 | } 68 | int n; 69 | LL ans = 0, tee; 70 | int main() { 71 | #ifdef LOCAL 72 | freopen("a.in", "r", stdin); 73 | freopen("a.out", "w", stdout); 74 | #endif 75 | 76 | scanf("%d", &n); 77 | g(i, 1, n) scanf("%lld", h + i); 78 | hs = n; 79 | build(); 80 | f(i, 1, n) { 81 | tee = getMin(); 82 | tee += getMin(); 83 | insert(tee); 84 | ans += tee; 85 | } 86 | printf("%lld\n", ans); 87 | 88 | #ifdef LOCAL 89 | fclose(stdin); 90 | fclose(stdout); 91 | #endif 92 | return 0; 93 | } 94 | 95 | -------------------------------------------------------------------------------- /c++/dataStructure/bit2d.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA(The Star And Thank Author License) 4 | * http://poj.org/problem?id=2155 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #define x1 x11 10 | #define y1 y11 11 | 12 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 13 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 14 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 15 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 16 | 17 | #ifdef WIN32 18 | #define LLD "%I64d" 19 | #define LLU "%I64u" 20 | #else 21 | #define LLD "%lld" 22 | #define LLU "%llu" 23 | #endif 24 | 25 | typedef long long LL;// NOLINT 26 | typedef long double real; 27 | 28 | const double INF = 1e100; 29 | const int oo = ~0u >> 2; 30 | const double pi = acos(-1.0); 31 | const double EPS = 1e-8; 32 | const int MAXN = 100033; 33 | 34 | inline void read(int &x) {// NOLINT 35 | char c = getchar(); 36 | int f = 1; 37 | while (c < '0' || c > '9') { 38 | if (c == '-') f = -1; 39 | c = getchar(); 40 | } 41 | x = 0; 42 | while (c >= '0' && c <= '9') { 43 | x = (x << 1) + (x << 3) + c - 48; 44 | c = getchar(); 45 | } 46 | x *= f; 47 | } 48 | int n; 49 | int t[1033][1033]; 50 | void upd(int x, int y) { 51 | for (int i = x; i; i -= i & -i) { 52 | for (int j = y; j; j -= j & -j) { 53 | t[i][j] ^= 1; 54 | } 55 | } 56 | } 57 | int get(int x, int y) { 58 | int res = 0; 59 | for (int i = x; i <= n; i += i & -i) { 60 | for (int j = y; j <= n; j += j & -j) { 61 | res += t[i][j]; 62 | } 63 | } 64 | return res & 1; 65 | } 66 | int T; 67 | int x1, y1, x2, y2; 68 | int m; 69 | char buf[12]; 70 | int main() { 71 | #ifdef LOCAL 72 | freopen("a.in", "r", stdin); 73 | freopen("a.out", "w", stdout); 74 | #endif 75 | 76 | scanf("%d", &T); 77 | while (T--) { 78 | scanf("%d%d", &n, &m); 79 | memset(t, 0, sizeof t); 80 | while (m--) { 81 | scanf("%s%d%d", buf, &x1, &y1); 82 | if (buf[0] == 'C') { 83 | scanf("%d%d", &x2, &y2); 84 | upd(x1 - 1, y1 - 1); 85 | upd(x2, y2); 86 | upd(x2, y1 - 1); 87 | upd(x1 - 1, y2); 88 | } else { 89 | printf("%d\n", get(x1, y1)); 90 | } 91 | } 92 | puts(""); 93 | } 94 | 95 | #ifdef LOCAL 96 | fclose(stdin); 97 | fclose(stdout); 98 | #endif 99 | return 0; 100 | } 101 | 102 | -------------------------------------------------------------------------------- /c++/dataStructure/dfsSeqAndTemporalSegTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @author SCaffrey (sirius.caffrey@gmail.com) 3 | * @copyright SATA(The Star And Thank Author License) 4 | * http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 5 | */ 6 | 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 100033; 35 | 36 | inline void read(int &x) {// NOLINT 37 | char c = getchar(); 38 | int f = 1; 39 | while (c < '0' || c > '9') { 40 | if (c == '-') f = -1; 41 | c = getchar(); 42 | } 43 | x = 0; 44 | while (c >= '0' && c <= '9') { 45 | x = (x << 1) + (x << 3) + c - 48; 46 | c = getchar(); 47 | } 48 | x *= f; 49 | } 50 | int n, m, idx; 51 | int w[MAXN], c[MAXN]; 52 | int fa[MAXN][18], dep[MAXN], belong[MAXN], pos[MAXN], sz[MAXN]; 53 | bool vis[MAXN]; 54 | struct edge { 55 | int t, x; 56 | } e[MAXN << 3]; 57 | int head[MAXN], cnt = 1; 58 | inline void add(int s, int t) { 59 | e[++cnt].t = t; e[cnt].x = head[s]; head[s] = cnt; 60 | } 61 | void dfs1(int x) { 62 | vis[x] = 1; sz[x] = 1; 63 | g(i, 1, 16) { 64 | if (dep[x] < (1 << i)) break; 65 | fa[x][i] = fa[ fa[x][i - 1] ][i - 1]; 66 | } 67 | for (int i = head[x]; i; i = e[i].x) { 68 | if (vis[e[i].t]) continue; 69 | dep[e[i].t] = dep[x] + 1; 70 | fa[e[i].t][0] = x; 71 | dfs1(e[i].t); 72 | sz[x] += sz[e[i].t]; 73 | } 74 | } 75 | void dfs2(int x, int chain) { 76 | pos[x] = ++idx; belong[x] = chain; 77 | int k = 0; 78 | for (int i = head[x]; i; i = e[i].x) { 79 | if (dep[e[i].t] > dep[x] && sz[e[i].t] > sz[k]) k = e[i].t; 80 | } 81 | if (!k) return; 82 | dfs2(k, chain); 83 | for (int i = head[x]; i; i = e[i].x) { 84 | if (e[i].t != k && dep[e[i].t] > dep[x]) dfs2(e[i].t, e[i].t); 85 | } 86 | } 87 | int lca(int x, int y) { 88 | if (dep[x] < dep[y]) std::swap(x, y); 89 | int t = dep[x] - dep[y]; 90 | g(i, 0, 16) if (t & (1 << i)) x = fa[x][i]; 91 | gd(i, 16, 0) if (fa[x][i] != fa[y][i]) { 92 | x = fa[x][i]; y = fa[y][i]; 93 | } 94 | if (x == y) return x; 95 | return fa[x][0]; 96 | } 97 | struct node { 98 | int mx, sum; 99 | node *ls, *rs; 100 | inline node() { 101 | mx = sum = 0; 102 | ls = rs = NULL; 103 | } 104 | inline void maintain() { 105 | if (ls == NULL) ls = new node(); 106 | if (rs == NULL) rs = new node(); 107 | mx = std::max(ls->mx, rs->mx); 108 | sum = ls->sum + rs->sum; 109 | } 110 | void update(int l, int r, int pos, int num) { 111 | if (l == r) { 112 | mx = sum = num; 113 | return; 114 | } 115 | if (ls == NULL) ls = new node(); 116 | if (rs == NULL) rs = new node(); 117 | int md = l + (r - l >> 1); 118 | if (pos <= md) { 119 | ls->update(l, md, pos, num); 120 | } else { 121 | rs->update(md + 1, r, pos, num); 122 | } 123 | maintain(); 124 | } 125 | int getMax(int l, int r, int ll, int rr) { 126 | if (l == ll && r == rr) return mx; 127 | if (ls == NULL) ls = new node(); 128 | if (rs == NULL) rs = new node(); 129 | int md = l + (r - l >> 1); 130 | if (rr <= md) return ls->getMax(l, md, ll, rr); 131 | if (ll > md) return rs->getMax(md + 1, r, ll, rr); 132 | return std::max(ls->getMax(l, md, ll, md), 133 | rs->getMax(md + 1, r, md + 1, rr)); 134 | } 135 | int getSum(int l, int r, int ll, int rr) { 136 | if (ll == l && rr == r) return sum; 137 | if (ls == NULL) ls = new node(); 138 | if (rs == NULL) rs = new node(); 139 | int md = l + (r - l >> 1); 140 | if (rr <= md) return ls->getSum(l, md, ll, rr); 141 | if (ll > md) return rs->getSum(md + 1, r, ll, rr); 142 | return ls->getSum(l, md, ll, md) + rs->getSum(md + 1, r, md + 1, rr); 143 | } 144 | }; 145 | node *root[MAXN]; 146 | int solveMax(int c, int x, int f) { 147 | int mx = 0; 148 | while (belong[x] != belong[f]) { 149 | mx = std::max(mx, root[c]->getMax(1, n, pos[ belong[x] ], pos[x])); 150 | x = fa[ belong[x] ][0]; 151 | } 152 | mx = std::max(mx, root[c]->getMax(1, n, pos[f], pos[x])); 153 | return mx; 154 | } 155 | int solveSum(int c, int x, int f) { 156 | int sum = 0; 157 | while (belong[x] != belong[f]) { 158 | sum += root[c]->getSum(1, n, pos[ belong[x] ], pos[x]); 159 | x = fa[ belong[x] ][0]; 160 | } 161 | sum += root[c]->getSum(1, n, pos[f], pos[x]); 162 | return sum; 163 | } 164 | int u, v; 165 | char s[12]; 166 | int x, y, lc, tee; 167 | int main() { 168 | #ifdef LOCAL 169 | freopen("a.in", "r", stdin); 170 | freopen("a.out", "w", stdout); 171 | #endif 172 | 173 | read(n); read(m); 174 | g(i, 1, n) { 175 | read(w[i]); read(c[i]); 176 | } 177 | g(i, 1, 100000) root[i] = new node(); 178 | f(i, 1, n) { 179 | read(u); read(v); 180 | add(u, v); add(v, u); 181 | } 182 | dfs1(1); dfs2(1, 1); 183 | g(i, 1, n) root[ c[i] ]->update(1, n, pos[i], w[i]); 184 | g(i, 1, m) { 185 | scanf("%s", s); 186 | read(x); read(y); 187 | if (s[0] == 'C') { 188 | if (s[1] == 'C') { 189 | root[ c[x] ]->update(1, n, pos[x], 0); 190 | c[x] = y; 191 | root[y]->update(1, n, pos[x], w[x]); 192 | } else { 193 | root[ c[x] ]->update(1, n, pos[x], y); 194 | w[x] = y; 195 | } 196 | } else { 197 | lc = lca(x, y); 198 | if (s[1] == 'S') { 199 | tee = solveSum(c[x], x, lc) + solveSum(c[x], y, lc); 200 | if (c[x] == c[lc]) tee -= w[lc]; 201 | printf("%d\n", tee); 202 | } else { 203 | printf("%d\n", std::max(solveMax(c[x], x, lc), solveMax(c[x], y, lc))); 204 | } 205 | } 206 | } 207 | 208 | #ifdef LOCAL 209 | fclose(stdin); 210 | fclose(stdout); 211 | #endif 212 | return 0; 213 | } 214 | 215 | -------------------------------------------------------------------------------- /c++/dataStructure/heap/README.md: -------------------------------------------------------------------------------- 1 | # Heap 2 | 3 | Here you'll find out an implementation of a heap at [heap.cpp](heap.cpp). -------------------------------------------------------------------------------- /c++/dataStructure/lca.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://codevs.cn/problem/2370/ 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | int n, m; 36 | int s, t, w, u, v; 37 | struct node { 38 | int t, w, x; 39 | } e[MAXN << 1]; 40 | int head[MAXN], cnt = 1; 41 | inline void add(int s, int t, int w) { 42 | e[++cnt].t = t; e[cnt].w = w; e[cnt].x = head[s]; head[s] = cnt; 43 | e[++cnt].t = s; e[cnt].w = w; e[cnt].x = head[t]; head[t] = cnt; 44 | } 45 | int dep[MAXN], vis[MAXN]; 46 | int fa[MAXN][18]; 47 | int cost[MAXN][18]; 48 | void dfs(int x) { 49 | vis[x] = 1; 50 | g(i, 1, 17) { 51 | if (dep[x] < (1 << i)) break; 52 | fa[x][i] = fa[ fa[x][i - 1] ][i - 1]; 53 | cost[x][i] = cost[x][i - 1] + cost[ fa[x][i - 1] ][i - 1]; 54 | } 55 | for (int i = head[x]; i; i = e[i].x) { 56 | if (vis[e[i].t]) continue; 57 | dep[e[i].t] = dep[x] + 1; 58 | fa[e[i].t][0] = x; 59 | cost[e[i].t][0] = e[i].w; 60 | dfs(e[i].t); 61 | } 62 | } 63 | int solve(int x, int y) { 64 | int res = 0; 65 | if (dep[x] < dep[y]) std::swap(x, y);// NOLINT 66 | int t = dep[x] - dep[y]; 67 | g(i, 0, 17) if (t & (1 << i)) { 68 | res += cost[x][i]; 69 | x = fa[x][i]; 70 | } 71 | // printf("%d %d\n", x, y); 72 | gd(i, 17, 0) if (fa[x][i] != fa[y][i]) { 73 | res += cost[x][i] + cost[y][i]; 74 | x = fa[x][i]; y = fa[y][i]; 75 | } 76 | // printf("%d %d %d %d\n", x, y, fa[x][0], fa[y][0]); 77 | if (x != y) { 78 | res += cost[x][0] + cost[y][0]; 79 | } 80 | return res; 81 | } 82 | int main() { 83 | scanf("%d", &n); 84 | f(i, 1, n) { 85 | scanf("%d%d%d", &u, &v, &w); 86 | add(u, v, w); 87 | } 88 | dfs(0); 89 | scanf("%d", &m); 90 | f(i, 0, m) { 91 | scanf("%d%d", &u, &v); 92 | printf("%d\n", solve(u, v)); 93 | } 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /c++/dataStructure/leftTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=2334 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | int nx[MAXN]; 36 | int v[MAXN], l[MAXN], r[MAXN], d[MAXN], root[MAXN]; 37 | int n, m; 38 | void start() { 39 | f(i, 0, MAXN - 33) nx[i] = i + 1; 40 | } 41 | int merge(int x, int y) { 42 | if (!x) return y; 43 | if (!y) return x; 44 | if (v[x] < v[y]) std::swap(x, y); 45 | r[x] = merge(r[x], y); 46 | if (d[l[x]] < d[r[x]]) std::swap(l[x], r[x]); 47 | d[x] = d[r[x]] + 1; 48 | return x; 49 | } 50 | int init(int x) { 51 | int p = nx[0]; nx[0] = nx[p]; 52 | v[p] = x; l[p] = r[p] = d[p] = 0; 53 | return p; 54 | } 55 | void del(int x) { 56 | nx[x] = nx[0]; nx[0] = x; 57 | } 58 | int insert(int x, int y) { 59 | return merge(x, init(y)); 60 | } 61 | int top(int x) { 62 | return v[x]; 63 | } 64 | int pop(int x) { 65 | int p = merge(l[x], r[x]); 66 | del(x); 67 | return p; 68 | } 69 | int fa[MAXN]; 70 | void init() { 71 | g(i, 0, n) fa[i] = i; 72 | } 73 | int get(int x) { 74 | int tee = fa[x], q; 75 | while (tee != fa[tee]) tee = fa[tee]; 76 | while (x != tee) { 77 | q = fa[x]; fa[x] = tee; x = q; 78 | } 79 | return tee; 80 | } 81 | bool unite(int a, int b) { 82 | int x = get(a), y = get(b); 83 | if (x == y) return 0; 84 | fa[x] = y; 85 | return 1; 86 | } 87 | int tee, x, y, a, b; 88 | int main() { 89 | while (~scanf("%d", &n)) { 90 | start(); 91 | init(); 92 | g(i, 1, n) { 93 | scanf("%d", &tee); 94 | root[i] = 0; 95 | root[i] = insert(root[i], tee); 96 | } 97 | scanf("%d", &m); 98 | f(i, 0, m) { 99 | scanf("%d%d", &a, &b); 100 | if (get(a) == get(b)) { 101 | puts("-1"); 102 | } else { 103 | a = get(a); b = get(b); 104 | x = top(root[a]); root[a] = pop(root[a]); 105 | y = top(root[b]); root[b] = pop(root[b]); 106 | root[a] = insert(root[a], x / 2); 107 | root[b] = insert(root[b], y / 2); 108 | unite(a, b); 109 | root[b] = merge(root[a], root[b]); 110 | printf("%d\n", top(root[b])); 111 | } 112 | } 113 | } 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /c++/dataStructure/segment_tree/README.md: -------------------------------------------------------------------------------- 1 | # Segment Tree 2 | 3 | [segment_tree_point_modify_seg_query.cpp](segment_tree_point_modify_seg_query.cpp). 4 | 5 | [segment_tree_seg_modify_seg_query.cpp](segment_tree_seg_modify_seg_query.cpp). 6 | 7 | [segment_tree_with_pools.cpp](segment_tree_with_pools.cpp). 8 | -------------------------------------------------------------------------------- /c++/dataStructure/segment_tree/segment_tree_point_modify_seg_query.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name segment_tree_point_modify_seg_query.cpp 3 | * @subject Segment Tree 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a segment tree implemented with pointers. 7 | * @tests http://acm.hdu.edu.cn/showproblem.php?pid=1166 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 14 | 15 | int T; 16 | int n; 17 | char buf[12]; 18 | int x, y; 19 | 20 | /** 21 | * 'struct node' 22 | * Each node represents one in the seg_tree 23 | */ 24 | struct node{ 25 | int l, r, md; 26 | int sum; 27 | node *ls, *rs; 28 | /** 29 | * constructor 30 | * @param ll [int] 31 | * @param rr [int] 32 | */ 33 | inline node(int ll = 0, int rr = 0):l(ll), r(rr),// NOLINT 34 | md(ll + (rr - ll >> 1)) { 35 | if (ll == rr) { 36 | scanf("%d", &sum); 37 | ls = rs = NULL; 38 | return; 39 | } 40 | ls = new node(ll, md); 41 | rs = new node(md + 1, rr); 42 | sum = ls->sum + rs->sum; 43 | } 44 | /** 45 | * point modify 46 | * @param pos [int] 47 | * @param delta [int] 48 | */ 49 | inline void add(int pos, int delta) { 50 | if (l == r) { 51 | sum += delta; 52 | return; 53 | } 54 | if (pos <= md) { 55 | ls->add(pos, delta); 56 | } else { 57 | rs->add(pos, delta); 58 | } 59 | sum = ls->sum + rs->sum; 60 | } 61 | /** 62 | * seg query 63 | * @param ll [int] 64 | * @param rr [int] 65 | * @return the sum of seg [ll, rr] [int] 66 | */ 67 | inline int query(int ll, int rr) { 68 | if (ll == l && rr == r) { 69 | return sum; 70 | } 71 | if (ll > md)return rs->query(ll, rr); 72 | if (rr <= md)return ls->query(ll, rr); 73 | return ls->query(ll, md) + rs->query(md + 1, rr); 74 | } 75 | }; 76 | 77 | int main() { 78 | scanf("%d", &T); 79 | g(i, 1, T) { 80 | scanf("%d", &n); 81 | node* root = new node(1, n); 82 | printf("Case %d:\n", i); 83 | while (~scanf("%s", buf)) { 84 | if (buf[0] == 'E')break; 85 | scanf("%d%d", &x, &y); 86 | if (buf[0] == 'A') { 87 | root->add(x, y); 88 | } else if (buf[0] == 'S') { 89 | root->add(x, -y); 90 | } else { 91 | printf("%d\n", root->query(x, y)); 92 | } 93 | } 94 | } 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /c++/dataStructure/segment_tree/segment_tree_seg_modify_seg_query.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name segment_tree_seg_modify_seg_query.cpp 3 | * @subject Segment Tree 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a segment tree implemented with pointers. 7 | * @tests http://acm.hdu.edu.cn/showproblem.php?pid=1698 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 14 | 15 | typedef long long LL;// NOLINT 16 | 17 | int n, q; 18 | char buf[12]; 19 | int a, b, c; 20 | 21 | /** 22 | * 'struct node' 23 | * Each node represents one in the seg_tree 24 | */ 25 | struct node{ 26 | int l, r, md; 27 | node *ls, *rs; 28 | LL cover; 29 | LL sum; 30 | /** 31 | * constructor 32 | * @param ll [int] 33 | * @param rr [int] 34 | */ 35 | inline node(int ll = 0,int rr = 0):l(ll), r(rr),// NOLINT 36 | md(ll + (rr - ll >> 1)) { 37 | cover = 0; 38 | if (l == r) { 39 | ls = rs = NULL; 40 | scanf("%lld", &sum); 41 | return; 42 | } 43 | ls = new node(ll, md); 44 | rs = new node(md + 1, rr); 45 | sum = ls->sum + rs->sum; 46 | } 47 | /** 48 | * [pushdown] 49 | * pass the cover down. 50 | */ 51 | inline void pushdown() { 52 | if (cover) { 53 | ls->cover += cover; 54 | rs->cover += cover; 55 | ls->sum += (ls->r - ls->l + 1) * cover; 56 | rs->sum += (rs->r - rs->l + 1) * cover; 57 | cover = 0; 58 | } 59 | } 60 | /** 61 | * seg modify 62 | * @param ll [int] 63 | * @param rr [int] 64 | * @param val [int] 65 | */ 66 | inline void add(int ll, int rr, int val) { 67 | if (ll == l && rr == r) { 68 | sum += val * (r - l + 1); 69 | cover += val; 70 | return; 71 | } 72 | pushdown(); 73 | if (rr <= md) { 74 | ls->add(ll, rr, val); 75 | } else if (ll > md) { 76 | rs->add(ll, rr, val); 77 | } else { 78 | ls->add(ll, md, val); rs->add(md + 1, rr, val); 79 | } 80 | sum = ls->sum + rs->sum; 81 | } 82 | /** 83 | * seg query 84 | * @param ll [int] 85 | * @param rr [int] 86 | * @return the sum of seg [ll, rr] [long long] 87 | */ 88 | inline LL query(int ll, int rr) { 89 | if (ll == l && rr == r) { 90 | return sum; 91 | } 92 | pushdown(); 93 | if (ll > md)return rs->query(ll, rr); 94 | if (rr <= md)return ls->query(ll, rr); 95 | return ls->query(ll, md) + rs->query(md + 1, rr); 96 | } 97 | }; 98 | 99 | int main() { 100 | scanf("%d%d", &n, &q); 101 | node* root = new node(1, n); 102 | while (q--) { 103 | scanf("%s", buf); 104 | if (buf[0] == 'C') { 105 | scanf("%d%d%d", &a, &b, &c); 106 | root->add(a, b, c); 107 | } else { 108 | scanf("%d%d", &a, &b); 109 | printf("%lld\n", root->query(a, b)); 110 | } 111 | } 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /c++/dataStructure/segment_tree/segment_tree_with_pools.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name segment_tree_seg_modify_seg_query.cpp 3 | * @subject Segment Tree 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a segment tree implemented with pointers. 7 | * @tests http://acm.hdu.edu.cn/showproblem.php?pid=1754 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | 16 | const int MAXNODE = 400033; 17 | 18 | int n, m; 19 | char buf[12]; 20 | int a, b; 21 | 22 | /** 23 | * 'struct node' 24 | * Each node represents one in the seg_tree 25 | */ 26 | struct node { 27 | int l, r, md; 28 | int mx; 29 | node *ls, *rs; 30 | /** 31 | * seg query 32 | * @param ll [int] 33 | * @param rr [int] 34 | * @return the max in seg [ll, rr] [int] 35 | */ 36 | inline int query(int ll, int rr) { 37 | if (ll == l && rr == r) { 38 | return mx; 39 | } 40 | if (ll > md)return rs->query(ll, rr); 41 | if (rr <= md)return ls->query(ll, rr); 42 | return std::max(ls->query(ll, md), rs->query(md + 1, rr)); 43 | } 44 | /** 45 | * point modify 46 | * @param pos [int] 47 | * @param val [int] 48 | */ 49 | inline void modify(int pos, int val) { 50 | if (l == r) { 51 | mx = val; 52 | return; 53 | } 54 | if (pos > md) { 55 | rs->modify(pos, val); 56 | } else { 57 | ls->modify(pos, val); 58 | } 59 | mx = std::max(ls->mx, rs->mx); 60 | } 61 | inline void remove() { 62 | if (ls)ls->remove(); 63 | if (rs)rs->remove(); 64 | delete this; 65 | } 66 | } pool[MAXNODE], *tail; 67 | 68 | /** 69 | * using pool to construct new node 70 | * @param ll [int] 71 | * @param rr [int] 72 | * @return the new node as required [node*] 73 | */ 74 | inline node* newnode(int ll, int rr) { 75 | node* ans = tail++; 76 | ans->l = ll; ans->r = rr; ans->md = ll + (rr - ll >> 1); 77 | if (ll == rr) { 78 | scanf("%d", &(ans->mx)); 79 | ans->ls = ans->rs = NULL; 80 | return ans; 81 | } 82 | ans->ls = newnode(ll, ans->md); 83 | ans->rs = newnode(ans->md + 1, rr); 84 | ans->mx = std::max(ans->ls->mx, ans->rs->mx); 85 | return ans; 86 | } 87 | 88 | int main() { 89 | while (~scanf("%d%d", &n, &m)) { 90 | tail = pool; 91 | node* root = newnode(1, n); 92 | while (m--) { 93 | scanf("%s%d%d", buf, &a, &b); 94 | if (buf[0] == 'Q') { 95 | printf("%d\n", root->query(a, b)); 96 | } else { 97 | root->modify(a, b); 98 | } 99 | } 100 | } 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /c++/dataStructure/size_balanced_tree/README.md: -------------------------------------------------------------------------------- 1 | # Size Balanced Tree 2 | -------------------------------------------------------------------------------- /c++/dataStructure/size_balanced_tree/size_balanced_tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name Size_Balanced_Tree.cpp 3 | * @subject Size Balanced Tree 4 | * @author KingsNeverDie(lm19990323@outlook.com) 5 | * @Copyright DWTFABG 6 | * @tests http://www.lydsy.com/JudgeOnline/problem.php?id=1503/ 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | struct node { 15 | int left, right, size, key; 16 | void init(int k) { 17 | key = k; 18 | size = 1; 19 | left = right = 0; 20 | } 21 | }tree[1000011]; 22 | 23 | int n, min; 24 | int delta; 25 | int root = 0, top = 0; 26 | const int nil = 0; 27 | int leave = 0; 28 | 29 | void right_rotate(int &x) {// NOLINT 30 | int y = tree[x].left; 31 | tree[x].left = tree[y].right; 32 | tree[y].right = x; 33 | tree[y].size = tree[x].size; 34 | tree[x].size = tree[tree[x].right].size + tree[tree[x].left].size + 1; 35 | x = y; 36 | } 37 | 38 | void left_rotate(int &y) {// NOLINT 39 | int x = tree[y].right; 40 | tree[y].right = tree[x].left; 41 | tree[x].left = y; 42 | tree[x].size = tree[y].size; 43 | tree[y].size = tree[tree[y].left].size + tree[tree[y].right].size + 1; 44 | y = x; 45 | } 46 | 47 | void Maintain(int &x, bool flag) {// NOLINT 48 | if (flag) { 49 | if (tree[tree[x].left].size < tree[tree[tree[x].right].left].size) { 50 | right_rotate(tree[x].right); 51 | left_rotate(x); 52 | } else if (tree[tree[x].left].size 53 | < tree[tree[tree[x].right].right].size) { 54 | left_rotate(x); 55 | } else { 56 | return; 57 | } 58 | } else { 59 | if (tree[tree[x].right].size < tree[tree[tree[x].left].right].size) { 60 | left_rotate(tree[x].left); 61 | right_rotate(x); 62 | } else if (tree[tree[x].right].size 63 | < tree[tree[tree[x].left].left].size) { 64 | right_rotate(x); 65 | } else { 66 | return; 67 | } 68 | } 69 | Maintain(tree[x].left, false); 70 | Maintain(tree[x].right, true); 71 | Maintain(x, true); 72 | Maintain(x, false); 73 | } 74 | 75 | /** 76 | * Insert a new point to Size Balanced Tree 77 | * @param x [int] 78 | * @param key [int] 79 | */ 80 | void insert(int &x, int key) {// NOLINT 81 | if (x == nil) { 82 | x = ++top; 83 | tree[x].init(key); 84 | return; 85 | } 86 | if (key < tree[x].key) { 87 | insert(tree[x].left, key); 88 | } else { 89 | insert(tree[x].right, key); 90 | } 91 | tree[x].size = tree[tree[x].left].size + tree[tree[x].right].size + 1; 92 | Maintain(x, key >= tree[x].key); 93 | } 94 | 95 | /** 96 | * Remove a point from Size Balanced Tree 97 | * @param x [int] 98 | * @param key [int] 99 | */ 100 | void remove(int &x, int key) {// NOLINT 101 | if (x == nil) { 102 | return; 103 | } else if (tree[x].key < key) { 104 | leave += tree[tree[x].left].size + 1; 105 | x = tree[x].right; 106 | remove(x, key); 107 | } else if (key <= tree[x].key) { 108 | remove(tree[x].left, key); 109 | } 110 | if (x != nil) tree[x].size = 111 | tree[tree[x].left].size + tree[tree[x].right].size + 1; 112 | } 113 | 114 | /** 115 | * Rank 116 | * @param x [int] 117 | * @param key [int] 118 | * @return [the key_th rank] 119 | */ 120 | int rank(int x, int key) { 121 | if (key == tree[tree[x].right].size + 1) return tree[x].key; 122 | if (key < tree[tree[x].right].size + 1) return rank(tree[x].right, key); 123 | return rank(tree[x].left, key - tree[tree[x].right].size - 1); 124 | } 125 | 126 | int main() { 127 | scanf("%d%d", &n, &min); 128 | char rd[5]; 129 | int k; 130 | while (n--) { 131 | scanf("%s%d", rd, &k); 132 | if (rd[0] == 'I' && k >= min) insert(root, k - delta); 133 | if (rd[0] == 'A') delta += k; 134 | if (rd[0] == 'S') { 135 | delta -= k; 136 | remove(root, min - delta); 137 | } 138 | if (rd[0] == 'F') { 139 | if (k <= top - leave) { 140 | printf("%d\n", rank(root, k) + delta); 141 | } else { 142 | puts("-1"); 143 | } 144 | } 145 | } 146 | printf("%d\n", leave); 147 | return 0; 148 | } 149 | -------------------------------------------------------------------------------- /c++/dataStructure/st.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://hihocoder.com/problemset/problem/1068 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #include // NOLINT 11 | #define x1 x11 12 | #define y1 y11 13 | 14 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 15 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 16 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 17 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 18 | 19 | #ifdef WIN32 20 | #define LLD "%I64d" 21 | #define LLU "%I64u" 22 | #else 23 | #define LLD "%lld" 24 | #define LLU "%llu" 25 | #endif 26 | 27 | typedef long long LL;// NOLINT 28 | typedef long double real; 29 | 30 | const double INF = 1e100; 31 | const int oo = ~0u >> 2; 32 | const double pi = acos(-1.0); 33 | const double EPS = 1e-8; 34 | const int MAXN = 1000033; 35 | 36 | int n, m; 37 | int x, a, b; 38 | int s[MAXN]; 39 | int p[MAXN]; 40 | int mn[MAXN][20]; 41 | void init() { 42 | p[1] = 0; 43 | g(i, 2, n) { 44 | p[i] = p[i - 1]; 45 | if ((1 << (p[i] + 1)) == i) ++p[i]; 46 | } 47 | gd(i, n, 1) { 48 | mn[i][0] = s[i]; 49 | for (int j = 1; (i + (1 << j) - 1) <= n; ++j) { 50 | mn[i][j] = std::min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]); 51 | } 52 | } 53 | } 54 | int solve(int ll, int rr) { 55 | int len = rr - ll + 1; 56 | int k = p[len]; 57 | return std::min(mn[ll][k], mn[rr - (1 << k) + 1][k]); 58 | } 59 | int main() { 60 | scanf("%d", &n); 61 | g(i, 1, n) scanf("%d", s + i); 62 | init(); 63 | scanf("%d", &m); 64 | f(i, 0, m) { 65 | scanf("%d%d", &a, &b); 66 | if (a > b) std::swap(a, b); 67 | printf("%d\n", solve(a, b)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /c++/dataStructure/treap.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://www.lydsy.com/JudgeOnline/problem.php?id=2733 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | int n, m; 36 | struct node { 37 | int v, s, r, c; 38 | node* ch[2]; 39 | inline node(int v = 0):v(v) {// NOLINT 40 | ch[0] = ch[1] = NULL; 41 | r = rand(); s = c = 1;// NOLINT 42 | } 43 | inline int cmp(int x) const { 44 | if (x == v) return -1; 45 | if (x < v) return 0; 46 | return 1; 47 | } 48 | inline void maintain() { 49 | s = c; 50 | f(i, 0, 2) if (ch[i]) s += ch[i]->s; 51 | } 52 | inline void print() { 53 | if (ch[0]) ch[0]->print(); 54 | printf("(%d,%d)", v, s); 55 | if (ch[1]) ch[1]->print(); 56 | } 57 | }; 58 | void rotate(node* &o, int d) {// NOLINT 59 | node* k = o->ch[d ^ 1]; 60 | o->ch[d ^ 1] = k->ch[d]; 61 | k->ch[d] = o; 62 | o->maintain(); k->maintain(); o = k; 63 | } 64 | void insert(node* &o, int x) {// NOLINT 65 | if (o == NULL) { 66 | o = new node(x); 67 | return; 68 | } 69 | ++o->s; 70 | if (o->v == x) { 71 | ++o->c; 72 | } else { 73 | int d = x > o->v; 74 | insert(o->ch[d], x); 75 | if (o->ch[d]->r < o->r) rotate(o, d ^ 1); 76 | } 77 | o->maintain(); 78 | } 79 | void remove(node* o, int x) { 80 | if (o->v == x) { 81 | if (o->c > 1) { 82 | --o->c; --o->s; return; 83 | } 84 | node* u = o; 85 | if (o->ch[0] && o->ch[1]) { 86 | int d = (o->ch[0]->r < o->ch[1]->r); 87 | rotate(o, d); remove(o->ch[d], x); 88 | } else { 89 | if (o->ch[0] == NULL && o->ch[1] == NULL) o = NULL; 90 | else if (o->ch[0]) o = o->ch[0]; 91 | else if (o->ch[1]) o = o->ch[1]; 92 | delete u; 93 | } 94 | } else { 95 | remove(o->ch[x > o->v], x); 96 | } 97 | if (o) o->maintain(); 98 | } 99 | int getRank(node* o, int x) { 100 | if (o == NULL) return 0; 101 | int sz = 0; 102 | if (o->ch[0]) sz = o->ch[0]->s; 103 | if (o->v == x) return sz + 1; 104 | if (x > o->v) return sz + o->c + getRank(o->ch[1], x); 105 | return getRank(o->ch[0], x); 106 | } 107 | int getKth(node* o, int x) { 108 | if (o == NULL || x <= 0 || x > o->s) return 0; 109 | int sz = 0; 110 | if (o->ch[0]) sz = o->ch[0]->s; 111 | if (x <= sz) return getKth(o->ch[0], x); 112 | if (x > sz + o->c) return getKth(o->ch[1], x - sz - o->c); 113 | return o->v; 114 | } 115 | int sz; 116 | node* ans; 117 | void getPre(node* o, int x) { 118 | if (o == NULL) return; 119 | while (o) { 120 | if (o->v < x) { 121 | ans = o; o = o->ch[1]; 122 | } else { 123 | o = o->ch[0]; 124 | } 125 | } 126 | } 127 | void getSuc(node* o, int x) { 128 | ans = o; 129 | while (o) { 130 | if (x < o->v) { 131 | ans = o; o = o->ch[0]; 132 | } else { 133 | o = o->ch[1]; 134 | } 135 | } 136 | } 137 | int opt, x; 138 | int main() { 139 | scanf("%d", &n); 140 | node* root = NULL; 141 | srand(233); 142 | g(i, 1, n) { 143 | scanf("%d%d", &opt, &x); 144 | switch (opt) { 145 | case 1: insert(root, x); break; 146 | case 2: remove(root, x); break; 147 | case 3: printf("%d\n", getRank(root, x)); break; 148 | case 4: printf("%d\n", getKth(root, x)); break; 149 | case 5: getPre(root, x); /*if (ans)*/ printf("%d\n", ans->v); break; 150 | case 6: getSuc(root, x); /*if (ans)*/ printf("%d\n", ans->v); break; 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /c++/dataStructure/treapMerge.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author SCaffrey(sirius.caffrey@gmail.com) 3 | * @Copyright SATA 4 | * http://www.lydsy.com/JudgeOnline/problem.php?id=2733 5 | */ 6 | #include // NOLINT 7 | #include // NOLINT 8 | #include // NOLINT 9 | #include // NOLINT 10 | #define x1 x11 11 | #define y1 y11 12 | 13 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 14 | #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) 15 | #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) 16 | #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) 17 | 18 | #ifdef WIN32 19 | #define LLD "%I64d" 20 | #define LLU "%I64u" 21 | #else 22 | #define LLD "%lld" 23 | #define LLU "%llu" 24 | #endif 25 | 26 | typedef long long LL;// NOLINT 27 | typedef long double real; 28 | 29 | const double INF = 1e100; 30 | const int oo = ~0u >> 2; 31 | const double pi = acos(-1.0); 32 | const double EPS = 1e-8; 33 | const int MAXN = 100033; 34 | 35 | struct node { 36 | node* ch[2]; 37 | int r, v, s, c, idx; 38 | node(int v):v(v) {ch[0] = ch[1] = NULL; r = rand(); s = 1; c = 1; idx = 0;}// NOLINT 39 | inline bool operator < (const node& T) const { 40 | return r < T.r; 41 | } 42 | inline int cmp(int x) const { 43 | if (x == v) return -1; 44 | if (x < 0) return 0; 45 | return 1; 46 | } 47 | inline void maintain() { 48 | s = c; 49 | f(i, 0, 2) if (ch[i]) s += ch[i]->s; 50 | } 51 | }; 52 | node* root[MAXN]; 53 | void rotate(node* &o, int d) {// NOLINT 54 | node* k = o->ch[d ^ 1]; o->ch[d ^ 1] = k->ch[d]; k->ch[d] = o; 55 | o->maintain(); k->maintain(); o = k; 56 | } 57 | void insert(node* &o, int x, int idx) {// NOLINT 58 | if (o == NULL) { 59 | o = new node(x); 60 | o->idx = idx; 61 | return; 62 | } 63 | ++o->s; 64 | if (o->v == x) { 65 | ++o->c; 66 | } else { 67 | int d = x > o->v; 68 | insert(o->ch[d], x, idx); 69 | if (o->ch[d]->r < o->r) rotate(o, d ^ 1); 70 | } 71 | o->maintain(); 72 | } 73 | void remove(node* &o, int x) {// NOLINT 74 | int d = o->cmp(x); 75 | if (d == -1) { 76 | node* u = o; 77 | if (o->ch[0] && o->ch[1]) { 78 | int d2 = o->ch[0]->r > o->ch[1]->r; 79 | rotate(o, d2); remove(o->ch[d2], x); 80 | } else { 81 | if (o->ch[0]) { 82 | o = o->ch[1]; 83 | } else { 84 | o = o->ch[0]; 85 | } 86 | delete u; 87 | } 88 | } else { 89 | remove(o->ch[d], x); 90 | } 91 | if (o) o->maintain(); 92 | } 93 | int getKth(node* o, int k) {// NOLINT 94 | if (o == NULL || k <= 0 || k > o->s) return -1; 95 | int s = 0; 96 | if (o->ch[0]) s += o->ch[0]->s; 97 | if (k <= s) return getKth(o->ch[0], k); 98 | if (k > s + o->c) return getKth(o->ch[1], k - s - o->c); 99 | return o->idx; 100 | } 101 | void merge(node* &a, node* &b) { // merge a into b NOLINT 102 | if (a->ch[0]) merge(a->ch[0], b); 103 | if (a->ch[1]) merge(a->ch[1], b); 104 | insert(b, a->v, a->idx); 105 | delete a; 106 | a = NULL; 107 | } 108 | int fa[MAXN]; 109 | int get(int x) { 110 | int tee = fa[x], q; 111 | while (tee != fa[tee]) tee = fa[tee]; 112 | while (x != tee) { 113 | q = fa[x]; fa[x] = tee; x = q; 114 | } 115 | return tee; 116 | } 117 | void add(int s, int t) { 118 | int u = get(s), v = get(t); 119 | if (u != v) { 120 | if (root[u]->s < root[v]->s) { 121 | fa[u] = v; merge(root[u], root[v]); 122 | } else { 123 | fa[v] = u; merge(root[v], root[u]); 124 | } 125 | } 126 | } 127 | int w[MAXN]; 128 | int u, v; 129 | int n, m; 130 | char buf[12]; 131 | int main() { 132 | scanf("%d%d", &n, &m); 133 | g(i, 1, n) { 134 | fa[i] = i; 135 | scanf("%d", w + i); 136 | root[i] = new node(w[i]); 137 | root[i]->idx = i; 138 | } 139 | f(i, 0, m) { 140 | scanf("%d%d", &u, &v); 141 | add(u, v); 142 | } 143 | scanf("%d", &m); 144 | while (m--) { 145 | scanf("%s%d%d", buf, &u, &v); 146 | if (buf[0] == 'Q') { 147 | u = get(u); 148 | if (root[u]->s < v) { 149 | puts("-1"); 150 | } else { 151 | printf("%d\n", getKth(root[u], v)); 152 | } 153 | } else { 154 | add(u, v); 155 | } 156 | } 157 | return 0; 158 | } 159 | -------------------------------------------------------------------------------- /generate_cpp_list.sh: -------------------------------------------------------------------------------- 1 | SPATH='.' 2 | SS='python cpplint.py' 3 | FILELIST(){ 4 | for filename in $(ls $SPATH) 5 | do 6 | SPATH=`pwd` 7 | if [ -f $filename ];then 8 | # exec 'cpplint' $filename 9 | SS=$SS' '$SPATH'/'$filename 10 | elif [ -d $filename ];then 11 | #echo Directory: $filename 12 | cd $filename 13 | SPATH=`pwd` 14 | # echo $SPATH 15 | FILELIST 16 | cd .. 17 | else 18 | echo "$SPATH/$filename is not a common file." 19 | fi 20 | done 21 | } 22 | cd $SPATH 23 | FILELIST 24 | echo $SS 25 | # echo "Done." 26 | -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @name test.cpp 3 | * @subject For CI Testing 4 | * @author SCaffrey(sirius.caffrey@gmail.com) 5 | * @Copyright DWTFABG 6 | * @comment This is a file for CI testing. 7 | */ 8 | #include 9 | #include 10 | #include 11 | 12 | #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) 13 | 14 | char buf[100033], tee[1033]; 15 | int id = 0; 16 | int main() { 17 | system("bash generate_cpp_list.sh > file.list"); 18 | // return 0; 19 | freopen("file.list", "r", stdin); 20 | gets(buf); 21 | // printf("%d\n",id); 22 | // f(i,0,id)puts(buf[i]); 23 | // return 0; 24 | // freopen("test_log.txt","w",stdout); 25 | system(buf); 26 | freopen("files.log", "r", stdin); 27 | while (gets(tee)) { 28 | if (strstr(tee, "Total")) { 29 | if (strcmp(tee, "Total errors found: 0")) { 30 | fprintf(stderr, "%s\n", tee); 31 | return 1; 32 | } else { 33 | fprintf(stderr, "Passed CppLint Tests!\n"); 34 | return 0; 35 | } 36 | } 37 | // puts(tee); 38 | } 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------