Hdu 1166 enemy deployment array (line segment Tree, tree array)

Source: Internet
Author: User

# Include <iostream> # include <cstdio> # include <cstring> using namespace std; struct node {int l; // left start point int r; // right end point int sum; // interval data sum}; node p [200004]; int a [50001]; int n; void bicould (int k, int l, int r) // create {int mid; p [k]. l = l; p [k]. r = r; if (l = r) {p [k]. sum = a [l]; return;} mid = (l + r)/2; biwould (k * 2, l, mid); biwould (k * 2 + 1, mid + 1, r); p [k]. sum = p [k * 2]. sum + p [k * 2 + 1]. sum; return;} int find (int k, int l, int r) // The value of the search interval {int mid; if (p [k]. l = l & p [k]. r = r) // locate the {return p [k] range. sum;} mid = (p [k]. l + p [k]. r)/2; if (l> mid) // right child in the midpoint {return find (k * 2 + 1, l, r );} if (r <= mid) // click the left child {return find (k * 2, l, r);} return find (k * 2, l, mid) + find (k * 2 + 1, mid + 1, r);} void update (int k, int x, int y) // update data {int mid; if (p [k]. l = x & p [k]. r = x) // if this point is found {p [k]. sum = y; // update data return;} mid = (p [k]. l + p [k]. r)/2; if (x <= mid) {update (k * 2, x, y);} else {update (k * 2 + 1, x, y);} p [k]. sum = p [k * 2]. sum + p [k * 2 + 1]. sum; return;} int main () {int t, I, k, x, y, m; char cz [7]; scanf ("% d", & t ); k = 0; while (t --) {k ++; scanf ("% d", & n); for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]);} biwould (, n); printf ("Case % d: \ n", k ); while (1) {scanf ("% s", & cz); if (cz [0] = 'E') {break ;} scanf ("% d", & x, & y); switch (cz [0]) {case 'q': {printf ("% d \ n ", find (1, x, y); break;} case 'A': {A [x] + = y; m = a [x]; update (1, x, m); break;} case's ': {a [x]-= y; m = a [x]; update (1, x, m );}}}} return 0 ;}

 

Related Article

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.