HDU 1540 POJ 2892 segment tree interval merging

Source: Internet
Author: User

Give n points, M operations, N points begin to link on one line

D operation to remove a point

Q Operation asks how many points can be connected to a point altogether

R Operation restores the last deleted point

Segment Tree processing interval Merging

Each interval is recorded with the longest continuous maximum and the right end continuous longest


#include "stdio.h" #include "string.h" struct node{int l,r,lx,rx,x;} Data[200010];int mark[50100];int Max (int a,int b) {if (a<b) return b;else return A;    void build (int l,int r,int k) {int mid;    Data[k].l=l;    Data[k].r=r;    data[k].lx=data[k].rx=data[k].x=data[k].r-data[k].l+1;    if (l==r) return;    Mid= (L+R)/2;    Build (L,MID,K*2); Build (mid+1,r,k*2+1);}    void Updata (int x,int k,int op) {int mid,ll,rr;        if (DATA[K].L==DATA[K].R) {data[k].lx=data[k].rx=data[k].x=op;    return;    } mid= (DATA[K].L+DATA[K].R)/2;    if (x<=mid) Updata (X,K*2,OP);    else if (x>mid) Updata (X,K*2+1,OP);    ll=data[k*2].r-data[k*2].l+1;    rr=data[k*2+1].r-data[k*2+1].l+1;    DATA[K].LX=DATA[K*2].LX;    if (DATA[K*2].LX==LL) data[k].lx+=data[k*2+1].lx;    Data[k].rx=data[k*2+1].rx;    if (DATA[K*2+1].RX==RR) Data[k].rx+=data[k*2].rx; Data[k].x=max (Max (data[k*2].x,data[k*2+1].x), DATA[K*2].RX+DATA[K*2+1].LX);}    int query (int x,int k) {int mid; if (DATA[K].L==DATA[K].R | | data[k].x==0 | |    data[k].x==data[k].r-data[k].l+1) return data[k].x;    Mid= (DATA[K].L+DATA[K].R)/2;        if (X<=mid) {if (DATA[K*2].R-X+1&LT;=DATA[K*2].RX) return query (x,k*2) +query (mid+1,k*2+1);    else return query (X,K*2);        } else {if (X-DATA[K*2+1].L+1&LT;=DATA[K*2+1].LX) return query (x,k*2+1) +query (mid,k*2);    else return query (x,k*2+1);    }}int Main () {int cnt,n,m,x;    Char ch[10];        while (scanf ("%d%d", &n,&m)!=eof) {build (1,n,1);        cnt=0;            while (m--) {scanf ("%s", ch);                if (ch[0]== ' D ') {scanf ("%d", &x);                Mark[cnt++]=x;            Updata (x,1,0);                } if (ch[0]== ' Q ') {scanf ("%d", &x);            printf ("%d\n", Query (x,1));                } if (ch[0]== ' R ') {cnt--; If (cnt<0) cnt=0;            else Updata (mark[cnt],1,1); }}} return 0;}




HDU 1540 POJ 2892 segment tree interval merging

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.