NEFU 1111 line segment tree Interval Update + lazy tag Introduction
Question: Chinese questions are not explained
The idea is to update the interval in a simple way. You can check the explanation of the lazy mark on the Internet. I will not talk about it here.
#include
#include
#include
#include #include
#include
using namespace std;long long num[1000010];long long num1[1000010];void buildtree(int le,int ri,int node){ if(le==ri){ scanf("%lld",&num[node]); return ; } int t=(le+ri)>>1; buildtree(le,t,node<<1); buildtree(t+1,ri,node<<1|1); num[node]=min(num[node<<1],num[node<<1|1]);}void pushdown(int node){ if(num1[node]){ num1[node<<1]+=num1[node]; num1[node<<1|1]+=num1[node]; num[node<<1]+=num1[node]; num[node<<1|1]+=num1[node]; num1[node]=0; }}void add(int d,int a,int b,int le,int ri,int node){ if(a<=le&&ri<=b){ num1[node]+=d; num[node]+=d; return ; } pushdown(node); int t=(le+ri)>>1; if(a<=t) add(d,a,b,le,t,node<<1); if(b>t) add(d,a,b,t+1,ri,node<<1|1); num[node]=min(num[node<<1],num[node<<1|1]);}long long query(int a,int b,int le,int ri,int node){ if(a<=le&&ri<=b){ return num[node]; } pushdown(node); long long ans=999999999; int t=(le+ri)>>1; if(a<=t) ans=min(ans,query(a,b,le,t,node<<1)); if(b>t) ans=min(ans,query(a,b,t+1,ri,node<<1|1)); return ans;}int main(){ int n,m,a,b,c,d; while(scanf("%d",&n)!=-1){ buildtree(1,n,1); scanf("%d",&m); while(m--){ scanf("%d",&a); if(a==1){ scanf("%d%d",&b,&c); b++;c++; if(b<=c) printf("%lld\n",query(b,c,1,n,1)); else{ long long ans1=query(b,n,1,n,1); long long ans2=query(1,c,1,n,1); printf("%lld\n",min(ans1,ans2)); } }else{ scanf("%d%d%d",&b,&c,&d); b++;c++; if(b<=c){ add(d,b,c,1,n,1); } else{ add(d,b,n,1,n,1); add(d,1,c,1,n,1); } }// for(int i=1;i<=4*n;i++)// cout<