Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 349 Accepted Submission(s): 57
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1000010; 5 struct node { 6 int cover,s,tag,maxtag,maxv; 7 LL sum; 8 } tree[maxn<<2]; 9 node put(node c,int t) { 10 if(!t) return c; 11 if(c.cover != c.s) c.maxv = t; 12 c.maxtag = t; 13 c.sum += (LL)t*(c.s - c.cover); 14 c.cover = c.s; 15 return c; 16 } 17 node calc(const node &a,const node &b,int t) { 18 node c; 19 c.tag = t; 20 c.s = a.s + b.s; 21 c.sum = a.sum + b.sum; 22 c.cover = a.cover + b.cover; 23 c.maxv = max(a.maxv,b.maxv); 24 c.maxtag = max(a.maxtag,b.maxtag); 25 return put(c,t); 26 } 27 int tmp,n,m; 28 void build(int L,int R,int v) { 29 tree[v].tag = 0; 30 tree[v].s = R - L + 1; 31 if(L == R) { 32 scanf("%d",&tmp); 33 tree[v].sum = tree[v].tag = tree[v].maxtag = tree[v].maxv = tmp; 34 tree[v].cover = 1; 35 return; 36 } 37 int mid = (L + R)>>1; 38 build(L,mid,v<<1); 39 build(mid+1,R,v<<1|1); 40 tree[v] = calc(tree[v<<1],tree[v<<1|1],0); 41 } 42 node query(int L,int R,int lt,int rt,int v) { 43 if(lt <= L && rt >= R) return tree[v]; 44 int mid = (L + R)>>1; 45 if(rt <= mid) return put(query(L,mid,lt,rt,v<<1),tree[v].tag); 46 if(lt > mid) return put(query(mid+1,R,lt,rt,v<<1|1),tree[v].tag); 47 return calc(query(L,mid,lt,rt,v<<1),query(mid+1,R,lt,rt,v<<1|1),tree[v].tag); 48 } 49 void cleartag(int v,int t) { 50 if(tree[v].maxtag < t) return; 51 if(tree[v].tag >= t) tree[v].tag = 0; 52 if(tree[v].s > 1) { 53 cleartag(v<<1,t); 54 cleartag(v<<1|1,t); 55 } 56 if(tree[v].s == 1) { 57 tree[v].sum = tree[v].maxtag = tree[v].maxv = tree[v].tag; 58 tree[v].cover = (tree[v].tag > 0); 59 } else tree[v] = calc(tree[v<<1],tree[v<<1|1],tree[v].tag); 60 } 61 void update(int L,int R,int lt,int rt,int t,int v) { 62 if(tree[v].tag && tree[v].tag <= t) return; 63 if(lt <= L && rt >= R) { 64 cleartag(v,t); 65 tree[v].tag = t; 66 if(L == R) { 67 tree[v].sum = tree[v].tag = tree[v].maxv = tree[v].maxtag = t; 68 tree[v].cover = (tree[v].tag > 0); 69 } else tree[v] = calc(tree[v<<1],tree[v<<1|1],t); 70 } else { 71 int mid = (L + R)>>1; 72 if(rt <= mid) update(L,mid,lt,rt,t,v<<1); 73 else if(lt > mid) update(mid+1,R,lt,rt,t,v<<1|1); 74 else { 75 update(L,mid,lt,rt,t,v<<1); 76 update(mid+1,R,lt,rt,t,v<<1|1); 77 } 78 tree[v] = calc(tree[v<<1],tree[v<<1|1],tree[v].tag); 79 } 80 } 81 int main() { 82 int kase,op,x,y,t; 83 scanf("%d",&kase); 84 while(kase--) { 85 scanf("%d%d",&n,&m); 86 build(1,n,1); 87 while(m--) { 88 scanf("%d%d%d",&op,&x,&y); 89 if(!op) { 90 scanf("%d",&t); 91 update(1,n,x,y,t,1); 92 } else if(op == 1) printf("%d\n",query(1,n,x,y,1).maxv); 93 else printf("%I64d\n",query(1,n,x,y,1).sum); 94 } 95 } 96 return 0; 97 }