1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII;
#define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ls rt<<1 #define rs rt<<1|1
const int maxn = 1000000; struct Seg { ll sum; int a,b,cnt; void spfy(int t) { if (a<=t) return; sum-=(ll)(a-t)*cnt; a=t; } } s[maxn<<2];
void push_up(int rt) { s[rt].sum=s[ls].sum+s[rs].sum; s[rt].a=max(s[ls].a,s[rs].a); s[rt].b=max(s[ls].b,s[rs].b); s[rt].cnt=0; if (s[ls].a==s[rt].a) s[rt].cnt+=s[ls].cnt; else s[rt].b=max(s[rt].b,s[ls].a); if (s[rs].a==s[rt].a) s[rt].cnt+=s[rs].cnt; else s[rt].b=max(s[rt].b,s[rs].a); }
void push_down(int rt) { s[ls].spfy(s[rt].a); s[rs].spfy(s[rt].a); }
void build(int l,int r,int rt) { if (l==r) { scanf("%d",&s[rt].a); s[rt].sum=s[rt].a; s[rt].b=-1; s[rt].cnt=1; return; } int m=(l+r)>>1; build(lson); build(rson); push_up(rt); }
void update(int L,int R,int t,int l,int r,int rt) { if (s[rt].a<=t) return; if (L<=l&&r<=R&&s[rt].b<t) {/* Do not add '=' !!! if s[rt].b == t, it supposed to be push_down, because s[rt].b, s[rt].cnt cannot be determined. */ s[rt].spfy(t); return; } push_down(rt); int m=(l+r)>>1; if (L<=m&&s[ls].a>t) update(L,R,t,lson); if (R>m&&s[rs].a>t) update(L,R,t,rson); push_up(rt); }
int getmax(int L,int R,int l,int r,int rt) { if (L<=l&&r<=R) return s[rt].a; int m=(l+r)>>1; int res=0; push_down(rt); if (L<=m) res=max(res,getmax(L,R,lson)); if (m<R) res=max(res,getmax(L,R,rson)); return res; }
ll getsum(int L,int R,int l,int r,int rt) { if (L<=l&&r<=R) return s[rt].sum; int m=(l+r)>>1; ll res=0; push_down(rt); if (L<=m) res+=getsum(L,R,lson); if (m<R) res+=getsum(L,R,rson); return res; }
int main() { int T,n,m; scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); build(1,n,1); while (m--) { int op,x,y; scanf("%d%d%d",&op,&x,&y); if (op==0) { int t; scanf("%d",&t); update(x,y,t,1,n,1); } else if (op==1) { printf("%d\n",getmax(x,y,1,n,1)); } else { printf("%lld\n",getsum(x,y,1,n,1)); } } } return 0; }
|