Hdu1166-deployment of enemy troops (line segment tree)

Source: Internet
Author: User

// File Name: hdu1_6.cpp // Author: bo_jwolf // Created Time: friday, August 16, 2013, # include <vector> # include <list> # include <map> # include <set> # include <deque> # include <stack> # include <bitset> # include <algorithm> # include <functional> # include <numeric> # include <utility> # include <sstream> # include <iostream> # include <iomanip> # include <cstdio> # include <cmath> # include <cstdlib> # include <cstring> # include <ctime> using namespace std; # define lson l, mid, rt <1 # define rson mid + 1, r, rt <1 | 1 const int maxn = 55555; // int sum [maxn <2]; struct node {int sum;} tree [maxn <2]; void PushUp (int rt) {tree [rt]. sum = tree [rt <1]. sum + tree [(rt <1 | 1)]. sum;} void build (int l, int r, int rt) {if (l = r) {scanf ("% d", & tree [rt]. sum); return;} int mid = (l + r)> 1; build (lson); build (rson); PushUp (rt);} void update (int p, int add, int l, int r, int rt) {if (l = r) {tree [rt]. sum + = add; return;} int mid = (l + r)> 1; if (p <= mid) update (p, add, lson ); else update (p, add, rson); PushUp (rt);} int query (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {return tree [rt]. sum;} int mid = (l + r)> 1; int ret = 0; if (L <= mid) ret + = query (L, R, lson ); if (R> mid) ret + = query (L, R, rson); return ret;} int main () {int T, n; scanf ("% d ", & T); for (int cas = 1; cas <= T; ++ cas) {printf ("Case % d: \ n", cas ); scanf ("% d", & n); build (1, n, 1); char op [10]; while (scanf ("% s", op )) {if (op [0] = 'E') break; int a, B; scanf ("% d", & a, & B ); if (op [0] = 'q') printf ("% d \ n", query (a, B, 1, n, 1 )); else if (op [0] ='s ') update (a,-B, 1, n, 1); else update (a, B, 1, n, 1) ;}} 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.