Introduction to line tree

Source: Internet
Author: User
# Include <iostream> # include <cmath> # include <cstring> # include <algorithm> using namespace STD; int n, m; int A [100001]; // Number of leaf knots N _ int64 ans; struct {int L, R; _ int64 sum, add; // _ int64} tree [400000]; // In the number, the number of all nodes is about 2 ^ 2 * n layers. logn H = logn + 1 or + 2. Each insert update cannot exceed O (logn) void build (INT l, int R, int I) {tree [I]. L = L; tree [I]. R = r; tree [I]. add = 0; If (L = r) {tree [I]. sum = A [l]; // The value of the leaf node = return of the input array;} int mid = (L + r)/2; build (L, mid, 2 * I); Build (Mid + 1, R, 2 * I + 1); tree [I]. sum = tree [2 * I]. sum + tree [2 * I + 1]. SUM;} void updata (int l, int R, int add, int I) {If (tree [I]. l> r | tree [I]. r <L) return; If (tree [I]. l> = L & tree [I]. r <= r) {tree [I]. sum + = (tree [I]. r-tree [I]. L + 1) * Add; tree [I]. add + = add; return; // lazy is only added to break down a unique subinterval} If (tree [I]. add) // lazy edge update edge maintenance in order to ensure that the child's ADD and sum are correct, the meaning of the current add = 0 (updated to the bottom) after the update {tree [2 * I]. sum + = (tree [2 * I]. r-tree [2 * I]. L + 1) * tree [I]. add; tree [2 * I]. add + = tree [I]. add; tree [2 * I + 1]. sum + = (tree [2 * I + 1]. r-tree [2 * I + 1]. L + 1) * tree [I]. add; tree [2 * I + 1]. add + = tree [I]. add; tree [I]. add = 0;} updata (L, R, add, 2 * I); updata (L, R, add, 2 * I + 1); tree [I]. sum = tree [2 * I]. sum + tree [2 * I + 1]. SUM;} void query (int l, int R, int I) {If (tree [I]. l> r | tree [I]. r <L) return; If (tree [I]. l> = L & tree [I]. r <= r) {Ans + = tree [I]. SUM; return;} If (tree [I]. add) // lazy side query side maintenance {tree [2 * I]. sum + = (tree [2 * I]. r-tree [2 * I]. L + 1) * tree [I]. add; tree [2 * I]. add + = tree [I]. add; tree [2 * I + 1]. sum + = (tree [2 * I + 1]. r-tree [2 * I + 1]. L + 1) * tree [I]. add; tree [2 * I + 1]. add + = tree [I]. add; tree [I]. add = 0;} query (L, R, 2 * I); query (L, R, 2 * I + 1); tree [I]. sum = tree [2 * I]. sum + tree [2 * I + 1]. SUM;} int main () {// freopen ("in.txt", "r", stdin); int I, X, Y, Z; char C; while (scanf ("% d", & N, & M )! = EOF) {for (I = 1; I <= N; I ++) scanf ("% d", & A [I]); Build (1, n, 1); scanf ("% * C"); for (I = 1; I <= m; I ++) {scanf ("% C", & C ); if (C = 'q') {scanf ("% d % * C", & X, & Y); ans = 0; query (x, y, 1); printf ("% i64d \ n", ANS);} else {scanf ("% d % * C", & X, & Y, & Z); updata (X, Y, Z, 1) ;}} return 0 ;}

Analyzed the code annotation of the first-line segment tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.