Hdu1166 line segment tree

Source: Internet
Author: User
# Include <iostream> # include <cstdio> # include <cstring> # define n 50000 using namespace STD; struct node {int L, R; long sum ;}; node tree [N * 3]; void buildtree (int l, int R, int POS) {tree [POS]. L = L; tree [POS]. R = r; If (L = r) {scanf ("% i64d", & tree [POS]. sum); return;} int mid = (L + r)> 1; buildtree (L, mid, POS * 2); buildtree (Mid + 1, R, pos * 2 + 1); tree [POS]. sum = tree [POS * 2]. sum + tree [POS * 2 + 1]. SUM;} long query (int l, int R, int POS) {If (L = tree [POS]. L & R = tree [POS]. r) return tree [POS]. SUM; else if (r <= (tree [POS]. L + tree [POS]. r)/2) return query (L, R, POS <1); else if (L> = (tree [POS]. L + tree [POS]. r)/2 + 1) return query (L, R, (Pos <1) + 1); elsereturn query (L, tree [POS * 2]. r, POS * 2) + query (tree [POS * 2 + 1]. l, R, POS * 2 + 1);} void Update (INT POs, int K, int add) {If (k> = tree [POS]. L & K <= tree [POS]. r) {tree [POS]. sum + = add; If (tree [POS]. L = tree [POS]. r) return; Update (Pos * 2, K, add); Update (Pos * 2 + 1, K, add) ;}return ;} int main () {int T; scanf ("% d", & T); int CAS = 1; while (t --) {int N; scanf ("% d", & N); int I; printf ("case % d: \ n", CAS ++); buildtree (1, n, 1); While (1) {char STR [20]; scanf ("% s", STR); If (STR [0] = 'E') break; else if (STR [0] = 'q ') {int T1, T2; scanf ("% d", & T1, & T2); printf ("% i64d \ n", query (T1, T2, 1);} else if (STR [0] ='s ') {int T1, T2; scanf ("% d", & T1, & T2 ); update (1, T1,-t2);} else if (STR [0] = 'A') {int T1, T2; scanf ("% d ", & T1, & T2); Update (1, T1, T2 );}}}}

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.