Simple Line Segment Tree for Enemy Arrangement

Keywords: Windows iOS

Simple Line Segment Tree for Enemy Arrangement

https://vjudge.net/contest/295911#problem/A

Time limit: 1000 ms
Memory limit: 32768 kB
OS: Windows

describe

Country C's deadly rival, Country A, was conducting military exercises during this period, so Derek, the spy chief of Country C, and Tidy, his men, were busy again. The task of Derek and Tidy is to monitor the activities of N engineering camps along the coastline of State A. As a result of some advanced monitoring methods, the number of each engineer camp is well known in Country C. The number of each engineer camp may change, and it may increase or decrease a number of manpower, but these can not escape the surveillance of Country C.
The CIA wants to study what tactics the enemy is exercising, so Tidy needs to report to Derek at any time how many people there are in a successive engineering camp, such as Derek: "Tidy, report immediately how many people there are from the third camp to the tenth camp!" Tidy needs to start calculating the total number of people in this section and reporting immediately. But the number of enemy barracks often changes, and Derek asks for different sections, so Tidy has to count one camp at a time, and soon exhausted, Derek is increasingly dissatisfied with Tidy's calculation speed: "You are a fat boy, so slow, I fired you squid!" Tidy thought: "Calculate for yourself, this is really a tiring job! I hate it! You fired me! "In desperation, Tidy had to call Windbreaker, a computer expert, for help. Windbreaker said,"Dead fat boy, ask you to do more acm problems and read more algorithmic books at ordinary times. Now you've tasted the bitter fruit!"Tidy said,"I know wrong... "But Windbreaker has already hung up. Tidy is distressed, so he's really going to crash, smart reader. Can you write a program to help him do this? But if your program is not efficient enough, Tidy will still be scolded by Derek.

Input

The first line is an integer T, indicating that there are T groups of data.
A positive integer N (N <= 50000) in the first row of each set of data indicates that the enemy has N Engineer barracks, followed by N positive integers, and a positive integer ai in the first row represents an ai individual at the beginning of the first engineer barracks (1 <= ai <= 50).
Next, there is a command on each line in four forms:
(1) Add i j,i and j are positive integers, representing an increase of J individuals in the first camp (j does not exceed 30)
(2) Sub ij, i and j are positive integers, indicating that the number of J individuals in the first camp is reduced (j does not exceed 30);
(3) Query ij, i and j are positive integers, i <=j, indicating the total number of camps from i to J.
(4)End means the end, and this command appears at the end of each set of data.
Up to 40,000 commands per set of data

Output

For group I data, first output "Case i:" and return.
For each Query query, output an integer and return to indicate the total number of people in the query section, which remains within int.

Sample Input

1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End

Sample Output

Case 1:
6
33
59

This is a simple question, but I encountered a strange BUG: strings used to read instructions must be scanf read, using cin will WA...
I wonder if it's a compiler problem?

Code

#include <bits/stdc++.h>
#define maxn 200005
#define ll long long
#define _for(i, a) for(ll i = 0; i < (a); i++)
#define _rep(i, a, b) for(ll i = (a); i <= (b); i++)
using namespace std;
ll arr[maxn], t[maxn * 4];
void build(ll node, ll beg, ll end) {
	if (beg == end) {
		t[node] = arr[beg];
		return;
	}
	ll mid = (beg + end) / 2;
	build(node * 2, beg, mid);
	build(node * 2 + 1, mid + 1, end);
	t[node] = t[node * 2] + t[node * 2 + 1];
}
void add(ll node, ll beg, ll end, ll l, ll r, ll val) {
	t[node] += val;
	if (beg == end) {
		return;
	}
	ll mid = (beg + end) / 2;
	if (mid >= r) add(node * 2, beg, mid, l, r, val);
	else add(node * 2 + 1, mid + 1, end, l, r, val);
}
ll query(ll node, ll beg, ll end, ll l, ll r) {
	if (beg == l && end == r) return t[node];
	ll mid = (beg + end) / 2, ans = 0;
	if (mid >= r) return query(node * 2, beg, mid, l, r);
	else if (mid < l) return query(node * 2 + 1, mid + 1, end, l, r);
	else return query(node * 2, beg, mid, l, mid) + query(node * 2 + 1, mid + 1, end, mid + 1, r);
}
int main() {
	ios::sync_with_stdio(false);
	ll T;
	scanf("%lld", &T);
	_rep(q, 1, T) {
		printf("Case %lld:\n", q);
		ll n;
		scanf("%lld", &n);
		memset(t, 0, sizeof(t));
		memset(arr, 0, sizeof(arr));
		_rep(i, 1, n) scanf("%lld", &arr[i]);
		build(1, 1, n);
		char s[20];
		while (scanf("%s", s), s[0] != 'E') {//Scnf is used here
			ll x, y;
			scanf("%lld%lld", &x, &y);
			switch (s[0]) {
			case 'Q':
				printf("%lld\n", query(1, 1, n, x, y)); break;
			case 'A':
				add(1, 1, n, x, x, y); break;
			case 'S':
				add(1, 1, n, x, x, -y); break;
			}
		}
	}
	return 0;
}

Posted by adige on Fri, 17 May 2019 00:41:20 -0700