标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23137 Accepted Submission(s):
11600
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> #define MAX 100100 #define INF 0x3f3f3f using namespace std; int sum[MAX<<2]; int change[MAX<<2]; void pushup(int o) { sum[o]=sum[o<<1]+sum[o<<1|1]; } void pushdown(int o,int m) { if(change[o]) { change[o<<1]=change[o<<1|1]=change[o]; sum[o<<1]=change[o]*(m-(m>>1)); sum[o<<1|1]=change[o]*(m>>1); change[o]=0; } } void gettree(int o,int l,int r) { sum[o]=1;change[o]=0; if(l==r) return ; int mid=(l+r)>>1; gettree(o<<1,l,mid); gettree(o<<1|1,mid+1,r); pushup(o); } void update(int o,int l,int r,int L,int R,int v) { if(L<=l&&R>=r) { change[o]=v; sum[o]=v*(r-l+1); return ; } pushdown(o,r-l+1); int mid=(r+l)>>1; if(L<=mid) update(o<<1,l,mid,L,R,v); if(R>mid) update(o<<1|1,mid+1,r,L,R,v); pushup(o); } int find(int o,int l,int r,int L,int R) { if(L<=l&&R>=r) { return sum[o]; } pushdown(o,r-l+1); int ans=0; int mid=(r+l)>>1; if(L<=mid) ans+=find(o<<1,l,mid,L,R); if(R>mid) ans+=find(o<<1|1,mid+1,r,L,R); return ans; } int main() { int t,k,i,j; int n,m; k=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int a,b,c; gettree(1,1,n); while(m--) { scanf("%d%d%d",&a,&b,&c); update(1,1,n,a,b,c); } printf("Case %d: The total value of the hook is ",k++); printf("%d.\n",find(1,1,n,1,n)); } return 0; }
hdoj 1689 Just a Hook【线段树区间修改】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4758952.html