Segment update of HDU 1698 Line Segment
Question: Give You T group data, N numbers in each group, initially 1, M operations. Each operation updates the values from a to B to ask the sum of n numbers.
Idea: a simple line segment is updated and requires the use of the lazy mark. To put it bluntly, the lazy mark is used to update it, otherwise it will be put there.
#include
#include
#include
#include
#include using namespace std;const int inf=0x3f3f3f3f;const int maxn=1e5+10;typedef long long ll;#define mm(a) memset(a,0,sizeof(a))int num[maxn<<2],num1[maxn<<2];void pushup(int node){ num[node]=num[node<<1]+num[node<<1|1];}void pushdown(int node,int node1){ if(num1[node]){ int t=num1[node]; num1[node<<1]=t; num1[node<<1|1]=t; num[node<<1]=(node1-(node1>>1))*t; num[node<<1|1]=(node1>>1)*t; num1[node]=0; }}void buildtree(int le,int ri,int node){ if(le==ri){ num[node]=1; return ; } int t=(le+ri)>>1; buildtree(le,t,node<<1); buildtree(t+1,ri,node<<1|1); pushup(node);}void update(int l,int r,int add,int le,int ri,int node){ if(l<=le&&ri<=r){ num1[node]=add; num[node]=add*(ri-le+1); return ; } pushdown(node,ri-le+1); int t=(le+ri)>>1; if(l<=t) update(l,r,add,le,t,node<<1); if(r>t) update(l,r,add,t+1,ri,node<<1|1); pushup(node);}int main(){ int T,t=1,n,m; scanf("%d",&T); while(T--){ mm(num1); scanf("%d%d",&n,&m); int a,b,c; buildtree(1,n,1); while(m--){ scanf("%d%d%d",&a,&b,&c); update(a,b,c,1,n,1);// for(int i=1;i<=4*n;i++)// cout<