Tunnel Warfare

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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>

using namespace std;
int m,n;
int D[50010];

struct node{
    int l,r;
    int ll,rl,ml;
    //左边开始连续的最大长度和右边开始最大的连续长度
    //以及这个区间最大连续长度
}tree[50010<<2];
void Build(int l,int r,int rt){
    tree[rt].l = l;
    tree[rt].r = r;
    tree[rt].ll =tree[rt].rl =tree[rt].ml = r-l+1;
    if(l==r)    return;
    int m=(l+r)/2;
    Build(l,m,rt*2);
    Build(m+1,r,rt*2+1);

}
int Query(int rt,int x){

    if(tree[rt].l==tree[rt].r||tree[rt].ml==0||tree[rt].ml==tree[rt].r-tree[rt].l+1){

        return tree[rt].ml;
    }
    int m=(tree[rt].l+tree[rt].r)/2;
    int res=0;
    if(x<=m)
    {
        if(x>=tree[rt<<1].r-tree[rt<<1].rl+1)
           return Query(rt<<1,x)+Query((rt<<1)|1,m+1);
        else
            return Query(rt<<1,x);
    }
    else
    {
        if(x<=tree[(rt<<1)|1].l+tree[(rt<<1)|1].ll-1)
           return Query((rt<<1)|1,x)+Query(rt<<1,m);
        else
            return Query((rt<<1)|1,x);
    }

}
void Updata(int rt,int L,int C){
   if(tree[rt].l==tree[rt].r)
    {
        tree[rt].ll=tree[rt].rl=tree[rt].ml=C;
        return;
    }
    int m=(tree[rt].l+tree[rt].r)/2;
    if(L<=m) Updata(rt<<1,L,C);
    else Updata(rt<<1|1,L,C);

    tree[rt].ll=tree[rt<<1].ll;
    tree[rt].rl=tree[(rt<<1)|1].rl;

    tree[rt].ml=max(tree[rt<<1].ml,tree[(rt<<1)|1].ml);
    tree[rt].ml=max(tree[rt].ml,tree[rt<<1].rl+tree[(rt<<1)|1].ll);

    if(tree[rt<<1].ll==tree[rt<<1].r-tree[rt<<1].l+1)
        tree[rt].ll+=tree[(rt<<1)|1].ll;
    if(tree[(rt<<1)|1].rl==tree[(rt<<1)|1].r-tree[(rt<<1)|1].l+1)
        tree[rt].rl+=tree[rt<<1].rl;
}
int main()
{
        while(~scanf("%d%d",&n,&m))
    {
        Build(1,n,1);
        char c[2];
        int x,d=0;
        while(m--){
            scanf("%s",c);
            if(c[0]=='D'){
                scanf("%d",&x);
                Updata(1,x,0);
                D[d++]=x;
            }
            if(c[0]=='Q'){
                scanf("%d",&x);
                cout << Query(1,x) << endl;
            }
            if(c[0]=='R'){
               
                    
                    Updata(1,D[--d],1);
                
                    
            }
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

05-12 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务