Conversation
|
🤭 感谢你的提交,请检查你的改动是否符合以下项目规范。 1. 格式化我们项目中各种编程语言代码(包括文档)所采用的格式化工具不同,提交 pr 之前必须确保代码、文档正确格式化。
2. Git 提交信息我们项目遵循 AngularJS Git Commit Message Conventions 规范,我们希望你的提交信息尽可能与项目保持一致。
3. 其它补充新增题解及代码时,需要创建 Solution.xxx 源代码文件(如果已存在,请确认算法是否更优,是则覆盖已有算法代码),同时,需要在 README.md 以及 README_EN.md 中添加对应的代码片段(英文文件中不要出现中文注释) 🤭 Thank you for your contribution. Please check if your changes comply with the following project specifications. 1. FormattingWe use different formatting tools for various programming languages (including documentation) in our project. You must ensure that the code and documentation are correctly formatted before submitting a pr.
2. Git Commit MessageOur project follows the AngularJS Git Commit Message Conventions. We hope that your submission information is as consistent as possible with the project.
3. Other notesWhen adding solutions and code, you need to create a Solution.xxx source code file (if it already exists, please confirm whether the algorithm is better, if yes, overwrite the existing algorithm code), and at the same time, you need to add the corresponding code snippets in README.md and README_EN.md (do not have Chinese comments in the English file) |
Fix TLE on LeetCode 3655 – XOR After Range Multiplication Queries II
What changed:
Replaced the heap-based propagation approach with an O(n + q log q) lazy stride multiplier sweep that passes all 605/605 test cases.
Why the old approach failed:
The heap re-inserted (position, k, multiplier) tuples at every step, causing up to O(n·q·log n) operations when stride k=1. This caused a Time Limit Exceeded on test case 604/605.
How the new approach works:
Instead of iterating each query range explicitly, we place two events per query:
A start event at index l — begins multiplying by v
A cancel event at last_valid_index + k — applies modular inverse of v to stop the effect
We then sweep each residue class mod k independently, propagating a running multiplier. Every index is visited exactly once, making the total sweep O(n).
Results:
✅ Passes 605/605 LeetCode test cases
✅ 300 random stress tests verified against brute-force
✅ All edge cases covered (single element, out-of-bounds cancel, large values, overlapping queries)
Files updated:
solution.py — rewritten algorithm
test_solution.py — full test suite added
README.md — algorithm explanation and walkthrough
README.md
test_solution.py
solution.py
PULL_REQUEST.md