Hdu-1166-enemy deployment

Source: Internet
Author: User

The first line is an integer T, indicating that there are T groups of data. The first line of each group of data is a positive integer N (N <= 50000), indicating that the enemy has N barracks, followed by N positive integers, the I positive integer ai represents an ai individual (1 <= ai <= 50) at the beginning of the I barracks ). Next, each line has a command in four forms: (1) Add I j, I and j are positive integers, indicating that j individuals are added to camp I (j cannot exceed 30) (2) Sub I j, I and j are positive integers, indicating that j individuals are reduced in camp I (j cannot exceed 30); (3) Query I j, I and j are positive integers. I <= j indicates the total number of users from camp I to Camp j. (4) End indicates the End. This command appears at the End of each group of data; each group of data can have up to 40000 commands --> direct BIT, just like a template question. [Cpp] # include <iostream> # include <cstdio> # include <cstring> using namespace std; const int maxn = 50000 + 10; int a [maxn], lowerbit [maxn], C [maxn], N; int sum (int x) // The first x items of the BIT and {int ret = 0; while (x> 0) {ret + = C [x]; x-= lowerbit [x];} return ret;} void add (int x, int d) // BIT modification -- add {while (x <= N) {C [x] + = d; x + = lowerbit [x] ;}} void sub (int x, int d) // BIT modification -- minus {while (x <= N) {C [x]-= D; x + = lowerbit [x] ;}} int main () {int T, cnt = 1, I, j; for (I = 0; I <maxn; I ++) lowerbit [I] = I & (-I); scanf ("% d", & T); while (T --) {memset (C, 0, sizeof (C); scanf ("\ n % d \ n", & N); for (I = 1; I <= N; I ++) {scanf ("% d", & a [I]); add (I, a [I]);} char s [7]; printf ("Case % d: \ n ", cnt ++); while (~ Scanf ("% s", s) {if (strcmp (s, "End") = 0) break; scanf ("% d", & I, & j); if (strcmp (s, "Query") = 0) printf ("% d \ n", sum (j)-sum (I-1 )); else if (strcmp (s, "Add") = 0) add (I, j); else sub (I, j) ;}} 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.