If you have a brute force solution runs 10 seconds, but time limit is 2 seconds, stop thinking the correct solution, go to optimize your brute force solution.
Problem: https://qoj.ac/problem/8616
This problem is weird: it requires tree path addition and tree path XOR sum queries.
If the queries were on a sequence rather than a tree, use this approach and you will get an $$$O(q\sqrt{n}\log n\log V)$$$ solution. By combining this with heavy-light decomposition, we can solve the tree version in $$$O(q\sqrt{n} \log^2 n\log V)$$$ time with a high constant factor, cannot pass the time limit. It's even slower than brute force under these constraints!
Now we think of brute force.
The basic brute force approach is, for two nodes $$$u$$$ and $$$v$$$, find their LCA, and move both $$$u,v$$$ upward toward their LCA, update all the elements on the path. This is slow, isn't it? On modern computers, contiguous memory access is faster than random memory access. So we can use heavy-light decomposition to turn the random accesses into $$$O(\log n)$$$ contiguous segments. Then, with #pragma GCC optimize("O3,unroll-loops") and #pragma GCC target("avx2,sse4.2,avx512f"), so you can get accepted on this problem.
Try it! https://qoj.ac/problem/8616








Auto comment: topic has been updated by __yql0991888__ (previous revision, new revision, compare).
Computers are fast nowadays, so we can solve this problem in O(nq).
I believe you have learned this trick. So lets do a little exercise now!
我常常追忆过去。
生命瞬间定格在脑海。我将背后的时间裁剪、折叠、蜷曲,揉捻成天上朵朵白云。
云朵之间亦有分别:积云厚重,而卷云飘渺。生命里震撼的场景掠过我的思绪便一生无法忘怀,而更为普通平常的记忆在时间的冲刷下只留下些许残骸。追忆宛如入梦,太过清楚则无法愉悦自己的幻想,过分模糊却又坠入虚无。只有薄雾间的山水,面纱下的女子,那恰到好处的朦胧,才能满足我对美的苛求。
追忆总在不经意间将我裹进泛黄的纸页里。分别又重聚的朋友,推倒又重建的街道,种种线索协助着我从一个具体的时刻出发沿时间的河逆流而上。曾经的日子无法重来,我只不过是一个过客。但我仍然渴望在每一次追忆之旅中留下闲暇时间,在一个场景前驻足,在岁月的朦胧里瞭望过去的自己,感受尽可能多的甜蜜。美好的时光曾流过我的身体,我便心满意足。
过去已经凝固,我带着回忆向前,只是时常疏于保管,回忆也在改变着各自的形态。这给我的追忆旅程带来些许挑战。
我该在哪里停留?我问我自己。