PKU3468 (A Simple Problem with Integers) segment update

Source: Internet
Author: User

[Cpp]/********************************** * ********** the subject is as follows: Q a B: query the sum of [a, B]; C a B x: update the range [a, B]; add x to all values of the range; algorithm idea: segment update of the Line Segment tree-delayed update; add a delayed node when querying and updating the interval; each time you want to query or update the interval to this interval; the node information is then transmitted to the left and right nodes of the child. This greatly reduces the overhead of Time and Space. algorithm process: Each update does not need to be updated to the leaf node; you only need to update it to the corresponding segment, and then record an add. When you update or query the next time, if you want to find the subnode of the segment, add the add to the subnode, set the add value to 0. In this way, the complexity of the subinterval query is the complexity of the update; **************************************** * ******/# include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace std; # define L l, m, u <1 # define R m + 1, r, u <1 | 1 typedef long LL; const int N = 111111; LL add [N <2]; LL sum [N <2]; void PushUp (int u) // update the information of the current node to the parent node {sum [u] = sum [u <1] + sum [u <1 | 1];} void PushDown (int u, int m) // update the information of the current node to the son node {if (add [u]) {add [u <1] + = add [u]; add [u <1 | 1] + = add [u]; sum [u <1] + = add [u] * (m-(m> 1 )); sum [u <1 | 1] + = add [u] * (m> 1); add [u] = 0 ;}} void build (int l, int r, int u) {add [u] = 0; if (l = r) {scanf ("% lld", & sum [u]); return ;} int m = (l + r)> 1; build (L); build (R); PushUp (u);} void update (int l1, int r1, int c, int l, int r, int u) {if (l1 <= l & r <= r1) {add [u] + = c; sum [u] + = (LL) c * (r-l + 1); return;} PushDown (u, r-l + 1 ); int m = (l + r)> 1; if (l1 <= m) update (l1, r1, c, L); if (m <r1) update (l1, r1, c, R); PushUp (u);} LL query (int l1, int r1, int l, int r, int u) {if (l1 <= l & r <= r1) {return sum [u];} PushDown (u, r-l + 1 ); int m = (l + r)> 1; LL res = 0; if (l1 <= m) res + = query (l1, r1, L ); if (m <r1) res + = query (l1, r1, R); return res;} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt "," r ", stdin); int N, Q; scanf (" % d ", & N, & Q); build (1, N, 1); while (Q --) {char op [2]; int a, B, c; scanf ("% s ", op); if (op [0] = 'q') {scanf ("% d", & a, & B ); printf ("% lld \ n", query (a, B, 1, N, 1);} else {scanf ("% d", &, & B, & c); update (a, B, c, 1, N, 1) ;}} return 0 ;}

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.