Segment Tree interval update + interval merging

Source: Internet
Author: User
Tags printf

Topic links

One look is the problem of merging the segment tree interval.

Two operations, one with the new, one query

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include < iostream> #include <algorithm> #include <stack> #include <queue> #include <string> #include
<vector> #include <set> #include <map> #include <string> using namespace std;
typedef long Long LL;
const int inf=0x3f3f3f3f;
Char str[1000010];
    struct Node1 {int l,r,len,lazy;
    int LB,RB,MB;
        void Merge (int key) {if (key==1) {lb=rb=mb=0;}
    else {Lb=rb=mb=len;}
}}node[1000010*4];
    void push_up (int n) {int lson=n*2,rson=n*2+1;
    node[n].lb=node[lson].lb;
    NODE[N].RB=NODE[RSON].RB;
    if (Node[lson].lb==node[lson].len) node[n].lb+=node[rson].lb;
    if (Node[rson].rb==node[rson].len) node[n].rb+=node[lson].rb;
Node[n].mb=max (Max (NODE[RSON].MB,NODE[LSON].MB), node[lson].rb+node[rson].lb);
        } void Push_down (int n) {if (node[n].lazy) {node[n*2].lazy=node[n*2+1].lazy=node[n].lazy; node[N*2]. Merge (Node[n].lazy); node[n*2+1].
        Merge (Node[n].lazy);
    node[n].lazy=0;
    }} void Build (int l,int r,int n) {node[n].l=l,node[n].r=r,node[n].len=r-l+1,node[n].lazy=0;
        if (l==r) {if (str[l]== ' A ') node[n].lb=node[n].rb=node[n].mb=0;
        else if (str[l]== ' B ') node[n].lb=node[n].rb=node[n].mb=1;
    Return
    } int mid= (L+R)/2;
    Build (L,MID,N*2);
    Build (mid+1,r,n*2+1);
PUSH_UP (n); } void Update (int p,int q,int n,int k) {if (P&LT;=NODE[N].L&AMP;&AMP;Q&GT;=NODE[N].R) {node[n].lazy=k;node[n].
    Merge (k); return;}
    Push_down (n);
    int mid= (NODE[N].L+NODE[N].R)/2;
    if (p>=mid+1) update (P,Q,N*2+1,K);
    else if (q<=mid) update (P,Q,N*2,K);
    Else{update (p,q,n*2,k); update (p,q,n*2+1,k);}
PUSH_UP (n);
    } node1 merge_query (Node1 p1,node1 p2) {node1 ans;
    Ans.len=p1.len+p2.len;
    ANS.LB=P1.LB;ANS.RB=P2.RB;
    if (P1.lb==p1.len) ans.lb+=p2.lb;
    if (P2.rb==p2.len) ans.rb+=p1.rb; Ans.mb=max (Max (P1.MB,P2.MB), P1.RB+P2.Lb);
return ans;
    } node1 Query (int p,int q,int N) {if (NODE[N].L&GT;=P&AMP;&AMP;Q&GT;=NODE[N].R) return node[n];
    Push_down (n);
    int mid= (NODE[N].L+NODE[N].R)/2;
    if (p>=mid+1) return query (p,q,n*2+1);
    else if (q<=mid) return query (P,Q,N*2);
else{return merge_query (Query (p,q,n*2), query (p,q,n*2+1));}}
    int main () {int t;scanf ("%d", &t);
        for (int ca=1;ca<=t;ca++) {printf ("Case #%d:\n", CA); int n,m;
        scanf ("%d%d", &n,&m); scanf ("%s", str+1);
        Build (1,n,1);
            while (m--) {int op,l,r,w;scanf ("%d%d%d", &op,&l,&r);
            if (op==1) {scanf ("%d", &w); update (L,R,1,W);}
                else {Node1 ans=query (l,r,1);
            printf ("%d\n", Max (Ans.lb,max (ANS.RB,ANS.MB)));
}}} 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.