Hdu 3954 线段树瞎搞
升级
注意:可以连升几级
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 7; #define lc rt<<1 #define rc rt<<1|1 int a[maxn << 2]; struct tree { int l , r; int level; //the max(level) needed min(exps) int mx,mi; int lazy; #define l(x) t[x].l #define r(x) t[x].r #define level(x) t[x].level #define mx(x) t[x].mx #define mi(x) t[x].mi #define lazy(x) t[x].lazy }t[maxn << 2]; void push_down(int rt){ if(lazy(rt)){ mx(lc) += level(lc) * lazy(rt); mx(rc) += level(rc) * lazy(rt); mi(lc) -= lazy(rt); mi(rc) -= lazy(rt); lazy(lc) += lazy(rt); lazy(rc) += lazy(rt); lazy(rt) = 0; } } void push_up(int rt){ level(rt) = max(level(lc),level(rc)); mx(rt) = max(mx(lc),mx(rc)); mi(rt) = min(mi(lc),mi(rc)); } void build(int rt,int l ,int r){ t[rt].l = l; r(rt) = r; level(rt) = 1; lazy(rt) = mx(rt) = 0; mi(rt) = ceil((a[level(rt) + 1] - mx(rt))/level(rt)); if(l == r){ return; } int mid = (l + r) >> 1; build(lc,l,mid); build(rc,mid + 1,r); push_up(rt); } void up_date(int rt,int l ,int r,int pos){ if(l <=l(rt) && r >= r(rt) && mi(rt) > pos){ mi(rt) = mi(rt) - pos; mx(rt) = mx(rt) + (pos*level(rt)); lazy(rt) = lazy(rt) + pos; return; } if(l <=l(rt) && r >= r(rt) && l(rt) == r(rt)){ mx(rt) = mx(rt) + pos*level(rt); t[rt].lazy=t[rt].lazy+pos; while(mx(rt) >= a[level(rt) + 1]) level(rt)++; lazy(rt) = 0; mi(rt) = ceil((a[level(rt) + 1] - mx(rt))/level(rt)); return; } push_down(rt); int mid = (l(rt) + r(rt)) >> 1; if(r <= mid) up_date(lc,l,r,pos); else if(l > mid) up_date(rc,l,r,pos); else up_date(lc,l,mid,pos),up_date(rc,mid+1,r,pos); push_up(rt); } int query(int rt,int l,int r){ if(l == l(rt) && r == r(rt)){ return mx(rt); } push_down(rt); int mid = (l(rt) + r(rt)) >> 1; if(r <= mid) return query(lc,l,r); else if(l > mid) return query(rc,l,r); else return max(query(lc,l,mid),query(rc,mid+1,r)); } int main(int argc, char const *argv[]) { int t; scanf("%d",&t); int kase = 0; while(t--){ memset(a,0,sizeof a); printf("Case %d:\n",++kase); int n , k , q; scanf("%d%d%d",&n,&k,&q); //for(int i = ) for(int i = 2;i <= k;i++) scanf("%d",&a[i]); a[k + 1] = 999999999; build(1,1,n); while(q--){ string str; cin >> str; if(str == "W"){ int a,b,c; scanf("%d%d%d",&a,&b,&c); up_date(1,a,b,c); } else{ int a,b; scanf("%d%d",&a,&b); printf("%d\n",query(1,a,b) ); } } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e4 + 7; #define lc rt<<1 #define rc rt<<1|1 int a[200]; struct tree { int l , r; int level; //the max(level) needed min(exps) int mx,mi; int lazy; #define l(x) t[x].l #define r(x) t[x].r #define level(x) t[x].level #define mx(x) t[x].mx #define mi(x) t[x].mi #define lazy(x) t[x].lazy }t[maxn << 2]; void push_down(int rt){ if(lazy(rt)){ mx(lc) += level(lc) * lazy(rt); mx(rc) += level(rc) * lazy(rt); mi(lc) -= lazy(rt); mi(rc) -= lazy(rt); lazy(lc) += lazy(rt); lazy(rc) += lazy(rt); lazy(rt) = 0; } } void push_up(int rt){ level(rt) = max(level(lc),level(rc)); mx(rt) = max(mx(lc),mx(rc)); mi(rt) = min(mi(lc),mi(rc)); } void build(int rt,int l ,int r){ t[rt].l = l; r(rt) = r; level(rt) = 1; lazy(rt) = mx(rt) = 0; mi(rt) = ceil((a[level(rt) + 1] - mx(rt))/level(rt)); //cout <<1<<endl; if(l == r){ return; } int mid = (l + r) >> 1; build(lc,l,mid); build(rc,mid + 1,r); push_up(rt); } void up_date(int rt,int l ,int r,int pos){ if(l <=l(rt) && r >= r(rt) ){ if(mi(rt) > pos){ mi(rt) = mi(rt) - pos; mx(rt) = mx(rt) + (pos*level(rt)); lazy(rt) = lazy(rt) + pos; return; } else { if(l == r){ mx(rt) = mx(rt) + pos*level(rt); t[rt].lazy=t[rt].lazy+pos; while(mx(rt) >= a[level(rt) + 1]) level(rt)++; //lazy(rt) = 0; mi(rt) = ceil((a[level(rt) + 1] - mx(rt))/level(rt)); return; } } } push_down(rt); int mid = (l(rt) + r(rt)) >> 1; if(r <= mid) up_date(lc,l,r,pos); else if(l > mid) up_date(rc,l,r,pos); else up_date(lc,l,mid,pos),up_date(rc,mid+1,r,pos); push_up(rt); } int query(int rt,int l,int r){ if(l == l(rt) && r == r(rt)){ return mx(rt); } push_down(rt); int mid = (l(rt) + r(rt)) >> 1; if(r <= mid) return query(lc,l,r); else if(l > mid) return query(rc,l,r); else return max(query(lc,l,mid),query(rc,mid+1,r)); } int main(int argc, char const *argv[]) { int t; scanf("%d",&t); int kase = 0; while(t--){ memset(a,0,sizeof a); printf("Case %d:\n",++kase); int n , k , q; scanf("%d%d%d",&n,&k,&q); //for(int i = ) for(int i = 2;i <= k;i++) scanf("%d",&a[i]); a[k + 1] = 100861111; build(1,1,n); while(q--){ string str; cin >> str; if(str == "W"){ int a,b,c; scanf("%d%d%d",&a,&b,&c); up_date(1,a,b,c); } else{ int a,b; scanf("%d%d",&a,&b); printf("%d\n",query(1,a,b) ); } } printf("\n"); } return 0; }