Analysis: Application of Segment tree, interval modification, delay modification using delay tag.
#include <iostream>using namespace std; #define N 100010class segmenttree{private:struct node{int left,right; Left and right child node int sum; interval and int lazy; Delay tag};p ublic:void buildtree (int root,int l,int R), void change (int root,int l,int r,int c), int Query (int root);p rivate:n Ode m_tree[n<<2];}; int segmenttree::query (int root) {return m_tree[root].sum;} void Segmenttree::change (int root,int l,int r,int c) {int mid,x,y;x=m_tree[root].left;y=m_tree[root].right;if (l==x && r==y) {m_tree[root].lazy=1;m_tree[root].sum= (y-x+1) *c; Each point of the interval is modified to C, and is equal to the number of points multiplied by Creturn;} Mid= (x+y) >>1;if (m_tree[root].lazy==1)//The If processing deferred update {m_tree[root].lazy=0; Change (2*root+1,x,mid,m_tree[root].sum/(y-x+1)); m_tree[root].sum/(r-l+1) to get the value change (2*root+2,mid+1,y,m_tree[root].sum/(y-x+1)) of the individual points previously reserved;} if (l<=mid) change (2*ROOT+1,L,MID<R?MID:R,C); This two-sentence update contains three cases, the update interval on the left of mid, on the right, both sides have if (r>mid) change (2*root+2,mid+1>l?mid+1:l,r,c); m_tree[root].sum=m_tree [root*2+1].sum+m_tree[root*2+2].sum;} void Segmenttree::buildtree (int root,int l,int r) {int Mid;m_tree[root].left=l;m_tree[root].right=r;m_tree[root].sum =r-l+1; Number of points m_tree[root].lazy=1 at the beginning of the interval and equal to the interval; Set delay mark if (l==r) return; mid= (l+r) >>1; Buildtree (2*root+1,l,mid); Build left subtree Buildtree (2*root+2,mid+1,r); Build right subtree}segmenttree seg_tree;int main () {int t,n,q,x,y,z,i;scanf ("%d", &t), I=0;while (t--) {scanf ("%d", &n); Seg_tree. Buildtree (0,0,n-1); scanf ("%d", &q), while (q--) {scanf ("%d%d%d", &x,&y,&z); Seg_tree. Change (0,x-1,y-1,z);} printf ("Case%d:the total value of the hook is%d.\n", ++i,seg_tree. Query (0));} return 0;}
HDU ACM 1698 Just a hook-> segment tree + interval modification