Write a program which manipulates a sequence AA = {a_0, a_1, ..., a_{n-1}a0,a1,...,an−1} with the following operations:
- add(s, t, x)add(s,t,x) : add xx to a_s, a_{s+1}, ..., a_tas,as+1,...,at.
- find(s, t)find(s,t) : report the minimum value in a_s, a_{s+1}, ..., a_tas,as+1,...,at.
Note that the initial values of a_i ( i = 0, 1, ..., n-1 )ai(i=0,1,...,n−1) are 0.
Input
nn qq query_1query1 query_2query2 : query_qqueryq
In the first line, nn (the number of elements in AA) and qq (the number of queries) are given. Then, iith query query_iqueryi is given in the following format:
0 ss tt xx
or
1 ss tt
The first digit represents the type of the query. '0' denotes add(s, t, x)add(s,t,x) and '1' denotes find(s, t)find(s,t).
Output
For each findfind query, print the minimum value.
Constraints
- 1 ≤ n ≤ 1000001≤n≤100000
- 1 ≤ q ≤ 1000001≤q≤100000
- 0 ≤ s ≤ t < n0≤s≤t<n
- -1000 ≤ x ≤ 1000−1000≤x≤1000
Sample Input 1
6 7 0 1 3 1 0 2 4 -2 1 0 5 1 0 1 0 3 5 3 1 3 4 1 0 5
Sample Output 1
-2 0 1 -1
#include <iostream>
#include <cmath>
using namespace std;
#define ll long long
#define inf 1e31+5
const int N=1e5+5;
ll a[N];
struct node
{
int l,r;
ll lazy;
ll minn;
} tr[N<<2];
//维护
void pushup(int u)
{
tr[u].minn=min(tr[u<<1].minn,tr[u<<1|1].minn);
}
//建树
void build(int u,int l,int r)
{
tr[u].l=l;
tr[u].r=r;
tr[u].lazy=0;
if(l==r)
{
tr[u].minn=a[l];
return ;
}
int mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
pushup(u);
}
void pushdown(int u)
{
if(tr[u].lazy)
{
tr[u<<1].lazy+=tr[u].lazy;
tr[u<<1].minn=tr[u<<1].minn+tr[u].lazy;
tr[u<<1|1].lazy+=tr[u].lazy;
tr[u<<1|1].minn=tr[u].lazy+tr[u<<1|1].minn;
tr[u].lazy=0;
}
}
//区间修改
void modify(int u,int l,int r,ll k)
{
if(l<=tr[u].l&&r>=tr[u].r)
{
tr[u].lazy+=k;
tr[u].minn=k+tr[u].minn;
return;
}
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid)
modify(u<<1,l,r,k);
if(r>mid)
modify(u<<1|1,l,r,k);
pushup(u);
}
//区间查询
ll query(int u,int l,int r)
{
if(l<=tr[u].l&&r>=tr[u].r)
{
return tr[u].minn;
}
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
ll res=inf;
if(l<=mid)
{
res=min(res,query(u<<1,l,r));
}
if(r>mid)
{
res=min(res,query(u<<1|1,l,r));
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n+5;i++)
{
a[i]=0;
}
build(1,1,n);
for(int i=1;i<=m;i++)
{
int t;
cin>>t;
if(t==0)
{
int l,r;
ll k;
cin>>l>>r>>k;
modify(1,l+1,r+1,k);//注意下标从1开始
}
else
{
int l,r;
cin>>l>>r;
cout<<query(1,l+1,r+1)<<endl;
}
}
}