HDU 1698 segment number interval update and deferred update

Source: Internet
Author: User

Let's start with the difference between the interval update and the single-point update the main difference is that the search process needs to identify an interval the latter is a point.

Put the code on both.

void Updata (int i)//single point update {int l=stu[i].l;int r=stu[i].r;int mid= (l+r)/2;//dichotomy if (l==r&&r==x)//x as Target ID When the left and right nodes are the same, it is time to find this number {Stu[i].maxx=y;return;} if (L<=x&&x<=mid) Updata (i*2);//Find Else Updata (i*2+1) to the left;// Find Stu[i].maxx=max (Stu[i*2].maxx,stu[i*2+1].maxx) to the right;//update Max's data when backtracking}
void update (int l,int r,int count)//x,y represents the interval//interval update {if (l>=x&&r<=y) {Mapp[count].sum=z*mapp[count] to be updated. Len;mapp[count].flag=z;return;} Pushdown (count); int mid= (L+R)/2;    if (x<=mid) update (l,mid,count*2);      if (y>=mid+1) update (mid+1,r,count*2+1);      Mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum; }
Sometimes we don't need to update to the bottom when the interval is updated. The concept of introducing a deferred update here can be returned when the search is contained within the range we need.

So how do you ensure that the underlying data is updated the next time you ask? A flag flag is required here if the current flag is not 0, update and change the current flag



On the Code

#include <iostream> #define MAXN 111111using namespace Std;int n,q,x,y,z;struct stu{int L,r,sum,flag,len;}; Stu mapp[maxn*4];void Build (int l,int r,int count) {Mapp[count].l=l;mapp[count].r=r;mapp[count].flag=0;mapp[count]. Len=r-l+1;if (l==r) {Mapp[count].sum=1;mapp[count].flag=1;return;}      int mid= (L+R)/2;build (l,mid,count*2);      Build (mid+1,r,count*2+1);  Mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;          }void pushdown (int count) {if (Mapp[count].flag)//If there is a tag down update {mapp[count*2].flag=mapp[count].flag;        Mapp[count*2+1].flag=mapp[count].flag;          Mapp[count*2].sum=mapp[count].flag*mapp[count*2].len;        Mapp[count*2+1].sum=mapp[count].flag*mapp[count*2+1].len; mapp[count].flag=0;//update is marked to eliminate the}}//down update process void update (int l,int r,int count)//x,y represents the interval {if (l>=x&&r <=y) if (l==r) {Mapp[count].sum=z*mapp[count].len;mapp[count].flag=z;return;}      Pushdown (count); int mid= (L+R)/2;if (x<=mid) update (L,MID,COUNT*2); if (y>=mid+1) update (Mid+1,r,count*2+1); Mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;  }int Main () {Cin.sync_with_stdio (false); int T;cin>>t;int Casee=1;while (t--) {cin>>n;build (1,n,1); Cin>>q;while (q--) {cin>>x>> Y>>z;update (1,n,1);}          cout<< "Case" <<casee++<< ": the total value of the hook"; cout<<mapp[1].sum<< "."  <<endl;  }return 0;}

Interval update for the number of HDU 1698 segments and deferred updates

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.