hdu 1166 enemy formation (tree-shaped array single point update + interval query)

Portal

Problem Description

Country C’s enemy, Country A, is conducting military exercises during this period, so country C spy leader Derek and his subordinate Tidy are busy again. Country A has arranged N engineer camps along the coastline. Derek and Tidy's task is to monitor the activities of these engineer camps. Due to the adoption of some advanced monitoring methods, the number of people in each engineering camp is clear to country C. The number of people in each engineering camp may change, and it may increase or decrease a number of manpower, but these cannot escape C. State surveillance.
The CIA wants to study the enemy's tactics for the exercise, so Tidy must report to Derek at any time how many people are in a certain continuous engineering camp. For example, Derek asks: "Tidy, immediately report how many people there are from the third camp to the tenth camp. !" Tidy will start counting and reporting the total number of people in this segment right away. However, the number of enemy camps changed frequently, and Derek asked different segments each time, so Tidy had to count one by one camp each time, and soon became exhausted. Derek's calculation of Tidy became faster. More and more dissatisfied: "You fat guy, it's so slow, I'll fire you!" Tidy thought, "You can count it yourself. This is really tiring work! I can't wait for you to fire me! "In desperation, Tidy had to call Windbreaker, a computer expert, for help. Windbreaker said, "Deadly fat boy, I asked you to do more acm questions and read more algorithm books. Now you can taste the bitter fruit!" Tidy said: " I knew it was wrong..." But Windbreaker has hung up. Tidy is very distressed, he will really fall apart, clever reader, can you write a program to help him complete this work? But if your program is not efficient enough, Tidy will still be scolded by Derek.

Input

An integer T in the first line indicates 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 engineer camps, and then there are N positive integers. The i-th positive integer ai represents the i-th engineer camp with ai at the beginning Individual (1<=ai<=50).
There is a command on each line in the next, and the command has 4 forms:
(1) Add ij, i and j are positive integers, which means that j individuals will be added to the i-th camp (j does not exceed 30)
(2) Sub ij ,i and j It is a positive integer, which means that the i-th camp reduces j individuals (j does not exceed 30);
(3) Query ij, i and j are positive integers, i<=j, which means that the total number of people who inquired from the i-th camp to the j-th camp;
(4) End means end, this command appears at the end of each group of data;
each group of data has a maximum of 40,000 commands

Output

For the i-th group of data, first output "Case i:" and press Enter.
For each Query query, output an integer and press Enter to indicate the total number of people in the query segment, and this number is kept 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

Code:

#include<bits/stdc++.h>
using namespace std;
int n,tree[50005];
int lowbit(int x)
{
    
    
	return x&(-x);
}
void updata(int i,int k)
{
    
    
	while(i<=n)
	{
    
    
		tree[i]+=k;
		i+=lowbit(i);
	}
}
int query(int i)
{
    
    
	int res=0;
	while(i>0)
	{
    
    
		res+=tree[i];
		i-=lowbit(i);
	}
	return res;
}
int main()
{
    
    
	int t;
	cin>>t;
	for(int f=1;f<=t;f++){
    
    
		memset(tree,0,sizeof(tree));
		cout<<"Case "<<f<<":"<<endl;
		cin>>n;
		for(int i=1;i<=n;i++){
    
    
			int k;
			cin>>k;
			//将初始的人数在数组中进行修改 
			updata(i,k);
		}
		string s;
		while(cin>>s)
		{
    
    
			if(s=="End") break;
			int x,y;
			cin>>x>>y;
			if(s=="Query"){
    
    
				//求的是x到y这个区间的总人数,所以对应的区间应该是(x-1)到y,这样才不会漏掉队伍x 
				cout<<query(y)-query(x-1)<<endl;
			}
			else if(s=="Add"){
    
    
				updata(x,y);
			}
			else if(s=="Sub"){
    
    
				//减少y相当于增加-y 
				updata(x,-y);
			}
		}
	}
	return 0;
}
        }

to sum up:

The tree array is an important method to solve the interval problem. The data is saved and modified in the form of a tree

It should be noted that each modification of the tree array requires modification of a large interval, rather than a single value modification.

Core functions:

int lowbit(int x)//用于更改下标
{
    
    
	return x&(-x);
}
void updata(int i,int k)//用于更新各个下标的值
{
    
    
	while(i<=n)
	{
    
    
		tree[i]+=k;
		i+=lowbit(i);
	}
}
int query(int i)//用于求区间和
{
    
    
	int res=0;
	while(i>0)
	{
    
    
		res+=tree[i];
		i-=lowbit(i);
	}
	return res;
}

Guess you like

Origin blog.csdn.net/Lzhzl211/article/details/114792870