Question G: Interval Query

Keywords: Windows

Title Description

There are N dining windows in the canteen. It's time for lunch now. There are many students in each window, and the number of people queuing in each window is constantly changing.
Now, how many people are queuing from window i to window j?

input

The first line of input is an integer T, indicating that there are T groups of test data.
The first line of each input group is a positive integer N (N <= 30000), indicating that the canteen has N windows.
The next line enters N positive integers, and the first positive integer AI indicates that there is an AI personal queue at the beginning of the first window. (1<=ai<=50)
Next, there is a command on each line. There are four forms of command:
(1)Add i j, i and j are positive integers, indicating that the number of J individuals is increased in the first window (j does not exceed 30);
(2)Sub i j, i and j are positive integers, indicating that the number of J individuals in the first window is reduced (j is no more than 30);
(3)Query i j, i and j are positive integers, i <=j, indicating the total number of windows i to J asked;
(4)End means the end, and this command appears at the end of each group of data.
Each set of data has a maximum of 40,000 commands.

output

For each set of inputs, the sample number is first output, which takes up one line.
Then for each Query query, output an integer, one line, representing 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
#include<bits/stdc++.h>
#define read() freopen("input.txt","r",stdin);
#define write() freopen("output.txt","w",stdout);
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll sum[maxn*4],num[maxn];
void build(int l,int r,int root){
	if(l==r){
		sum[root]=num[l];
		return;
	}
	int m=(r+l)/2;
	build(l,m,root*2);
	build(m+1,r,root*2+1);
	sum[root]=sum[root*2]+sum[root*2+1];
}
void update(int p,int v,int l,int r,int root){
	if(l==r){
		sum[root]+=v;return;
	}
	else{
		int m=(l+r)/2;
		if(p<=m) update(p,v,l,m,root*2);
		else update(p,v,m+1,r,root*2+1);
		sum[root]=sum[root*2]+sum[root*2+1];	
	}
}
ll query(int ql,int qr,int l,int r,int root){
	if(ql<=l&&r<=qr){
		return sum[root];
	}
	int m=(r+l)/2;
	ll ans=0;
	if(ql<=m) ans+=query(ql,qr,l,m,root*2);
	if(m<qr) ans+=query(ql,qr,m+1,r,root*2+1);
	return ans;
}
int main()
{
	read();write();
	int t,kase=0;scanf("%d",&t);
	while(t--){
		printf("Case %d:\n",++kase);
		int n;scanf("%d",&n);
		for( int i=1; i<=n; i++ ) scanf("%lld",&num[i]);
		build(1,n,1);
		string op;ll a,b,vau;
		while(cin>>op&&op!="End"){
			scanf("%lld %lld",&a,&b);
			if(op=="Query") printf("%lld\n",query(a,b,1,n,1));
			else if(op=="Add") update(a,b,1,n,1);
			else if(op=="Sub") update(a,-b,1,n,1);
		}
	}
    return 0;
}

Posted by nuttynibbles on Mon, 07 Oct 2019 18:59:39 -0700