线段树常用操作

系列操作I
题目描述
给出序列 a1,a2,…,an (0≤ai≤10^9) ,有关于序列的两种操作:
1. ai (1≤i≤n)  加上x(-10^3≤x≤10^3)
2. 求 max{al,al+1,…,ar} (1≤l≤r≤n)
输入格式
第一行包含两个数 n(1≤n≤10^5)和 m(1≤m≤10^5),表示序列长度和操作次数。
接下来一行n个数,以空格隔开,表示 a1,a2,…,an。
接下来 m 行,每行为以下两种格式之一。
0 i x ,表示 ai 加上 x 。
1 l r ,求 max{ al,al+1,…,ar }。
输出格式
对于每次询问,输出单独的一行表示答案。
样例数据 1
输入
5 3 
1 2 3 4 5 
1 1 5 
0 5 -5 
1 1 5
输出

4


#include <bits/stdc++.h>
using namespace std;

const int Max=100010;
struct cz1{int l,r,num;};
cz1 tree[Max*4];
int n,m;
int a[Max];

int get_int()
{
   int x=0,f=1;
   char c;
   for(c=getchar();(c<'0'||c>'9')&&(c!='-');c=getchar());
   if(c=='-') {c=getchar();f=-1;}
   for(;c>='0'&&c<='9';c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

void build(int root,int l,int r)
{
   tree[root].l=l;
   tree[root].r=r;
   if(l==r)
   {
   	 tree[root].num=a[l];
   	 return;
   }
   int mid=(l+r)/2;
   build(root<<1,l,mid);
   build(root<<1|1,mid+1,r);
   tree[root].num=max(tree[root<<1].num,tree[root<<1|1].num);
}

void add(int root,int i,int num)
{
   if(tree[root].l==tree[root].r)
   {
   	 tree[root].num+=num;
   	 return;
   }
   int mid=(tree[root].l+tree[root].r)/2;
   if(i<=mid) add(root<<1,i,num);
   if(i>mid) add(root<<1|1,i,num);
   tree[root].num=max(tree[root<<1].num,tree[root<<1|1].num);
}

long long Q(int root,int L,int R)   //第一种写法 
{
   if(L<=tree[root].l&&tree[root].r<=R)
   {
   	 return tree[root].num;
   }
   int mid=(tree[root].l+tree[root].r)/2;
   long long maxx=-10000000;
   if(L<=mid) maxx=max(maxx,Q(root<<1,L,R));
   if(R>mid) maxx=max(maxx,Q(root<<1|1,L,R));
   return maxx;
}

/*/long long Q(int root,int L,int R)  //第二种写法 
{
   if(L<=tree[root].l&&tree[root].r<=R)
   {
   	 return tree[root].num;
   }
   int mid=(tree[root].l+tree[root].r)/2;
   if(R<=mid) return Q(root<<1,L,R);
   else if(L>mid) return Q(root<<1|1,L,R);
   else return max(Q(root<<1,L,R), Q(root<<1|1,L,R));
}/*/


int main()
{
   //freopen("lx.in","r",stdin);

   n=get_int();
   m=get_int();
   for(int i=1;i<=n;i++) a[i]=get_int();

   build(1,1,n);

   while(m--)
   {
   	 int x,y,z;
   	 x=get_int();
   	 y=get_int();
   	 z=get_int();
   	 if(x==0) add(1,y,z); 
   	 else printf("%lld\n",Q(1,y,z));
   }

   return 0;
}

系列操作II
题目描述
给出数列 a1,a2,…,an(0≤ai≤10^9),有关序列的两种操作。
1. al,al+1,…,ar(1≤l≤r≤n)加上 x(-10^3≤x≤10^3)
2. 求 ai(1≤i≤n)
输入格式
第一行包含两个数 n(1≤n≤10^5)和 m(1≤m≤10^5),表示序列的长度和操作次数。
接下来的一行有 n 个数,以空格隔开,表示 a1,a2…an;
接下来的 m 行,每行为有以下两种格式之一:
0 1 r x ,表示al,al+1,…,ar 加上 x。
1 i     ,求 ai 。
输出格式
对于每次询问,输出单独一行表示答案。
样例数据 1
输入
5 3 
1 2 3 4 5 
1 5 
0 1 5 -7 
1 5
输出

-2


#include <bits/stdc++.h>
using namespace std;

const int Max=100010;
struct cz1{int l,r,num;};
cz1 tree[Max*4];
int n,m;
int a[Max];

int get_int()
{
   int x=0,f=1;
   char c;
   for(c=getchar();(c<'0'||c>'9')&&(c!='-');c=getchar());
   if(c=='-') {c=getchar();f=-1;}
   for(;c>='0'&&c<='9';c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

void build(int root,int l,int r)
{
   tree[root].l=l;
   tree[root].r=r;
   if(l==r)
   {
   	 tree[root].num=a[l];
   	 return;
   }
   int mid=(l+r)/2;
   build(root<<1,l,mid);
   build(root<<1|1,mid+1,r);
}

void add(int root,int L,int R,int num)
{
   if(L<=tree[root].l&&tree[root].r<=R)
   {
   	 tree[root].num+=num;
   	 return;
   }
   int mid=(tree[root].l+tree[root].r)/2;
   if(L<=mid) add(root<<1,L,R,num);
   if(R>mid) add(root<<1|1,L,R,num);
}

void pushdown(int root)
{
   tree[root<<1].num+=tree[root].num;
   tree[root<<1|1].num+=tree[root].num;
   tree[root].num=0;
}

int Q(int root,int pos)
{
   if(tree[root].l==tree[root].r)
   {
   	 return tree[root].num;
   }
   pushdown(root);
   int mid=(tree[root].l+tree[root].r)/2;
   if(pos<=mid) return Q(root<<1,pos);
   else return Q(root<<1|1,pos);
}

int main()
{
   //freopen("lx.in","r",stdin);

   n=get_int();
   m=get_int();
   for(int i=1;i<=n;i++) a[i]=get_int();

   build(1,1,n);

   while(m--)
   {
   	 int x;
   	 x=get_int();
   	 if(x==0)
   	 {
   	   int l,r,num;
   	   l=get_int();
   	   r=get_int();
   	   num=get_int();
   	   add(1,l,r,num);
   	 }
   	 else
   	 {
   	   int pos;
   	   pos=get_int();
   	   cout<<Q(1,pos)<<endl;
   	 }
   }

   return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值