版本比较
线段树 树状数组
正文
增加 4 行删除 3 行
21 行未改动22232425262627282930303132333411 行未改动4647484950505152535427 行未改动82838485868788899045 行未改动21 行未改动void pushdown(int p) //标志性的线段树代码{ if (tree[p].lazy) { int mid =(tree[p].right - tree[p].left + 1); int mid = tree[p].left + tree[p].right >> 1; tree[p << 1].lazy += tree[p].lazy; //lazy传给左儿子 tree[p << 1].data += tree[p].lazy * (mid - tree[p << 1].left + 1); //更新左儿子的data tree[p << 1 | 1].lazy += tree[p].lazy; //lazy传给右儿子 tree[p << 1 | 1].data += tree[p].lazy * (tree[p << 1].right - mid); //更新右儿子的data tree[p << 1 | 1].data += tree[p].lazy * (tree[p << 1 | 1].right - mid); //更新右儿子的data tree[p].lazy = 0; //父节点的lazy清零 } } 11 行未改动 int mid = L + R >> 1; //(L+R)/2 build(p << 1, L, mid); //递归建左子树 build(p << 1 | 1, mid + 1, R); ////递归建右子树 tree[p].data = tree[p >> 1].data + tree[p << 1 | 1].data; //回溯时维护数据信息 tree[p].data = tree[p << 1].data + tree[p << 1 | 1].data; //回溯时维护数据信息}
int query(int p, int L, int R) //在以p为根节点的线段树中,查询区间[L,R] {27 行未改动 cout.tie(0); return 0;}
```线段树练习: https://www.luogu.com.cn/problem/P3372
45 行未改动