题目:
http://acm.hdu.edu.cn/showproblem.php?pid=6315
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
#include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define pqueue priority_queue #define NEW(a,b) memset(a,b,sizeof(a)) const double pi=4.0*atan(1.0); const double e=exp(1.0); const int maxn=3e6+8; typedef long long LL; typedef unsigned long long ULL; //typedef pair<LL,LL> P; const LL mod=1e9+7; using namespace std; struct node{ LL l,r,sum,g,mi; LL lazy; LL mid(){ return (l+r)>>1; } }a[maxn]; int b[maxn]; void build(int l,int r,int num){ a[num].l=l; a[num].r=r; a[num].lazy=0; if(l==r){ a[num].sum=0; a[num].g=0; a[num].mi=b[l]; } else{ build(l,a[num].mid(),num<<1); build(a[num].mid()+1,r,(num<<1)|1); a[num].g=a[num<<1].g+a[(num<<1)|1].g; a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi); } } void as(int d) { if(a[d].lazy) { a[(d<<1)].lazy+=a[d].lazy; a[(d<<1|1)].lazy+=a[d].lazy; a[(d<<1)].mi-=a[d].lazy; a[(d<<1|1)].mi-=a[d].lazy; a[d].lazy=0; } } LL Find(int l,int r,int num){ if(a[num].l==l&&a[num].r==r){ return a[num].g; } if(l>a[num].mid()){ return Find(l,r,(num<<1)|1); } else if(r<=a[num].mid()){ return Find(l,r,num<<1); } else{ return Find(l,a[num].mid(),num<<1)+Find(a[num].mid()+1,r,(num<<1)|1); } } void add(int l,int r,int num,LL x){ if(a[num].l==l&&a[num].r==r||x==0){ a[num].lazy+=x; a[num].mi-=x; if(a[num].mi>0){ return ; } else if(l!=r){ as(num); add(l,a[num].mid(),num<<1,0); add(a[num].mid()+1,r,(num<<1)|1,0); a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi); a[num].g=a[num<<1].g+a[(num<<1)|1].g; return; } } if(l==r&&a[num].l==l&&a[num].r==r) { if(a[num].mi<=0) { a[num].mi=a[num].lazy=0; a[num].mi=b[l]; a[num].g++; } return; } as(num); if(l>a[num].mid()){ add(l,r,(num<<1)|1,x); } else if(r<=a[num].mid()){ add(l,r,num<<1,x); } else { add(l,a[num].mid(),num<<1,x); add(a[num].mid()+1,r,(num<<1)|1,x); } a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi); a[num].g=a[num<<1].g+a[(num<<1)|1].g; //cout<<‘a‘<<a[num].l<<‘ ‘<<a[num].r<<‘ ‘<<a[num].g<<endl; } int main(){ fio; int n,m; string op; int x,y; while(cin>>n>>m){ for(int i=1;i<=n;i++){ cin>>b[i]; } build(1,n,1); while(m--){ cin>>op>>x>>y; if(op[0]==‘a‘){ add(x,y,1,1); } else if(op[0]==‘q‘){ add(x,y,1,0); cout<<Find(x,y,1)<<endl; } } } }
HDU 6351 Naive Operations(线段树)
原文:https://www.cnblogs.com/Profish/p/9367652.html