线段树基础

对于一个序列,要求它在[r,l]区间上的和,我们首先会想到的就是前缀和(当然,这是针对值是固定不变的情况)
若序列是动态的,我们就有必要应用线段树了。
线段树的几个基本应用:
1、单点修改区间求和
2、单点修改区间最值
3、求逆序对个数
4、区间求最大值的位子
5、成段替换 (懒标记)
6、成段增减区间求和

1、单点修改区间求和
Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在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<iostream>
#include<cstring> 
#include<vector>
#define MAX 50000 
using namespace std;
struct node{
	int left,right,sum,f;
}tree[4*MAX+1];
int a[MAX];

void build(int l,int r,int k);
void query(int l,int r,int k,int &ans);
void add(int x,int y,int k);
void sub(int x,int y,int k);
int main()
{
	ios::sync_with_stdio(false);
	int t;
	while(cin>>t)
	{
		for(int i=1;i<=t;i++)
		{
			vector<int>res;
			int n;
			cin>>n;
			for(int k=0;k<n;k++)
			{
				cin>>a[k];
			}
			build(1,n,1);
			char q[15];
			cin>>q;
			while(strcmp(q,"End"))
			{
				if(!strncmp(q,"Query",5))
				{
					int ans=0;
					int x,y;
					cin>>x>>y;
					query(x,y,1,ans);
					res.push_back(ans);
				}	
				else if(!strncmp(q,"Add",3))
				{
					int x,y;
					cin>>x>>y;
					add(x,y,1);
				}	
				else if(!strncmp(q,"Sub",3))
				{
					int x,y;
					cin>>x>>y;
					sub(x,y,1);
				}	
				cin>>q;

			}
			cout<<"Case "<<i<<":"<<endl;
			for(int k=0;k<res.size();k++)
				cout<<res[k]<<endl;
			
			
			 
		}
	 } 
 } 
 
 void build(int l,int r,int k)
 {
 	tree[k].left=l;
 	tree[k].right=r;
 	if(l==r)
 	{
 		tree[k].sum=a[l-1];
 		return ;
	 }
	 else
	 {
	 	build(l,(l+r)/2,2*k);
	 	build((l+r)/2+1,r,2*k+1);
	 	tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;
	 }
 }
 
 void query(int l,int r,int k,int &ans)
 {
 	if(l<=tree[k].left&&r>=tree[k].right)
 	{
 		ans+=tree[k].sum;
 		return ;
	 }
	 int m=(tree[k].left+tree[k].right)/2;
 	if(l<=m)
 	{
 		query(l,r,2*k,ans);
	 }
	if(r>m)
	{
		query(l,r,2*k+1,ans);
	}
 }
 
 void add(int x,int y,int k)
 {
 	if(x==tree[k].left&&x==tree[k].right)
 	{
 		tree[k].sum+=y;
 		return;
	 }
	 int m=(tree[k].left+tree[k].right)/2;
	 if(x<=m)
	 	add(x,y,2*k);
	 else
	 	add(x,y,2*k+1);
	tree[k].sum+=y;
 }
 
  void sub(int x,int y,int k)
 {
 	if(x==tree[k].left&&x==tree[k].right)
 	{
 		tree[k].sum-=y;
 		return;
	 }
	 int m=(tree[k].left+tree[k].right)/2;
	 if(x<=m)
	 	sub(x,y,2*k);
	 else
	 	sub(x,y,2*k+1);
	tree[k].sum-=y;
 	
 }

2、单点修改区间最值
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取’Q’或’U’) ,和两个正整数A,B。
当C为’Q’的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为’U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。

Output
对于每一次询问操作,在一行里面输出最高成绩。

Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output
5
6
5
9

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
	int left,right,ma;
}tree[800010];
int a[200005];

void build(int l,int r,int k);
void query(int l,int r,int k,int &ans);
void updata(int x,int y,int k);
void output(int n);

