Poj 2892 --- Tunnel Warfare (Single Point update and interval merge of line tree), poj2892 --- tunnel

Source: Internet
Author: User

Poj 2892 --- Tunnel Warfare (Single Point update and interval merge of line tree), poj2892 --- tunnel

Question Link

 

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. generally speaking, ages connected by tunnels lay in a line. cannot the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the versions and destroyed the parts of tunnels in them. the Eighth Route Army commanders requested the latest connection state of the tunnels and ages. if some versions are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integersNAndM(N,M≤ 50,000) indicating the number of versions and events. Each of the nextMLines describes an event.

There are three different events described in different format shown below:

 

Output

Output the answer to each of the Army commanders 'request in order on a separate line.

Sample Input

7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024

Hint

An authentication of the sample input:

      OOOOOOO
D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO

Source

POJ Monthly -- 2006.07.30, updog... n they are connected one by one according to the serial number, and now there are m operations, there are the following operations: 1. D x indicates to destroy village x. 2. Q x indicates querying the number of villages (including village x) that village x can reach ). 3. R indicates the repair of the last destroyed village. Each query outputs a value. Idea: The line segment tree is updated at a single point, and the interval is merged. The destroyed village number is stored in the stack. The Code is as follows:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <stack>using namespace std;const int maxn=50005;stack<int> s;struct Node{    int l,r,m;}tr[4*maxn];void build(int i,int l,int r){    tr[i].l=tr[i].r=tr[i].m=r-l+1;    if(l==r) return;    int mid=(l+r)/2;    build(2*i,l,mid);    build(2*i+1,mid+1,r);}void update(int i,int l,int r,int x,int y){    if(l==r)    {        tr[i].l=tr[i].r=tr[i].m=y;        return;    }    int mid=(l+r)/2;    if(x<=mid) update(2*i,l,mid,x,y);    else update(2*i+1,mid+1,r,x,y);    if(tr[2*i].m==mid-l+1) tr[i].l=tr[2*i].m+tr[2*i+1].l;    else tr[i].l=tr[2*i].l;    if(tr[2*i+1].m==r-mid) tr[i].r=tr[2*i+1].m+tr[2*i].r;    else tr[i].r=tr[2*i+1].r;    tr[i].m=max(max(tr[2*i].m,tr[2*i+1].m),tr[2*i].r+tr[2*i+1].l);}int query(int i,int l,int r,int x){    int sum=0;    if(l==r) return tr[i].m;    if(r-l+1==tr[i].m) return tr[i].m;    int mid=(l+r)/2;    if(x<=mid){        if(mid-tr[2*i].r+1<=x)        return tr[2*i].r+tr[2*i+1].l;        else return query(2*i,l,mid,x);    }    else {        if(tr[2*i+1].l+mid>=x)        return tr[2*i].r+tr[2*i+1].l;        else return query(2*i+1,mid+1,r,x);    }}int main(){    int n,m;    while(scanf("%d",&n)!=EOF)    {        scanf("%d",&m);        build(1,1,n);        int x;        char str[5];        while(!s.empty()) s.pop();        while(m--)        {            scanf("%s",str);            if(str[0]=='D')            {                scanf("%d",&x);                s.push(x);                update(1,1,n,x,0);            }            else if(str[0]=='Q')            {                scanf("%d",&x);                printf("%d\n",query(1,1,n,x));            }            else            {                update(1,1,n,s.top(),1);                s.pop();            }        }    }    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.