线段树

线段树基本操作

常用宏定义

#define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 

建树

void build(int l, int r, int rt)//l,r表示当前节点区间,rt表示当前节点编号 { tree[rt].l = l; tree[rt].r = r; if(l == r){//说明到达叶子节点 scanf("%d", &tree[rt].w); return; } int m = (l+r)/2;//二分 build(ls);//建立左子树 build(rs);//建立右子树 tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; //状态合并,此结点的w=两个孩子的w之和  } 

lazy标记下传
以给区间都加上数x为例

void down(int rt)//标记下传  { tree[rt<<1].mark += tree[rt].mark; tree[rt<<1|1].mark += tree[rt].mark; tree[rt<<1].w += tree[rt].mark*(tree[rt<<1].r-tree[rt<<1].l+1); tree[rt<<1|1].w += tree[rt].mark*(tree[rt<<1|1].r-tree[rt<<1|1].l+1); tree[rt].mark = 0; } 

单点查询

void ask_poi(int rt)//rt表示当前节点编号 单点查询 { if(tree[rt].l == tree[rt].r)//当该结点的左右端点相等,是叶子节点,是最终答案 { ans = tree[rt].w; return; } if(tree[rt].mark) down(rt);//懒标志下传 int m = (tree[rt].l + tree[rt].r)/2; if(x <= m) ask_poi(rt<<1);//目标位置比中点靠左,就递归左孩子 else ask_poi(rt<<1|1);//反之,递归右孩子 } 

区间查询

void ask_interval(int rt)//区间查询 { if(tree[rt].l >= a && tree[rt].r <=b) { ans += tree[rt].w; return; } if(tree[rt].mark) down(rt); int m = (tree[rt].l + tree[rt].r)/2; if(a <= m) ask_interval(rt<<1); if(b > m) ask_interval(rt<<1|1); } 

单点修改

void change_poi(int rt)//rt表示当前节点编号 单点修改 { if(tree[rt].l == tree[rt].r)//找到目标位置 { tree[rt].w += s; return; } if(tree[rt].mark) down(rt); int m = (tree[rt].l + tree[rt].r)/2; if(x<=m) change_poi(rt<<1); else change_poi(rt<<1|1); tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; } 

区间修改

void change_interval(int rt)//区间修改 { if(tree[rt].l >= a && tree[rt].r <=b) { tree[rt].w += (tree[rt].r - tree[rt].l + 1) * s; tree[rt].mark += s; return; } if(tree[rt].mark) down(rt); int m = (tree[rt].l + tree[rt].r)/2; if(a <= m) change_interval(rt<<1); if(b > m) change_interval(rt<<1|1); tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; } 

模板题:
敌兵布阵:
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>  #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 using namespace std; const int N=50010; int x,a,b,ans,s; struct node { int l, r, w, mark;//记录左孩子,右孩子,区间和,还有延时标记 }tree[N*4]; void build(int l, int r, int rt)//l,r表示当前节点区间,rt表示当前节点编号 { tree[rt].l = l; tree[rt].r = r; if(l == r){//说明到达叶子节点 scanf("%d", &tree[rt].w); return; } int m = (l+r)/2;//二分 build(ls);//建立左子树 build(rs);//建立右子树 tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; //状态合并,此结点的w=两个孩子的w之和  } void down_add(int rt)//标记下传  { tree[rt<<1].mark += tree[rt].mark; tree[rt<<1|1].mark += tree[rt].mark; tree[rt<<1].w += tree[rt].mark*(tree[rt<<1].r-tree[rt<<1].l+1); tree[rt<<1|1].w += tree[rt].mark*(tree[rt<<1|1].r-tree[rt<<1|1].l+1); tree[rt].mark = 0; } void down_sub(int rt)//标记下传  { tree[rt<<1].mark -= tree[rt].mark; tree[rt<<1|1].mark -= tree[rt].mark; tree[rt<<1].w -= tree[rt].mark*(tree[rt<<1].r-tree[rt<<1].l+1); tree[rt<<1|1].w -= tree[rt].mark*(tree[rt<<1|1].r-tree[rt<<1|1].l+1); tree[rt].mark = 0; } void poi_add(int rt)//rt表示当前节点编号 单点修改 { if(tree[rt].l == tree[rt].r)//找到目标位置 { tree[rt].w += s; return; } if(tree[rt].mark) down_add(rt); int m = (tree[rt].l + tree[rt].r)/2; if(x<=m) poi_add(rt<<1); else poi_add(rt<<1|1); tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; } void poi_sub(int rt)//rt表示当前节点编号 单点修改 { if(tree[rt].l == tree[rt].r)//找到目标位置 { tree[rt].w -= s; return; } if(tree[rt].mark) down_sub(rt); int m = (tree[rt].l + tree[rt].r)/2; if(x<=m) poi_sub(rt<<1); else poi_sub(rt<<1|1); tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; } void ask_interval(int rt)//区间查询 { if(tree[rt].l >= a && tree[rt].r <=b) { ans += tree[rt].w; return; } // if(tree[rt].mark) down(rt); int m = (tree[rt].l + tree[rt].r)/2; if(a <= m) ask_interval(rt<<1); if(b > m) ask_interval(rt<<1|1); } int main(){ int T; scanf("%d",&T); while(T--){ int n; char ord[10]; scanf("%d",&n); build(1,n,1); scanf("%s",ord); while(ord[0]!='E'){ if(ord[0]=='Q'){ scanf("%d%d",&a,&b); ans=0; ask_interval(1); printf("%d\n",ans); } else if(ord[0]=='A'){ scanf("%d%d",&x,&s); poi_add(1); } else{ scanf("%d%d",&x,&s); poi_sub(1); } scanf("%s",ord); } } return 0; } 

