标签:
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<algorithm> #include<string.h> #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R const int maxn=101000; int sumv[maxn*4]; int col[maxn*4]; void PushUP(int rt){ sumv[rt]=sumv[rt*2]+sumv[rt*2+1]; } void PushDown(int rt,int len){ if(col[rt]){ col[rt*2]=col[rt]; col[rt*2+1]=col[rt]; sumv[rt*2]=col[rt]*(len-len/2); sumv[rt*2+1]=col[rt]*(len/2); col[rt]=0; } } void build(int rt,int L,int R){ col[rt]=0; if(L==R){ sumv[rt]=1; return ; } build(lson); build(rson); PushUP(rt); } void update(int rt,int L,int R,int l_ran,int r_ran,int val){ if(l_ran<=L&&R<=r_ran){ sumv[rt]=val*(R-L+1); col[rt]=val; return ; } PushDown(rt,R-L+1); if(l_ran<=mid){ update(lson,l_ran,r_ran,val); } if(r_ran>mid){ update(rson,l_ran,r_ran,val); } PushUP(rt); } int main(){ int t,cnt=0; scanf("%d",&t); while(t--){ int n,m; scanf("%d%d",&n,&m); build(1,1,n); for(int i=0;i<m;i++){ int ta,tb,tc; scanf("%d%d%d",&ta,&tb,&tc); update(1,1,n,ta,tb,tc); } printf("Case %d: The total value of the hook is %d.\n",++cnt,sumv[1]); } }
HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4392266.html