Poj 3466 a simple problem with Integers

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3468 http://poj.org/problem? Id = 3468 http://poj.org/problem? Id = 3468

Idea: this is a question about interval modification interval query. Because the data in the question is relatively large, it is definitely not feasible to use a single modification or query .... Note that the data may be relatively large. Use _ int64 or long to store data .....

Code:

# Include <stdio. h> # include <math. h >#define L (u) (U <1) # define R (u) (U <1 | 1) const int M = 100010; struct node {_ int64 L, R; _ int64 add; long sum;} node [M * 4] ;__ int64 A [m]; void pushup (_ int64 U) {node [u]. sum = node [L (u)]. sum + node [R (u)]. SUM; return;} void Pushdown (_ int64 U) {node [L (u)]. add + = node [u]. add; node [L (u)]. sum + = (node [L (u)]. r-node [L (u)]. L + 1) * node [u]. add; node [R (u)]. add + = node [u]. add; node [R (u)]. sum + = (node [R (u)]. r-node [R (u)]. L + 1) * node [u]. add; node [u]. add = 0;} void build (_ int64 U ,__ int64 left ,__ int64 right) {node [u]. L = left; node [u]. R = right; node [u]. add = 0; If (Left = right) {node [u]. sum = A [left]; return;} _ int64 mid = (node [u]. L + node [u]. r)/2; build (L (u), left, mid); Build (R (u), Mid + 1, right); pushup (U );} void Update (_ int64 U ,__ int64 left ,__ int64 right ,__ int64 v) {If (left <= node [u]. L & node [u]. r <= right) {node [u]. add + = V; node [u]. sum + = (node [u]. r-node [u]. L + 1) * V; return;} // node [u]. sum + = (right-left + 1) * V; // The range represented by the current node is not the subinterval of the query interval if (node [u]. add) Pushdown (U); // analyze whether the current node's laziness mark is 0, if the value is not 0, update the data _ int64 mid = (node [u]. L + node [u]. r)/2; If (right <= mid) Update (L (u), left, right, V); else if (left> mid) Update (R (u ), left, right, V); else {Update (L (u), left, mid, V); Update (R (u), Mid + 1, right, V );} node [u]. sum = node [L (u)]. sum + node [R (u)]. sum ;:__ int64 query (_ int64 U ,__ int64 left ,__ int64 right) {If (left <= node [u]. L & node [u]. r <= right) {return node [u]. SUM;} If (node [u]. add) Pushdown (U); <span style = "font-family: Arial, Helvetica, sans-serif;"> // analyze whether the lazy mark of the current node is 0, if the value is not 0, update data for the child node </span> _ int64 mid = (node [u]. L + node [u]. r)/2; If (right <= mid) return query (L (u), left, right); else if (left> mid) return query (R (u ), left, right); else {return (query (L (u), left, mid) + query (R (u), Mid + 1, right ));}} int main () {_ int64 n, m, I, x, y, z; while (scanf ("% i64d % i64d", & N, & M) = 2) {for (I = 1; I <= N; I ++) {scanf ("% i64d", & A [I]);} build (1, 1, n); char STR [5]; for (I = 0; I <m; I ++) {scanf ("% s", STR ); if (STR [0] = 'C') {scanf ("% i64d % i64d % i64d", & X, & Y, & Z); Update (1, x, y, z);} else {scanf ("% i64d % i64d", & X, & Y); printf ("% i64d \ n", query (1, x, y) ;}}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.