I Hate It:
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

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

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

Hint
Huge input,the C function scanf() will work better than cin

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxx=200005; int s[maxx],mx[maxx*4]; void pushup(int rt) { mx[rt]=max(mx[rt*2],mx[rt*2+1]); } void build(int l,int r,int rt) { if(l==r) { mx[rt]=s[l]; return ; } int mid=(l+r)/2; build(l,mid,rt*2); build(mid+1,r,rt*2+1); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l && R>=r) return mx[rt]; int mid=(l+r)/2; int ret=0; if(L<=mid) ret=max(ret,query(L,R,l,mid,rt*2)); if(R>mid) ret=max(ret,query(L,R,mid+1,r,rt*2+1)); return ret; } void update(int L,int s,int l,int r,int rt) { if(l==r) { mx[rt]=s; return ; } int mid=(l+r)/2; if(L<=mid) update(L,s,l,mid,rt*2); else update(L,s,mid+1,r,rt*2+1); pushup(rt); } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1; i<=n; i++) scanf("%d",&s[i]); build(1,n,1); int a,b; char ch; while(m--) { scanf(" %c%d%d",&ch,&a,&b); if(ch=='Q') printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; } 

just a hook:
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<bits/stdc++.h> using namespace std; #define lson l,mid,id*2 #define rson mid+1,r,id*2+1 const int maxn=100000+100; int sum[maxn*4]; int lazy[maxn*4]; void pushup(int id){ sum[id]=sum[id*2]+sum[id*2+1]; lazy[id]=0; } void pushdown(int id,int m){ if(lazy[id]){ lazy[id*2]=lazy[id*2+1]=lazy[id]; sum[id*2]=(m-m/2)*lazy[id]; sum[id*2+1]=m/2*lazy[id]; lazy[id]=0; return; } } void build(int l,int r,int id){ if(l==r){ sum[id]=1; lazy[id]=0; return ; } int mid=(l+r)/2; build(lson); build(rson); pushup(id); } void update(int l,int r,int id,int c,int L,int R){ if(L<=l && R>=r){ sum[id]=c*(r-l+1); lazy[id]=c; return ; } pushdown(id,r-l+1); int mid=(l+r)/2; if(L<=mid){ update(lson,c,L,R); } if(R>mid){ update(rson,c,L,R); } pushup(id); } int main() { int t; int n,m,a,b,c,lala; scanf("%d",&t); lala=0; while(t--){ lala++; scanf("%d",&n); build(1,n,1); scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&a,&b,&c); update(1,n,1,c,a,b); } printf("Case %d: The total value of the hook is %d.\n",lala,sum[1]); } return 0; } 

A Simple Problem with Integers:
Language:
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 171720 Accepted: 52877
Case Time Limit: 2000MS
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
Hint

The sums may exceed the range of 32-bit integers.