int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
			for(int i=0;i<n;i++)
			{
				scanf("%d",&a[i]);
			}
			build(1,n,1);
			//output(n);
			char q;
			for(int i=1;i<=m;i++)
			{
				char a=getchar();
				int x,y;
				scanf("%c%d%d",&q,&x,&y);
				if(q=='Q')
				{
					int ans=0;
					query(x,y,1,ans);
					printf("%d\n",ans);
				}
				else if(q=='U')
				{
					updata(x,y,1);
					//output(n);
				}
			}
	 } 
 } 
 
 void build(int l,int r,int k)
 {
 	tree[k].left=l;
 	tree[k].right=r;
 	if(l==r)
 	{
 		tree[k].ma=a[l-1];
 		return ;
	 }
	 else
	 {
	 	build(l,(l+r)/2,2*k);
	 	build((l+r)/2+1,r,2*k+1);
	 	tree[k].ma=max(tree[2*k].ma,tree[2*k+1].ma);
	 }
 }
 
 void query(int l,int r,int k,int &ans)
 {
 	if(l<=tree[k].left&&r>=tree[k].right)
 	{
 		ans=max(tree[k].ma,ans);
 		return ;
	 }
	 int m=(tree[k].left+tree[k].right)/2;
 	if(l<=m)
 	{
 		query(l,r,2*k,ans);
	 }
	if(r>m)
	{
		query(l,r,2*k+1,ans);
	}
 }
 
 void updata(int x,int y,int k)
 {
 	if(x==tree[k].left&&x==tree[k].right)
 	{
 		tree[k].ma=y;
 		return;
	 }
	 int m=(tree[k].left+tree[k].right)/2;
	 if(x<=m)
	 	updata(x,y,2*k);
	 else
	 	updata(x,y,2*k+1);
	tree[k].ma=max(tree[2*k].ma,tree[2*k+1].ma);
 }
 
 void output(int n)
 {
 	for(int i=1;i<=4*n;i++)
 		printf("%d %d %d\n",tree[i].left,tree[i].right,tree[i].ma);
 }

3、求逆序对个数
Problem Description
The inversion number of a given number sequence a1, a2, …, an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

For a given sequence of numbers a1, a2, …, an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

a1, a2, …, an-1, an (where m = 0 - the initial seqence)
a2, a3, …, an, a1 (where m = 1)
a3, a4, …, an, a1, a2 (where m = 2)

an, a1, a2, …, an-1 (where m = n-1)

You are asked to write a program to find the minimum inversion number out of the above sequences.

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.

Output
For each case, output the minimum inversion number on a single line.

Sample Input
10
1 3 6 9 0 8 5 7 4 2

Sample Output
16

#include<iostream>
#include<algorithm>
#define MAX 5000
using namespace std;
struct node{
	int left,right,sum;
}tree[4*MAX+1];
int a[MAX];
void build(int l,int r,int k);
void query(int l,int r,int k,int &ans);
void updata(int x,int k);

int main()
{
	ios::sync_with_stdio(false);
	int n;
	while(cin>>n)
	{
		build(1,n,1);
		int ans=0;
		for(int i=0;i<n;i++)	//从头输入a[i],设a[i]+1为区间,tree[a[i]+1].sum=1,那么就将a[i+1]前面的数组对应的节点都更新了	
		{						//计算a[i+1]+1至n这段距离内,有多少符合条件的节点 
			cin>>a[i];
			query(a[i]+1,n,1,ans);	
			updata(a[i]+1,1);	
		 } 
		int min_=ans;
		for(int i=0;i<n-1;i++)
		{
			ans=ans-a[i]+(n-a[i]-1);
			min_=min(ans,min_);
		}
		cout<<min_<<endl;	
	}
}

void build(int l,int r,int k)
{
	tree[k].left=l;
	tree[k].right=r;
	tree[k].sum=0;
	int m=(l+r)/2;
	if(l==r)
	{
		return;
	}
	else
	{
		build(l,m,2*k);
		build(m+1,r,2*k+1);
	}
}

void query(int l,int r,int k,int &ans)
{
	if(l<=tree[k].left&&r>=tree[k].right)
	{
			ans+=tree[k].sum;
			return;
	}
	int m=(tree[k].left+tree[k].right)/2;
	if(l<=m)
		query(l,r,2*k,ans);
	if(r>m)
		query(l,r,2*k+1,ans);
	
}

