diff --git a/.gitignore b/.gitignore index de72442..c4d1050 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,5 @@ /DSLib/Test/SegmentTree/Debug /DSLib/Deps /DSLib/out +/DSLib/Client/FenwickTree/Debug +/DSLib/Lib/FenwickTree/Debug diff --git a/DSLib/Client/FenwickTree/FenwickTree.cpp b/DSLib/Client/FenwickTree/FenwickTree.cpp new file mode 100644 index 0000000..66a6f7b --- /dev/null +++ b/DSLib/Client/FenwickTree/FenwickTree.cpp @@ -0,0 +1,16 @@ +#include "FenwickTree.h" + +#include + +int main(int argc, char* argv[]) +{ + std::vector data = { 1, 2, 3, 4, 5, 6, 7 }; + ds::fen_tree f(data.size()); + for (std::size_t i = 0; i < data.size(); ++i) + { + f.update_point(i + 1, data[i]); + } + + std::cout << f.sum(data.size()) << std::endl; + return 0; +} diff --git a/DSLib/Client/FenwickTree/FenwickTree.vcxproj b/DSLib/Client/FenwickTree/FenwickTree.vcxproj new file mode 100644 index 0000000..691fcd2 --- /dev/null +++ b/DSLib/Client/FenwickTree/FenwickTree.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {37248D29-4153-4C53-9BB8-DD16AB36C257} + Win32Proj + FenwickTree + 10.0.16299.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + "$(SolutionDir)Lib\FenwickTree" + + + true + Console + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + "$(SolutionDir)Lib\FenwickTree" + + + true + Console + + + + + Level3 + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + "$(SolutionDir)Lib\FenwickTree" + + + true + true + true + Console + + + + + Level3 + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + "$(SolutionDir)Lib\FenwickTree" + + + true + true + true + Console + + + + + + + + + \ No newline at end of file diff --git a/DSLib/Client/FenwickTree/FenwickTree.vcxproj.filters b/DSLib/Client/FenwickTree/FenwickTree.vcxproj.filters new file mode 100644 index 0000000..0437fd5 --- /dev/null +++ b/DSLib/Client/FenwickTree/FenwickTree.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/DSLib/DSLib.sln b/DSLib/DSLib.sln index af69431..44fbe45 100644 --- a/DSLib/DSLib.sln +++ b/DSLib/DSLib.sln @@ -43,6 +43,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SegmentTree", "Client\Segme EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SegmentTree", "Test\SegmentTree\SegmentTree.vcxproj", "{A740815A-4E10-4116-A01E-73D71286FBA6}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FenwickTree", "Lib\FenwickTree\FenwickTree.vcxproj", "{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FenwickTree", "Client\FenwickTree\FenwickTree.vcxproj", "{37248D29-4153-4C53-9BB8-DD16AB36C257}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -179,6 +183,22 @@ Global {A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x64.Build.0 = Release|x64 {A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x86.ActiveCfg = Release|Win32 {A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x86.Build.0 = Release|Win32 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x64.ActiveCfg = Debug|x64 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x64.Build.0 = Debug|x64 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x86.ActiveCfg = Debug|Win32 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x86.Build.0 = Debug|Win32 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x64.ActiveCfg = Release|x64 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x64.Build.0 = Release|x64 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x86.ActiveCfg = Release|Win32 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x86.Build.0 = Release|Win32 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x64.ActiveCfg = Debug|x64 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x64.Build.0 = Debug|x64 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x86.ActiveCfg = Debug|Win32 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x86.Build.0 = Debug|Win32 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x64.ActiveCfg = Release|x64 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x64.Build.0 = Release|x64 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x86.ActiveCfg = Release|Win32 + {37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -201,6 +221,8 @@ Global {EB5FC754-BC16-4B5E-80C3-3D7AC23A63D2} = {6FE9DCA5-B3F2-4593-B71E-3B9BC3A59723} {4EBF5131-F65F-4F3B-8C78-A9706EE1F495} = {A97B1356-3EAC-40A4-B591-E557CA356147} {A740815A-4E10-4116-A01E-73D71286FBA6} = {2943D6FC-5010-421B-9250-B7CC6BB2E118} + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F} = {6FE9DCA5-B3F2-4593-B71E-3B9BC3A59723} + {37248D29-4153-4C53-9BB8-DD16AB36C257} = {A97B1356-3EAC-40A4-B591-E557CA356147} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {83EEE417-9039-4161-A830-DDDEECE454CD} diff --git a/DSLib/Lib/FenwickTree/FenwickTree.cpp b/DSLib/Lib/FenwickTree/FenwickTree.cpp new file mode 100644 index 0000000..089d6aa --- /dev/null +++ b/DSLib/Lib/FenwickTree/FenwickTree.cpp @@ -0,0 +1 @@ +#include "FenwickTree.h" \ No newline at end of file diff --git a/DSLib/Lib/FenwickTree/FenwickTree.h b/DSLib/Lib/FenwickTree/FenwickTree.h new file mode 100644 index 0000000..f94747f --- /dev/null +++ b/DSLib/Lib/FenwickTree/FenwickTree.h @@ -0,0 +1,48 @@ +#pragma once +#include + +namespace ds +{ + class fen_tree + { + public: + explicit fen_tree(const std::size_t size) :size_(size + 1) + { + f_.assign(size + 1, 0); + } + + void update_point(std::size_t i, std::size_t value); + std::size_t sum(std::size_t i) const; + std::size_t range_sum(std::size_t i, std::size_t j) const; + + private: + std::vector f_; + std::size_t size_; + }; + + inline void fen_tree::update_point(std::size_t i, const std::size_t value) + { + while (i < size_) + { + f_[i] += value; + i += i & -i; + } + } + + inline std::size_t fen_tree::sum(std::size_t i) const + { + std::size_t s = 0; + while (i > 0) + { + s += f_[i]; + i -= i & -i; + } + + return s; + } + + inline std::size_t fen_tree::range_sum(std::size_t i, std::size_t j) const + { + return sum(j) - (i == 1 ? 0 : sum(i - 1)); + } +} diff --git a/DSLib/Lib/FenwickTree/FenwickTree.vcxproj b/DSLib/Lib/FenwickTree/FenwickTree.vcxproj new file mode 100644 index 0000000..dc60f11 --- /dev/null +++ b/DSLib/Lib/FenwickTree/FenwickTree.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F} + Win32Proj + FenwickTree + 10.0.16299.0 + + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + + + Level3 + Disabled + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + + + Level3 + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + Level3 + MaxSpeed + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/DSLib/Lib/FenwickTree/FenwickTree.vcxproj.filters b/DSLib/Lib/FenwickTree/FenwickTree.vcxproj.filters new file mode 100644 index 0000000..c485a14 --- /dev/null +++ b/DSLib/Lib/FenwickTree/FenwickTree.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + + + Source Files + + + \ No newline at end of file