You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
intgetsum(int l, int r, int s, int t, int p) {
// [l, r] 为查询区间, [s, t] 为当前节点包含的区间, p 为当前节点的编号if (l <= s && t <= r)
return d[p]; // 当前区间为询问区间的子集时直接返回当前区间的和int m = s + ((t - s) >> 1), sum = 0;
if (l <= m) sum += getsum(l, r, s, m, p * 2);
// 如果左儿子代表的区间 [s, m] 与询问区间有交集, 则递归查询左儿子if (r > m) sum += getsum(l, r, m + 1, t, p * 2 + 1);
// 如果右儿子代表的区间 [m + 1, t] 与询问区间有交集, 则递归查询右儿子return sum;
}
请选择:
我正在访问这个页面
https://oi-wiki.org/ds/seg/#%E7%BA%BF%E6%AE%B5%E6%A0%91%E7%9A%84%E5%8C%BA%E9%97%B4%E6%9F%A5%E8%AF%A2
我发现页面有这样的问题
在上述函数中,该处出现错误
我们以10个元素举例,此时s=1,t=10,作图可得m=6。带入上式,可以得到m=5,与预期不符,出现错误。
出现此问题的原因是线段树不是一个完全二叉树,不可以直接计算得出中值。
The text was updated successfully, but these errors were encountered: