PART 1
单点更新,区间询问
1443区间和升级版
题目描述 这次的任务是:在伴随着数字改变的情况下,试试统计某段的和。 输入 第一行两个整数n和m,表示有一个长度为n个序列和m个操作
接下来m行,每行的内容属于以下一种:
Change x a:把第x个数改成a
Query x y:求出[x,y]这段区间的和。
输出 对于每个query,输出结果。
最基本的线段树运用,
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,x,y) for(int i=(x);i<=(y);i++)
#define M 100005
int A[M];
struct node{int L,R,sum;}tree[M*2];
void build(int L,int R,int p){
tree[p].L=L,tree[p].R=R;
if(L==R){tree[p].sum=A[L];return;}//A是输入的数组
int mid=(L+R)>>1;
build(L,mid,2*p);
build(mid+1,R,p*2+1);
tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
int query(int L,int R,int p){
if(tree[p].L==L&&tree[p].R==R)return tree[p].sum;
int mid=(tree[p].L+tree[p].R)>>1;
if(R<=mid)return query(L,R,2*p);
else if(L>mid)return query(L,R,2*p+1);
else return query(L,mid,2*p)+query(mid+1,R,2*p+1);
}
void update(int x,int a,int p){
if(tree[p].L==tree[p].R){
tree[p].sum=a;return;
}
int mid=(tree[p].L+tree[p].R)>>1;
if(x<=mid)update(x,a,2*p);
else update(x,a,2*p+1);
tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
int main(){
int n,m;
cin>>n>>m;
FOR(i,1,n)scanf("%d",&A[i]);
build(1,n,1);
FOR(i,1,m){
char A[10];
scanf("%s",A);
int x,y;
if(A[0]=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y,1));
}
else {
scanf("%d%d",&x,&y);
update(x,y,1);
}
}
return 0;
}
PART2
区间更新,区间询问
1444区间和继续升级
题目描述 这次的任务是:在伴随着某段数字的改变的情况,试试统计某段的和。 输入 第一行两个整数n和m,表示有一个长度为n个序列和m个操作
接下来m行,每行的内容属于以下一种:
Add x y a:把下标在[x,y]这个区间的都加上a(a∈[-10000,10000])。
Query x y:求出下标在[x,y]这段区间的和。
输出 对于每个询问输出相应的结果。
这里运用到了延迟更新的思想,即用到左儿子和右儿子是才去更新他们的值
这样可以控制询问和更新的复杂度仍保持在O(logn),延迟更新能很好地体现up和down的作用
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,x,y) for(int i=(x);i<=(y);i++)
#define M 100005
#define ll long long
struct node{
int l,r,add;
ll sum;
}tree[M<<2];
int A[M];
void Up(int p){
tree[p].sum=tree[p<<1].sum+tree[(p<<1)+1].sum;
return;
}
void Down(int p){
if(tree[p].add==0)return;
tree[p<<1].sum+=(tree[p<<1].r-tree[p<<1].l+1)*tree[p].add;
tree[(p<<1)+1].sum+=(tree[(p<<1)+1].r-tree[(p<<1)+1].l+1)*tree[p].add;
tree[p<<1].add+=tree[p].add;
tree[(p<<1)+1].add+=tree[p].add;
tree[p].add=0;
}
void build(int L,int R,int p){
tree[p].l=L,tree[p].r=R;
tree[p].add=0;
if(L==R){tree[p].sum=A[L];return;}//A是输入的数组
int mid=(L+R)>>1;
build(L,mid,2*p);
build(mid+1,R,(p<<1)+1);
Up(p);
}
void update(int l,int r,int a,int p){
if(tree[p].l==l&&tree[p].r==r){
tree[p].sum+=1LL*a*(r-l+1);
tree[p].add+=a;
return;
}
Down(p);
int mid=(tree[p].l+tree[p].r)>>1;
if(mid>=r)update(l,r,a,p<<1);
else if(mid<l)update(l,r,a,p<<1|1);
else update(l,mid,a,p<<1),update(mid+1,r,a,p<<1|1);
Up(p);
}
ll query(int l,int r,int p){
if(tree[p].l==l&&tree[p].r==r){
return tree[p].sum;
}
Down(p);
int mid=(tree[p].l+tree[p].r)>>1;
if(mid>=r)return query(l,r,p<<1);
else if(mid<l)return query(l,r,p<<1|1);
else return 1LL*query(l,mid,p<<1)+query(mid+1,r,p<<1|1);
}
int main(){
int n,m;
cin>>n>>m;
FOR(i,1,n)scanf("%d",&A[i]);
build(1,n,1);
FOR(i,1,m){
char C[10];
scanf("%s",C);
if(C[0]=='Q'){
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1));
}
else {
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,1);
}
}
return 0;
}
PART3
线段树优化dp
线段树可以优化类似于LIS继承具有单调性的题目
复杂度能由n*n优化到nlog(n);
2950: 最长完美子序列
题目描述 如果一个序列,它的任意两个相邻的元素之差都不超过K,那么这个序列就被称作完美序列。
一个序列的子序列,就是从这个序列中,按照从前往后的顺序,取出任意多个数字(不一定连续)组成的新序列。
一个序列的完美子序列,就是从这个序列中取出一个子序列,且这个子序列是完美序列。
给定一个序列,求这个序列的最长完美子序列。
输入 第一行两个整数n和K,n表示序列的长度。
第二行n个元素。
输出 输出最长完美子序列的长度。
#include<bits/stdc++.h>
#define M 200005
using namespace std;
struct node{
int L,R,val;
}tree[4*M];
int n,A[M],tmp;
int B[M];
void build(int L,int R,int p){
tree[p].L=L,tree[p].R=R;tree[p].val=0;
if(L==R)return;
int mid=(L+R)>>1;
build(L,mid,p*2);
build(mid+1,R,p*2+1);
}
int query(int L,int R,int p){
if(tree[p].L==L&&tree[p].R==R){
return tree[p].val;
}
int mid=(tree[p].L+tree[p].R)>>1;
if(R<=mid)return query(L,R,p*2);
else if(L>mid)return query(L,R,2*p+1);
else return max(query(L,mid,p*2),query(mid+1,R,p*2+1));
}
void update(int x,int a,int p){
if(tree[p].L==tree[p].R){
tree[p].val=max(tree[p].val,a);
return;
}
int mid=(tree[p].L+tree[p].R)>>1;
if(x<=B[mid]) update(x,a,p*2);
else update(x,a,p*2+1);
tree[p].val=max(tree[2*p].val,tree[p*2+1].val);
}
int main(){
int k;
cin>>n>>k;
build(1,n,1);
for(int i=1;i<=n;i++)scanf("%d",&A[i]),B[i]=A[i];
sort(B+1,B+n+1);
for(int i=1;i<=n;i++){
int l=lower_bound(B+1,B+1+n,A[i]-k)-B;
int r=upper_bound(B+1,B+1+n,A[i]+k)-B-1;
tmp=0;
if(r>=l)tmp=query(l,r,1);
update(A[i],tmp+1,1);
}
printf("%d\n",query(1,n,1));
return 0;
}