void updata(int x,int k)
{
	if(x==tree[k].left&&x==tree[k].right)
	{
		tree[k].sum=1;
		return;
	}
	int m=(tree[k].left+tree[k].right)/2;
	if(x<=m)
		updata(x,2*k);
	else
		updata(x,2*k+1);
	tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;
}

4、区间求最大值的位子
Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

If there is no valid location for a new announcement, it is not put on the billboard (that’s why some programming contests have no participants from this university).

Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.

Input
There are multiple cases (no more than 40 cases).

The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.

Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can’t be put on the billboard, output “-1” for this announcement.

Sample Input
3 5 5
2
4
3
3
3

Sample Output
1
2
1
3
-1

#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 200000
using namespace std;
int tree[4*MAX+1];

void build(int l,int r,int k,int w);
void query(int l,int r,int k,int x,int &ans);
void output(int h)
{
	cout<<endl;
	for(int i=1;i<=4*h+1;i++)
		cout<<tree[i]<<endl;
		cout<<endl;
}
int main()
{
	int h,w,n,x,ans;
	while(scanf("%d%d%d",&h,&w,&n)!=EOF)
	{
		if(h>n)
			h=n;
		build(1,h,1,w);
		//output(h);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			ans=0;
			query(1,h,1,x,ans);
			//output(h);
			printf("%d\n",ans); 
		}
	}
 } 
 
 void build(int l,int r,int k,int w) 
 {
 	int m=(l+r)/2;
 	tree[k]=w;
 	if(l==r)
 	{
 		return;
 	}
 	else
 	{
 		build(l,m,2*k,w);
 		build(m+1,r,2*k+1,w);
	 }
 }
 
void query(int l,int r,int k,int x,int &ans)
 {
 	int m=(l+r)/2;
 	if(l==r)
 	{
 		if(tree[k]-x<0)
 			ans=-1;
 		else
 		{
 			ans=l;
 			tree[k]-=x;
		 }
 		return;
	}
	if(tree[2*k]>=x)
		query(l,m,2*k,x,ans);
	else if(tree[2*k+1]>=x)
		query(m+1,r,2*k+1,x,ans);
	else if(tree[2*k]<x&&tree[2*k+1]<x)
	{
		ans=-1;
		return;
	}
	tree[k]=max(tree[2*k],tree[2*k+1]);
 }

5、成段替换 (懒标记)
Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input
1
10
2
1 5 2
5 9 3

Sample Output
Case 1: The total value of the hook is 24.

#include<iostream>
#include<cstdio>
#define MAX 100000
#define lson l,m,2*k
#define rson m+1,r,2*k+1
using namespace std;
struct node{
	int left,right,sum,lazy;
}tree[4*MAX+1];
void build(int l,int r,int k);
void pushDown(int k);
void updata(int l,int r,int k,int x);
//void query(int l,int r,int k,int &ans);
int main()
{
	int t;
	scanf("%d",&t);
	for(int k=1;k<=t;k++)
	{
		int n,q;
		scanf("%d%d",&n,&q);
		build(1,n,1);
		int x,y,z,ans=0;
		for(int i=1;i<=q;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			updata(x,y,1,z);
		}
		//query(1,n,1,ans);
		printf("Case %d: The total value of the hook is %d.\n",k,tree[1].sum);
		
	}
 } 

void build(int l,int r,int k)
{
	tree[k].left=l;
	tree[k].right=r;
	tree[k].lazy=0;
	int m=(l+r)/2;
	if(l==r)
	{
		tree[k].sum=1;
		return;
	}
	else{
		build(lson);
		build(rson);
		tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
	}
}

void pushDown(int k)
{
	if(tree[k].lazy)
	{
		tree[k*2].lazy=tree[k].lazy;
		tree[k*2].sum=(tree[k*2].right-tree[k*2].left+1)*tree[k].lazy;
		tree[k*2+1].lazy=tree[k].lazy;
		tree[k*2+1].sum=(tree[k*2+1].right-tree[k*2+1].left+1)*tree[k].lazy;
		tree[k].lazy=0;
		return;
	}
}

