Poj a simple problem with integers [Line Segment tree, Interval Update]

Source: Internet
Author: User

You have n integers, A1, A2 ,..., One. You need to process two types of operations. One type of operation is to add some given numbers at a given interval. The other is the sum of the required quantities at a given interval.

Difficulties: the lazy mark is mainly used to identify the problems. In fact, the lazy mark is that not all the currently changed values are updated. It will be updated when used, which saves a lot of time.

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

Code:

# Include <stdio. h> # include <string. h> # define maxn 100005 # define lc l, M, RT <1 # define RC m + 1, R, RT <1 | 1 # define ll _ int64ll sum [maxn <2], add [maxn <2]; // The add array is the void pushup (int rt) to be added for temporary storage) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Pushdown (int rt, int m) // This is difficult, after understanding this question, it is almost the same {If (add [RT]) {Add [RT <1] + = add [RT]; add [RT <1 | 1] + = add [RT]; sum [RT <1] + = add [RT] * (m-(M> 1 )); sum [RT <1 | 1] + = add [RT] * (M> 1); add [RT] = 0 ;}} void creat (INT l, int R, int RT) {Add [RT] = 0; If (L = r) {scanf ("% i64d", & sum [RT]); return ;} int M = (L + r)> 1; creat (LC); creat (RC); pushup (RT);} void Update (INT le, int Ri, int num, int L, int R, int RT) {If (Le <= L & R <= RI) {Add [RT] + = num; sum [RT] + = (LL) num * (R-l + 1); return;} Pushdown (RT, R-l + 1 ); int M = (L + r)> 1; if (Le <= m) Update (Le, RI, num, LC); If (RI> m) Update (Le, ri, num, RC); pushup (RT);} ll query (INT le, int Ri, int L, int R, int RT) {If (Le <= L & R <= RI) {return sum [RT];} Pushdown (RT, R-l + 1); LL res = 0; ll M = (L + r)> 1; if (Le <= m) RES + = query (Le, RI, LC); If (RI> m) res + = query (Le, RI, RC); Return res;} int main () {int n, m; scanf ("% d", & N, & M); creat (1, n, 1); char s [2]; int A, B, C; while (M --) {scanf ("% s ", s); If (s [0] = 'q') {scanf ("% d", & A, & B ); printf ("% i64d \ 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.