线段树单点更新区间查询
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
struct node{
int l,r,value,lazy;
}tree[maxn<<2];
void pushup(int m)
{
tree[m].value=tree[m<<1].value+tree[m<<1|1].value;
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
if(l==r)
{
cin>>tree[m].value;
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
}
void updata(int m,int inx,int value)
{
if(tree[m].l==inx&&tree[m].r==inx)
{
tree[m].value+=value;
return;
}
int mid=(tree[m].l+tree[m].r)>>1;
if(inx<=mid) updata(m<<1,inx,value);
else updata(m<<1|1,inx,value);
pushup(m);
}
int query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
return tree[m].value;
int mid=(tree[m].l+tree[m].r)>>1;
int temp;
if(r<=mid)temp=query(m<<1,l,r);
else if(l>mid) temp=query(m<<1|1,l,r);
else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
return temp;
}
int main()
{
int n,m,t,tt;
cin>>t;
for(tt=1;tt<=t;tt++)
{
cin>>n;
printf("Case %d:\n",tt);
int i,j,g;
build(1,1,n);
return 0;
}
线段树单点更新区间最大值
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2e5+5;
struct Tree{
int m,l,r,val;
}tree[maxn<<2];
void pushup(int m)
{
tree[m].val=max(tree[m<<1].val,tree[m<<1|1].val);
}
/*void pushdown(int m)
{
if(tree[m].lazy)
{
tree[m<<1].lazy+=tree[m].lazy;
tree[m<<1|1].lazy+=tree[m].lazy;
tree[m<<1].val+=(tree[m<<1].r-tree[m<<1].l+1)*tree[m].lazy;
tree[m<<1|1].val+=(tree[m<<1|1].r-tree[m<<1|1].l+1)*tree[m].lazy;
tree[m].lazy=0;
}
}*/
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
if(l==r)
{
scanf("%d",&tree[m].val);
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
}
void update(int m,int x,int val)
{
if(tree[m].l==x&&tree[m].r==x)
{
tree[m].val=val;
return;
}
int mid=(tree[m].l+tree[m].r)>>1;
if(x<=mid)
update(m<<1,x,val);
else update(m<<1|1,x,val);
pushup(m);
}
int maxx;
void query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
{
maxx=max(maxx,tree[m].val);
return;
}
int mid=(tree[m].l+tree[m].r)>>1;
if(r<=mid)
query(m<<1,l,r);
else if(l>mid)
query(m<<1|1,l,r);
else
query(m<<1,l,mid),query(m<<1|1,mid+1,r);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
for(int i=0;i<m;i++)
{
char s[5];
int x,y;
scanf("%s%d%d",s,&x,&y);
if(s[0]=='Q')
{
maxx=0;
query(1,x,y);
printf("%d\n",maxx);
}
else
update(1,x,y);
}
}
}
线段树区间更新,要注意 区间求和 会否超int
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
struct node{
int l,r,value,lazy;
}tree[maxn<<2];
void pushup(int m)
{
tree[m].value=tree[m<<1].value+tree[m<<1|1].value;
}
void pushdown(int m)
{
if(tree[m].lazy)
{
tree[m<<1].lazy+=tree[m].lazy;
tree[m<<1|1].lazy+=tree[m].lazy;
tree[m<<1].value+=tree[m].lazy*(tree[m<<1].r-tree[m<<1].l+1);
tree[m<<1|1].value+=tree[m].lazy*(tree[m<<1|1].r-tree[m<<1|1].l+1);
tree[m].lazy=0;
}
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
tree[m].lazy=0;
if(l==r)
{
cin>>tree[m].value;
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
}
void updata(int m,int l,int r,int value)
{
if(tree[m].l==l&&tree[m].r==r)
{
tree[m].lazy+=value;
tree[m].value+=(r-l+1)*value;
return;
}
pushdown(m);
int mid=(tree[m].l+tree[m].r)>>1;
if(r<=mid) updata(m<<1,l,r,value);
else if(l>mid) updata(m<<1|1,l,r,value);
else
{
updata(m<<1,l,mid,value);
updata(m<<1|1,mid+1,r,value);
}
pushup(m);
}
int query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
return tree[m].value;
pushdown(m);
int mid=(tree[m].l+tree[m].r)>>1;
int temp;
if(r<=mid)temp=query(m<<1,l,r);
else if(l>mid) temp=query(m<<1|1,l,r);
else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
return temp;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=0;i<m;i++)
{
int l,r;
char s[5];
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%d\n",query(1,l,r));
}
else
{
int c;
scanf("%d%d%d",&l,&r,&c);
updata(1,l,r,c);
}
}
return 0;
}