hdu 1540 Tunnel Warfare

来源:互联网 发布:js模块化是什么意思 编辑:程序博客网 时间:2024/05/16 18:43

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2393    Accepted Submission(s): 883


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

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

Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

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

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
 

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
 

线段树的题目,刚开始看的时候一点思路都没有,然后各种网搜思路,表示还是不懂,不过隔壁同学用了一种巧妙的方法解决了!

不一定从上往下找,从小往上也是一种不错的选择;

由题目我们可以知道如果我们要找这个点的连通,只要从左找,或从右找!这个时候我么只要判断中间这个点是否是连通的就行了!

看代码!

#include <cstdio>#include <cstdlib>#include <cstring>#include <stack>#include <cstdlib>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;struct node{    int l,r;    bool f;//对于每个点,我们只记录连通还是不连通!或是这一段是否全连通,有一个不通就不通}sum[50000*4];int map[50000];void build(int l,int r,int rt){    sum[rt].l=l;    sum[rt].r=r;    sum[rt].f=true;    if(l==r)    {        map[l]=rt;        return ;    }    int m=(r+l)>>1;    build(lson);    build(rson);}void destory(int rt){    if(rt>0)    {        sum[rt].f=false;        destory(rt>>1);    }}void rebuild(int rt){    while(rt>0)    {        sum[rt].f=true;        if(rt%2==0&&sum[rt+1].f==true)        {            rt>>=1;        }        else if(rt%2!=0&&sum[rt-1].f==true)        {            rt>>=1;        }        else        {            break;        }    }}int search(int p,int &l,int &r,int rt){    if(sum[map[p]].f==false)    {        return 0;    }    if(sum[rt].f==true)    {        l=sum[rt].l;        r=sum[rt].r;        return r-l+1;    }    int m=(l+r)>>1;    if(sum[map[m]].f==true&&sum[map[m+1]].f==true)    {        int x1=l;        int y1=m;        int x2=m+1;        int y2=r;        search(p,x1,y1,rt<<1);        search(p,x2,y2,rt<<1|1);        if(x2==y1+1)        {            l=x1;            r=y2;        }        else if(p<=y1)        {            l=x1;            r=y1;        }        else if(p>=x2)        {            l=x2;            r=y2;        }        return r-l+1;    }    else if(p<=m)    {        search(p,l,m,rt<<1);        r=m;        return r-l+1;    }    else if(p>m)    {        int x1=m+1;        search(p,x1,r,rt<<1|1);        l=x1;        return r-l+1;    }}int main(){    int n,k;    char ch;    int x;    while(scanf("%d%d",&n,&k)!=EOF)    {        build(1,n,1);        stack<int> v;        while(k--)        {            getchar();            scanf("%c",&ch);            if(ch=='D')            {                scanf("%d",&x);                v.push(x);                destory(map[x]);            }            else if(ch=='R'&&!v.empty())            {                x=v.top();                v.pop();                rebuild(map[x]);            }            else            {                scanf("%d",&x);                int le=1;                int ri=n;                printf("%d\n",search(x,le,ri,1));            }        }    }}



 

原创粉丝点击