Line Segment tree + Interval Update ----- HDU 4902: Nice boat

Source: Internet
Author: User
Nice boat

Time Limit: 30000/15000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 968 accepted submission (s): 441


Problem descriptionthere is an old country and the king fell in love with a dedevil. the dedevil always asks the king to do some crazy things. although the King used to be wise and beloved by his people. now he is just like a boy in love and can't refuse any request from the dedevil. also, this dedevil is looking like a very cute Loli.

Let us continue our story, z * P (actually you) defeat the 'mengmengda 'party's leader, and the 'mengmengda' party dissolved. z * p becomes the most famous guy among the princess's knight party.

One day, the people in the party find that z * P has died. as what he has done in the past, people just say 'Oh, what a nice boat 'and don't care about why he died.

Since then, since people died but no one knows why and everyone is fine about that. Meanwhile, the dedevil sends her knight to challenge you with algorithm contest.

There is a hard data structure Problem in the contest:

There are n numbers A_1, A_2 ,..., a_n on a line, everytime you can change every number in a segment [L, R] into a number x (type 1), or change every number a_ I in a segment [L, r] Which is bigger than X to gcd (a_ I, x) (Type 2 ).

You shoshould output the final sequence.

 

Inputthe first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a integers n.
The next line contains N integers A_1, A_2,..., a_n separated by a single space.
The next line contains an integer Q, denoting the number of the operations.
The next Q line contains 4 integers t, L, R, X. T Denotes the operation type.

T <= 2, N, q <= 100000
A_ I, x> = 0
A_ I, X is in the range of int32 (C ++)
 

 

Outputfor each test case, output a line with N integers separated by a single space representing the final sequence.
Please output a single more space after end of the sequence 

 

Sample input11016807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 3 6 101 4 8 742430422 3 4 165317291 1 8 14748331692 7 9 11315709332 3 7 15057953352 4 10 1019292671 2 8 16243791492 6 7 21100106722 2 5 937186357

 

Sample output16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149

 

Authorwjmzbmr

 

Source2014 multi-university training contest 4

 

[Topic]

Here is a string of numbers. There are two operations. 1 indicates converting the number in a range to X, 2 indicates that if the number in a range is greater than X, it is changed to the minimum public approx. Of this number and X; otherwise, it remains unchanged. The number of changes after the final output.

 

[Question analysis]

This question is different from other line segment trees. This question is output only after all processing is completed.
We add a flag to each node to mark whether the numbers in the interval are the same. If the interval is the same number, we can perform batch processing, which will greatly reduce the time complexity.
Use another temp to store the Val of the node, and then pass the Temp value layer by layer in the Pushdown function.
Here, temp not only plays the role of whether the node has been updated down (equivalent to lazy), but also plays the role of recording the value to be updated on the subnode.
Temp is changed only when the Val value is changed.

 

//Memory   Time// 5376K     651MS#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<iomanip>#include<string>#include<climits>#include<cmath>#define MAX 100100#define LL long longusing namespace std;int n,m;int  ans;int num[MAX];struct Tree{    int l,r;    bool flag;    int val,temp;};Tree tree[MAX<<2];int gcd(int x,int y){    return y?gcd(y,x%y):x;}void pushup(int x){    int tmp=x<<1;    tree[x].val=max(tree[tmp].val,tree[tmp+1].val);    tree[x].flag=(tree[tmp].val==tree[tmp+1].val&&tree[tmp].flag&&tree[tmp+1].flag);}void pushdown(int x){    if(tree[x].temp==-1)return;    int tmp=x<<1;    int mid=(tree[x].l+tree[x].r)>>1;    tree[tmp].val=tree[tmp+1].val=tree[tmp].temp=tree[tmp+1].temp=tree[x].temp;    tree[x].temp=-1;}void build(int l,int r,int x){    tree[x].flag=0;    tree[x].temp=-1;    tree[x].l=l,tree[x].r=r;    if(l==r)    {        scanf("%d",&tree[x].val);        tree[x].flag=1;        return;    }    int tmp=x<<1;    int mid=(l+r)>>1;    build(l,mid,tmp);    build(mid+1,r,tmp+1);    pushup(x);}void update(int l,int r,int num,int x){    if(r<tree[x].l||l>tree[x].r)return;    if(l<=tree[x].l&&r>=tree[x].r)    {        tree[x].flag=1;        tree[x].val=num;        tree[x].temp=num;        return;    }    pushdown(x);    int tmp=x<<1;    int mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid)        update(l,r,num,tmp);    else if(l>mid)        update(l,r,num,tmp+1);    else    {        update(l,mid,num,tmp);        update(mid+1,r,num,tmp+1);    }    pushup(x);}void change(int l,int r,int num,int x){    if(r<tree[x].l||l>tree[x].r)return;    if(tree[x].flag&&tree[x].val<=num)return;    if(l<=tree[x].l&&r>=tree[x].r&&tree[x].flag)    {        tree[x].val=gcd(tree[x].val,num);        tree[x].temp=tree[x].val;        return;    }    pushdown(x);    int tmp=x<<1;    int mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid)        change(l,r,num,tmp);    else if(l>mid)        change(l,r,num,tmp+1);    else    {        change(l,mid,num,tmp);        change(mid+1,r,num,tmp+1);    }    pushup(x);}void query(int l,int r,int k,int x){    if(k<tree[x].l||k>tree[x].r)return;    if(tree[x].flag)    {        ans=tree[x].val;        return;    }    pushdown(x);    int tmp=x<<1;    int mid=(tree[x].l+tree[x].r)>>1;    if(k<=mid)        query(l,mid,k,tmp);    else        query(mid+1,r,k,tmp+1);}int main(){    int T;    int t,l,r,num;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        build(1,n,1);        scanf("%d",&m);        while(m--)        {            scanf("%d %d %d %d",&t,&l,&r,&num);            if(t==1)                update(l,r,num,1);            else                change(l,r,num,1);        }        for(int i=1;i<=n;i++)        {            ans=0;            query(1,n,i,1);            printf("%d ",ans);        }        puts("");    }    return 0;}

  

 

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.