#include<iostream>
#include<cstdio>
#define MAX 200005
#define INF 0x3fffffff
long long c,tree[4*MAX],add[4*MAX];
long long _min(long long a,long long b)
{
return a<b?a:b;
}
void pushdown(int n)
{
if(add[n]!=0)
{
add[2*n]+=add[n];
add[2*n+1]+=add[n];
tree[2*n]+=add[n];
tree[2*n+1]+=add[n];
add[n]=0;
}
}
void pushup(int n)
{
tree[n]=_min(tree[2*n],tree[2*n+1]);
}
void build(int s,int e,int n)
{
add[n]=0;
if(s==e)
{
scanf("%I64d",&tree[n]);
return ;
}
int mid=(s+e)/2;
build(s,mid,2*n);
build(mid+1,e,2*n+1);
pushup(n);
}
void modify(int a,int b,long long c,int s,int e,int n)
{
if(a<=s&&e<=b)
{
add[n]+=c;
tree[n]+=c;
return ;
}
pushdown(n);
int mid=(s+e)/2;
if(a<=mid) modify(a,b,c,s,mid,2*n);
if(mid<b) modify(a,b,c,mid+1,e,2*n+1);
pushup(n);
}
long long query(int a,int b,int s,int e,int n)
{
if(a<=s&&e<=b)
{
return tree[n];
}
pushdown(n);
int mid=(e+s)/2;
long long s2,s1;s1=s2=INF;
if(a<=mid) s1=_min(s1,query(a,b,s,mid,2*n));
if(mid<b) s2=_min(s2,query(a,b,mid+1,e,2*n+1));
return _min(s1,s2);
}
int main()
{
int n,m,i,a,b;
char q;
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&a,&b);
q=getchar();
if(q==' ')
{
scanf("%I64d",&c);
if(a<=b)
{
modify(a+1,b+1,c,1,n,1);
}
else
{
modify(a+1,n,c,1,n,1);
modify(1,b+1,c,1,n,1);
}
}
else
{
if(a<=b) printf("%I64d\n",query(a+1,b+1,1,n,1) );
else
{
long long s1=query(a+1,n,1,n,1);
long long s2=query(1,b+1,1,n,1);
printf("%I64d\n",_min(s1,s2) );
}
}
}
return 0;
}
CodeForces 52C Circular RMQ (区间更新线段树)
最新推荐文章于 2022-06-05 13:30:58 发布