void updata(int l,int r,int k,int x)
{
	int L=tree[k].left,R=tree[k].right;
	if(l<=L&&r>=R)
	{
		tree[k].lazy=x;
		tree[k].sum=(R-L+1)*x;
		return;
	}
	pushDown(k);
	int m=(L+R)/2;
	if(l<=m)
		updata(l,r,k<<1,x);
	if(r>m)
		updata(l,r,k<<1|1,x);
	tree[k].sum=tree[k*2].sum+tree[k*2+1].sum; 
}

/*void query(int l,int r,int k,int &ans)
{
	int L=tree[k].left,R=tree[k].right;
	if(l<=L&&r>=R)
	{
		ans+=tree[k].sum;
		return;
	}
	pushDown(k);
	int m=(L+R)/2;
	if(l<=m)
		query(l,r,k*2,ans);
	if(r>m)
		query(l,r,k*2+1,ans);
}*/

6、成段增减区间求和
Description

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output

4
55
9
15

#include<iostream>
#include<cstdio>
#define MAX 100000
#define LL long long int
using namespace std;
struct node{
	LL left,right,lazy;
	LL sum;
}tree[4*MAX+1];
LL a[MAX];

void build(LL l,LL r,int k);
void pushDown(int k);
void query(LL l,LL r,int k,LL &ans);
void updata(LL l,LL r,int k,LL x);

int main()
{
	int n,q;
	scanf("%d%d",&n,&q);
		for(int i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		build(1,n,1);
	//	output(n);
		char ch;
		LL x,y,z;
		LL ans;
		for(int i=1;i<=q;i++)
		{
			char c=getchar();
			scanf("%c",&ch);
			//printf("%d\n",i);
			if(ch=='Q')
			{
				ans=0;
				scanf("%lld%lld",&x,&y);
				query(x,y,1,ans);
			//	output(n);
				printf("%lld\n",ans);
			}
			if(ch=='C')
			{
				scanf("%lld%lld%lld",&x,&y,&z);
				updata(x,y,1,z);
		//		output(n);
			}
			
		}
}

void build(LL l,LL r,int k)
{
	tree[k].left=l;
	tree[k].right=r;
	tree[k].lazy=0;
	int m=(l+r)/2;
	if(l==r)
	{
		tree[k].sum=a[l-1];
	}
	else
	{
		build(l,m,2*k);
		build(m+1,r,2*k+1);
		tree[k].sum=tree[2*k].sum+tree[2*k+1].sum; 
	}
}

void query(LL l,LL r,int k,LL &ans)
{
	if(l<=tree[k].left&&r>=tree[k].right)
	{
		ans+=tree[k].sum;
		return;
	}
	pushDown(k);
	int m=(tree[k].left+tree[k].right)/2;
	if(l<=m)
		query(l,r,2*k,ans);
	if(r>m)
		query(l,r,2*k+1,ans);
}

void pushDown(int k)
{
	if(tree[k].lazy!=0)
	{
		tree[2*k].lazy+=tree[k].lazy;
		tree[2*k].sum+=(tree[2*k].right-tree[2*k].left+1)*tree[k].lazy;
		tree[2*k+1].lazy+=tree[k].lazy;
		tree[2*k+1].sum+=(tree[2*k+1].right-tree[2*k+1].left+1)*tree[k].lazy;
		tree[k].lazy=0;
	}
	return;
}

void updata(LL l,LL r,int k,LL x)
{
	if(l<=tree[k].left&&r>=tree[k].right)
	{
		tree[k].lazy+=x;
		tree[k].sum+=(tree[k].right-tree[k].left+1)*x;
		return;
	}
	pushDown(k);
	int m=(tree[k].left+tree[k].right)/2;
	if(l<=m)
		updata(l,r,2*k,x);
	if(r>m)
		updata(l,r,2*k+1,x);
	tree[k].sum=tree[2*k].sum+tree[2*k+1].sum;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值