HDU ACM 1698 Just a hook-> segment tree + interval modification

Source: Internet
Author: User

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&LT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.