Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24474 Accepted Submission(s): 12194
#include<bits/stdc++.h> using namespace std; #define maxn 100005 struct ss { int l,r; int sum; int tag; }tr[4*maxn]; void build (int k,int s,int t) { tr[k].l=s; tr[k].r=t; tr[k].tag=0; if(s==t) { tr[k].sum=1; return ; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; //cout<<tr[k].sum<<endl; } void pushdown(int k,int m)//向下更新节点 { tr[k<<1].tag=tr[k].tag; tr[k<<1|1].tag=tr[k].tag; tr[k<<1].sum=tr[k].tag*(m-(m>>1)); tr[k<<1|1].sum=tr[k].tag*(m>>1); tr[k].tag=0; } void update (int k,int s,int t,int exm) { if(tr[k].l==s&&tr[k].r==t) { tr[k].tag=exm; tr[k].sum=exm*(t-s+1); return ; } if(tr[k].l==tr[k].r) return ; if(tr[k].tag) pushdown(k,tr[k].r-tr[k].l+1); int mid=(tr[k].l+tr[k].r)>>1; if(t<=mid) update(k<<1,s,t,exm); else if(s>mid) update(k<<1|1,s,t,exm); else { update(k<<1,s,mid,exm); update(k<<1|1,mid+1,t,exm); } tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; } int ask(int k,int s,int t) { if(tr[k].l==s&&tr[k].r==t) { return tr[k].sum; } if(tr[k].tag) pushdown(k,tr[k].r-tr[k].l+1); int mid=(tr[k].l+tr[k].r)>>1; int ans=0; if(t<=mid) ans+= ask(k<<1,s,t); else if(s>mid) ans+=ask(k<<1|1,s,t); else { ans+=ask(k<<1,s,mid); ans+=ask(k<<1|1,mid+1,t); } return ans; } int t; int n; int m; int qq,ww,ee; //int a[maxn]; int main() { while(scanf("%d",&t)!=EOF) { for(int i=1;i<=t;i++) { scanf("%d",&n); build(1,1,n); scanf("%d",&m); for(int j=1;j<=m;j++) { scanf("%d%d%d",&qq,&ww,&ee); update(1,qq,ww,ee); } printf("Case %d: The total value of the hook is %d.\n",i,ask(1,1,n)); } } return 0; }
原文:http://www.cnblogs.com/hsd-/p/5055780.html