到简单线段树题,首先赋值不是加上,然后下放也是需要的(不会优化,而选择将就)
卑微的我只会做简单题……
#include <iostream> #include <string> #include <cstdio> using namespace std; int tree[400000],lazy[400000]; void push_up(int rt){ tree[rt]=tree[rt<<1]+tree[rt<<1|1]; } void push_down(int rt,int _size){ if(lazy[rt]){ tree[rt<<1]=(_size-(_size>>1))*lazy[rt]; tree[rt<<1|1]=(_size>>1)*lazy[rt]; lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; lazy[rt]=0; } } void build(int l,int r,int rt){ lazy[rt]=0; if(l==r){ tree[rt]=1; return; } int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); push_up(rt); } void add(int a,int b,int c,int l,int r,int rt){ if(a<=l&&b>=r){ tree[rt]=(r-l+1)*c; lazy[rt]=c; return; } push_down(rt,r-l+1); int mid=(r+l)>>1; if(a<=mid)add(a,b,c,l,mid,rt<<1); if(b>mid)add(a,b,c,mid+1,r,rt<<1|1); push_up(rt); } int main(){ int t; cin >> t; for(int i=1;i<=t;i++){ int n,m,a,b,c; cin >> n >> m; build(1,n,1); while(m--){ cin>>a>>b>>c; add(a,b,c,1,n,1); } printf("Case %d: The total value of the hook is %d.\n",i,tree[1]); } return 0; }
原文:https://www.cnblogs.com/sos3210/p/12344338.html