poj hdu Tunnel Warfare

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2814 Accepted: 1044

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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Hint

An illustration of the sample input:

      OOOOOOO

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

Source

 

 

用SET做,这个程序是另外大牛的,实践是422MS,用C++编译

 

#include <cstdio>

#include <set>

using namespace std;

int stak[50000];

int main()

{

int n,m,pos,count;

char cmd;

set<int> mp;

scanf("%d%d",&n,&m);

mp.insert(0);

mp.insert(n+1);

count=0;

while(m--)

{

      scanf(" %c",&cmd);

 if(cmd=='D')

 {

 scanf("%d",&pos);

 mp.insert(pos);

 stak[count++]=pos;

 }

 if(cmd=='Q')

 {

 scanf("%d",&pos);

 

 if(*mp.lower_bound(pos)==pos)

 printf("0/n");

 else

 {

 set<int>::iterator STI;

 STI = mp.lower_bound(pos);

 printf("%d/n",*STI-*(--STI)-1);

 }

 

 }

 if(cmd=='R')

 {

 mp.erase(mp.find(stak[--count]));

 }

 

}

return 0;

}

 

用线段树是200多MS,YEAH?!

#include<stdio.h>

#include<string.h>

#define LL(x)  (x<<1)

#define RR(x)  (x<<1|1)

int stack[50010],n,m;

 

struct Seg_Tree

{

    int l,r;

    int cval,lval,rval;

    void doit(int st)  {  cval=lval=rval=( st==1? 0 : dis() ) ;}

    int dis() { return (r-l+1) ;}

    int mid() { return (l+r)/2 ;}

}tree[50010*3];

 

int max(int a,int b)

{

    if(a>b)   return a;

    return b;

}

 

void build(int left,int right,int idx)

{

     tree[idx].l=left;

     tree[idx].r=right;

     tree[idx].doit(0);

     if(left==right)   return;

     int mid=tree[idx].mid();

     build(left,mid,LL(idx));

     build(mid+1,right,RR(idx));

}

 

void update(int x,int st,int idx)

{

     if(tree[idx].l==tree[idx].r)

     {

         tree[idx].doit(st);

         return ;

     }

     int mid=tree[idx].mid();

     if(x<=mid)  update(x,st,LL(idx));

     else  update(x,st,RR(idx));

     tree[idx].cval=max(tree[LL(idx)].rval+tree[RR(idx)].lval,

                        max(tree[LL(idx)].cval,tree[RR(idx)].cval));

     tree[idx].lval=tree[LL(idx)].lval;

     tree[idx].rval=tree[RR(idx)].rval;

     if(tree[LL(idx)].cval==tree[LL(idx)].dis()) tree[idx].lval+=tree[RR(idx)].lval;

     if(tree[RR(idx)].cval==tree[RR(idx)].dis()) tree[idx].rval+=tree[LL(idx)].rval;

}

 

int query(int x,int idx)

{

     if(tree[idx].l==tree[idx].r || tree[idx].cval==0 || tree[idx].cval==tree[idx].dis() )

     {

         return tree[idx].cval;

     }

     int mid=tree[idx].mid();

     if(x<=mid)

     {

         if(x>tree[LL(idx)].r-tree[LL(idx)].rval)

             return query(x,LL(idx))+query(tree[RR(idx)].l,RR(idx));

         else

             return query(x,LL(idx));

     }

     else

     {

         if(x<tree[RR(idx)].l+tree[RR(idx)].lval)

             return query(tree[LL(idx)].r,LL(idx))+query(x,RR(idx));

         else

             return query(x,RR(idx));

     }

}

 

int main()

{

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        memset(tree,0,sizeof(tree));

        memset(stack,0,sizeof(stack));

        int num=0;

        build(1,n,1);

        for(int i=1;i<=m;i++)

        {

            char ch[2];

            scanf("%s",&ch);

            if(ch[0]=='D')

            {

                int x;

                scanf("%d",&x);

                update(x,1,1);

                stack[num++]=x;

            }

            else

            if(ch[0]=='Q')

            {

                int x;

                scanf("%d",&x);

                printf("%d/n",query(x,1));

            }

            else

            {

                if(num==0)  continue;

                update(stack[--num],0,1);

            }

        }

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值