diff --git "a/exercises/huanhuilong/\344\270\255\347\255\211/\346\237\245\346\211\276\347\202\271\345\234\250\350\207\252\347\204\266\345\214\272\351\227\264\347\232\204\345\235\220\346\240\207/solution.cpp" "b/exercises/huanhuilong/\344\270\255\347\255\211/\346\237\245\346\211\276\347\202\271\345\234\250\350\207\252\347\204\266\345\214\272\351\227\264\347\232\204\345\235\220\346\240\207/solution.cpp" index 0159b0967a245fec852757f3458680b9f93448e2..5e7f1e716916b276c48e297f9bd19cba68554da1 100644 --- "a/exercises/huanhuilong/\344\270\255\347\255\211/\346\237\245\346\211\276\347\202\271\345\234\250\350\207\252\347\204\266\345\214\272\351\227\264\347\232\204\345\235\220\346\240\207/solution.cpp" +++ "b/exercises/huanhuilong/\344\270\255\347\255\211/\346\237\245\346\211\276\347\202\271\345\234\250\350\207\252\347\204\266\345\214\272\351\227\264\347\232\204\345\235\220\346\240\207/solution.cpp" @@ -8,113 +8,113 @@ class Range { public: - Range():m_pos(0), m_length(0){}; - - Range(uint64_t pos, uint64_t length): m_pos(pos), m_length(length){} - - Range(Range const & rhs){ - if (this == &rhs) - return; - m_pos = rhs.m_pos; - m_length = rhs.m_length; - } - Range& operator = (Range const & rhs){ - if (this == &rhs) - return *this; - m_pos = rhs.m_pos; - m_length = rhs.m_length; - return *this; - } - - ~Range(){} - - bool operator<(Range const & rhs) const{ + Range():m_pos(0), m_length(0){}; + + Range(uint64_t pos, uint64_t length): m_pos(pos), m_length(length){} + + Range(Range const & rhs){ + if (this == &rhs) + return; + m_pos = rhs.m_pos; + m_length = rhs.m_length; + } + Range& operator = (Range const & rhs){ + if (this == &rhs) + return *this; + m_pos = rhs.m_pos; + m_length = rhs.m_length; + return *this; + } + + ~Range(){} + + bool operator<(Range const & rhs) const{ return m_pos < rhs.m_pos || (m_pos == rhs.m_pos && m_length < rhs.m_length); } - uint64_t GetPos() const { + uint64_t GetPos() const { return m_pos; } - uint64_t GetLength() const{ + uint64_t GetLength() const{ return m_length; } - uint64_t GetRight() const{ + uint64_t GetRight() const{ return m_pos+m_length; } public: - uint64_t m_pos; - uint64_t m_length; + uint64_t m_pos; + uint64_t m_length; }; size_t findNatureRangeIndex(std::vector& rangeList, uint64_t point){ - size_t n = rangeList.size(); - size_t first = 0; - size_t last = 2 * n + 1; - size_t max = last; - - uint64_t testBeg = 0; - uint64_t testEnd = 0; - size_t idx = SIZE_MAX; - while (first <= last) { - size_t mid = (first + last) / 2; // compute mid point. + size_t n = rangeList.size(); + size_t first = 0; + size_t last = 2 * n + 1; + size_t max = last; + + uint64_t testBeg = 0; + uint64_t testEnd = 0; + size_t idx = SIZE_MAX; + while (first <= last) { + size_t mid = (first + last) / 2; // compute mid point. std::cout<<"xx:"<= max - 1) { - uint64_t lastEnd = rangeList[n - 1].m_pos + rangeList[n - 1].m_length; + // 处理右边界 + if (mid >= max - 1) { + uint64_t lastEnd = rangeList[n - 1].m_pos + rangeList[n - 1].m_length; std::cout<<"point:"<=lastEnd) { - idx = max - 1; - } else { - idx = max - 2; - } - break; - } - - // 处理左边界 - if (mid == 0) { - if (point >= rangeList[0].m_pos) { - idx = 1; - } else { - idx = 0; - } - break; - } - - // 处理中间 - bool even = mid % 2 == 0; - if (even) { - size_t left = (mid - 1) / 2; - size_t right = left + 1; - testBeg = rangeList[left].m_pos + rangeList[left].m_length; - testEnd = rangeList[right].m_pos; - } else { - size_t current = mid / 2; - testBeg = rangeList[current].m_pos; - testEnd = rangeList[current].m_pos + rangeList[current].m_length; - } - - // 二分查找 + if (point>=lastEnd) { + idx = max - 1; + } else { + idx = max - 2; + } + break; + } + + // 处理左边界 + if (mid == 0) { + if (point >= rangeList[0].m_pos) { + idx = 1; + } else { + idx = 0; + } + break; + } + + // 处理中间 + bool even = mid % 2 == 0; + if (even) { + size_t left = (mid - 1) / 2; + size_t right = left + 1; + testBeg = rangeList[left].m_pos + rangeList[left].m_length; + testEnd = rangeList[right].m_pos; + } else { + size_t current = mid / 2; + testBeg = rangeList[current].m_pos; + testEnd = rangeList[current].m_pos + rangeList[current].m_length; + } + + // 二分查找 - if (point>=testEnd) { + if (point>=testEnd) { std::cout<<"->"<