#include <iostream>  #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 using namespace std; const int N=1000010; long long a,b,ans,s; struct node { long long l, r, w, mark;//记录左孩子,右孩子,区间和,还有延时标记 }tree[N<<2]; void build(long long l,long long r,long long rt)//l,r表示当前节点区间,rt表示当前节点编号 { tree[rt].l = l; tree[rt].r = r; if(l == r){//说明到达叶子节点 scanf("%d", &tree[rt].w); return; } long long m = (l+r)/2;//二分 build(ls);//建立左子树 build(rs);//建立右子树 tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; //状态合并,此结点的w=两个孩子的w之和  } void down(long long rt)//标记下传  { if(tree[rt].mark!=0){ tree[rt<<1].mark += tree[rt].mark; tree[rt<<1|1].mark += tree[rt].mark; tree[rt<<1].w += tree[rt].mark*(tree[rt<<1].r-tree[rt<<1].l+1); tree[rt<<1|1].w += tree[rt].mark*(tree[rt<<1|1].r-tree[rt<<1|1].l+1); tree[rt].mark = 0; } } void change_interval(long long rt)//区间修改 { if(tree[rt].l >= a && tree[rt].r <=b) { tree[rt].w += (tree[rt].r - tree[rt].l + 1) * s; tree[rt].mark += s; return; } if(tree[rt].mark) down(rt); long long m = (tree[rt].l + tree[rt].r)/2; if(a <= m) change_interval(rt<<1); if(b > m) change_interval(rt<<1|1); tree[rt].w = tree[rt<<1].w + tree[rt<<1|1].w; } void ask_interval(long long rt)//区间查询 { if(tree[rt].l >= a && tree[rt].r <=b) { ans += tree[rt].w; return; } if(tree[rt].mark) down(rt); long long m = (tree[rt].l + tree[rt].r)/2; if(a <= m) ask_interval(rt<<1); if(b > m) ask_interval(rt<<1|1); } int main(){ long long n,q; char ord[5]; scanf("%lld%lld",&n,&q); build(1,n,1); while(q--){ scanf("%s",ord); if(ord[0]=='Q'){ ans=0; scanf("%lld%lld",&a,&b); ask_interval(1); printf("%lld\n",ans); } else{ scanf("%lld%lld%lld",&a,&b,&s); change_interval(1); } } return 0; } 

2019牛客暑期多校训练营(第四场)C - sequence:

//rt表示当前节点 
#include <iostream>
#define ll long long
#define ls rt<<1,l,mid
#define rs rt<<1|1,mid+1,r 
using namespace std;
const int Max=3e6+10;
const ll inf = 1e18+5; 
ll sum[Max];
ll a[Max];
int l[Max],r[Max];
struct node{
	int L,R;
	ll mx,mi;
}tree[Max<<2]; 
void build(int rt,int l,int r){
	tree[rt].L=l; tree[rt].R=r;
	if(tree[rt].L==tree[rt].R){
		tree[rt].mi==sum[l];
		tree[rt].mx=sum[l];
		return;
	}
	int mid=(l+r)>>1;
	build(ls);
	build(rs);
	tree[rt].mi = min(tree[rt<<1].mi,tree[rt<<1|1].mi);
	tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx);	
}
ll getmin(int rt,int l,int r){
	if(tree[rt].L>=l&&tree[rt].R<=r){
		return tree[rt].mi;
	}
	int mid=(tree[rt].L+tree[rt].R)>>1;
	ll ans = inf;
	if(l<=mid)ans = min(ans,getmin(rt<<1,l,r));
	if(r>mid)ans = min(ans,getmin(rt<<1|1,l,r));
	return ans;
}
ll getmax(int rt,int l,int r){
	if(tree[rt].L>=l&&tree[rt].R<=r){
		return tree[rt].mx;
	}
	int mid=(tree[rt].L+tree[rt].R)>>1;
	ll ans = inf;
	if(l<=mid)ans = max(ans,getmax(ls));
	if(r>mid)ans = max(ans,getmax(rs));
	return ans;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		l[i]=i,r[i]=i;
	}
	for(int i=1;i<=n;i++){
		scanf("%lld",&sum[i]);
		sum[i]=sum[i]+sum[i-1];
	}
	build(1,1,n);
	for(int i=1;i<=n;i++){
	while(a[l[i]-1]>=a[i])l[i] = l[l[i] - 1];
	}
	for(int i=n;i>=1;i--){
	while(a[r[i]+1]>=a[i]) r[i] = r[r[i] + 1];
	}
	ll ans = -1e18+5;
	for(int i=1;i<=n;i++ ){
		if(a[i]>0){
			ll x1 = getmax(1,i,r[i]);
			ll x2 = getmin(1,l[i]-1,i-1);
			ans = max(ans,(x1-x2)*a[i]);	
		}else if(a[i]<0){
			ll x1 = getmin(1,i,r[i]);
			ll x2 = getmax(1,l[i]-1,i-1);
			ans = max(ans,(x1-x2)*a[i]);
		}
	}
	printf("%lld\n",ans);
	return 0;
}