HDU 1540——Tunnel Warfare

来源:互联网 发布:python画一朵玫瑰花 编辑:程序博客网 时间:2024/05/18 17:56

Tunnel Warfare

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


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!


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
 

Source

POJ Monthly 


题意:n个村子每个村子和编号相邻的村子连接。

3个操作:

D x 摧毁第x个村子

Q x 询问x村子直接和间接与多少个村子相连(自己算一个)

R 重建最后一个村子


分析:

线段树+区间维护,把未被摧毁的村子标记为1,摧毁标记为0,即求包含x的最长1序列


代码:

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<stack>#define INF 0x3f3f3f3f#define EXP 0.00000001#define MOD 1e9+7#define MAXN 50005#define ltree 2*id,ll,mid#define rtree 2*id+1,mid+1,rr#define FO(i,n,m) for(int i=n;i<=m;++i)#define mem(a) memset(a,0,sizeof(a))typedef long long LL;using namespace std;struct node{    int ml,mr,ms; //分别储存最长左端长度,最长右端长度,以及最长总长度    int len;}tree[MAXN*4];int n,m;void Pushup(int id){    int lid=2*id,rid=2*id+1;    tree[id].ml=tree[lid].ml;    if(tree[lid].ml==tree[lid].len)        tree[id].ml+=tree[rid].ml;    tree[id].mr=tree[rid].mr;    if(tree[rid].mr==tree[rid].len)        tree[id].mr+=tree[lid].mr;    tree[id].ms=max(tree[lid].mr+tree[rid].ml,max(tree[lid].ms,tree[rid].ms));    //最长的一段是中间合并区间,左边最长区间,右边最长区间中最大的那一个}void Build(int id,int ll,int rr){    tree[id].len=rr-ll+1;    if(ll==rr)    {        tree[id].ml=1;        tree[id].mr=1;        tree[id].ms=1;        return;    }    int mid=(ll+rr)/2;    Build(ltree);    Build(rtree);    Pushup(id);}void Update(int id,int ll,int rr,int x,int sta){    if(ll==rr)    {        tree[id].ml=sta;        tree[id].mr=sta;        tree[id].ms=sta;        return;    }    int mid=(ll+rr)/2;    if(x<=mid)        Update(ltree,x,sta);    if(x>mid)        Update(rtree,x,sta);    Pushup(id);}int Query(int id,int ll,int rr,int a){    if(ll==rr||tree[id].ms==0||tree[id].ms==tree[id].len)        return tree[id].ms;    int mid=(ll+rr)/2;    if(a<=mid)    {        if(mid-tree[2*id].mr+1<=a)            return Query(ltree,a)+Query(rtree,mid+1);        else            return Query(ltree,a);    }    if(a>mid)    {        if(tree[2*id+1].ml+mid>=a)            return Query(ltree,mid)+Query(rtree,a);        else            return Query(rtree,a);    }}int main(){    while(~scanf("%d%d",&n,&m))    {        stack<int>re;  //先进后出用栈储存        Build(1,1,n);        while(m--)        {            char s;            int a;            getchar();            scanf("%c",&s);            if(s=='D')            {                scanf("%d",&a);                re.push(a);                Update(1,1,n,a,0);            }            if(s=='R')            {                a=re.top();                re.pop();                Update(1,1,n,a,1);            }            if(s=='Q')            {                scanf("%d",&a);                printf("%d\n",Query(1,1,n,a));            }        }    }    return 0;}