---恢复内容开始---

Tunnel Warfare


Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

Sample Output

1
0
2
4
模板题目,解释代码中
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int l,r;
int ll,rl,ml;
}a[MAXN*];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].ll=a[rt].rl=a[rt].ml=r-l+;
if(l==r) return ;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void merge(int rt)
{
//若左子树已满,则应与右子树左区间合并
a[rt].ll=a[rt<<].ll;
if(a[rt<<].ll==(a[rt<<].r-a[rt<<].l+))
{
a[rt].ll+=a[(rt<<)|].ll;
} //若右子树已满,则应与左子树的有区间合并
a[rt].rl=a[(rt<<)|].rl;
if(a[(rt<<)|].rl==(a[(rt<<)|].r-a[(rt<<)|].l+))
{
a[rt].rl+=a[rt<<].rl;
}
//该子树的最大连续长度为 左或右子树连续长度的最大者 或者 将左子树右区间与右子树左区间合并的长度
a[rt].ml=max(max(a[rt<<].ml,a[(rt<<)|].ml),a[rt<<].rl+a[(rt<<)|].ll);
} void update(int rt,int pos,int val)
{
if(a[rt].l==a[rt].r)
{
if(val) a[rt].ll=a[rt].rl=a[rt].ml=;
else a[rt].ll=a[rt].rl=a[rt].ml=;
return ;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid) update(rt<<,pos,val);
else update((rt<<)|,pos,val);
merge(rt);
} int query(int rt,int pos)
{
if(a[rt].l==a[rt].r||a[rt].ml==||a[rt].ml==a[rt].r-a[rt].l+)//到达叶子节点或者该节点已满或空,那么不必向下走了
{
return a[rt].ml;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid)//在左子树中
{
if(pos>=a[rt<<].r-a[rt<<].rl+)//若在左子树的右区间
{
return query(rt<<,pos)+query((rt<<)|,mid+);//则需要查询右子树
}
else
{
return query(rt<<,pos);
} }
else
{
if(pos<=a[(rt<<)|].l+a[(rt<<)|].ll-)//若在右子树的左区间
{
return query((rt<<)|,pos)+query(rt<<,mid);//则需要查询左子树
}
else
{
return query((rt<<)|,pos);
}
}
} int stack[MAXN];
int top; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int pos;
top=;
build(,,n);
while(m--)
{
scanf("%*c");
char op;scanf("%c",&op);
if(op=='D')
{
scanf("%d",&pos);
stack[top++]=pos;
update(,pos,);
}
else if(op=='Q')
{
scanf("%d",&pos);
printf("%d\n",query(,pos));
}
else
{
pos=stack[--top];
update(,pos,);
}
}
}
}
 
 
 

---恢复内容结束---

HDU1540(线段树统计连续长度)的更多相关文章

  1. hdu-1540线段树刷题

    title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...

  2. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  3. HDU 6070 (线段树)(统计颜色)

    HDU 6070 Partition Problem : 给一段长度为n的序列,要求找出一段区间,使得这段区间的数字种类除以区间长度最小.输出最后的答案即可.(n <= 60000)(9s时限) ...

  4. Tunnel Warfare(hdu1540 线段树)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. Tunnel Warfare(HDU1540+线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...

  6. hdu1540线段树

    https://vjudge.net/contest/66989#problem/I #include<iostream> #include<cstdio> #include& ...

  7. Tunnel Warfare--- hdu1540 线段树求连续子区间

    题目链接 题意:有n个村庄,编号分别为1-n:由于战争会破坏村庄,但是我们也会修复: D x代表村庄x被破坏: Q x是求与x相连的有几个没有被破坏: R 是修复最后一次被破坏的村庄: 接下来有m个操 ...

  8. [题解](线段树最大连续子段和)POJ_3667_Hotel

    题意:1.求一个最靠左的长x的区间全部为0,并修改为1,输出这个区间的左端点 2.修改一个区间为0 实际上是维护最大连续子段和,原来也写过 大概需要维护一个左/右最大子段和,当前这段最大子段长,再维护 ...

  9. hdu1540线段树连续区间

    模板题>.<当初学了一波又忘了 #include<map> #include<set> #include<cmath> #include<queu ...

随机推荐

  1. 14-redis运维常用命令

    一:运维常用的server端命令    TIME 查看时间戳与微秒数 DBSIZE 查看当前库中的key数量 BGREWRITEAOF 后台进程重写AOF BGSAVE       后台保存rdb快照 ...

  2. Android 繪圖白板元件,有畫筆和板擦的功能 (转)

    package com.example.drawboard; import java.util.ArrayList; import java.util.List; import android.con ...

  3. 九度OJ 1026:又一版 A+B (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11412 解决:3086 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < ...

  4. vscode设置默认shell 快速到行

    vscode设置默认shell - CSDN博客 https://blog.csdn.net/butterfly5211314/article/details/78944805 在文件 -> 首 ...

  5. sqlldr 用法

    转自:http://blog.chinaunix.net/uid-23622436-id-2394093.html 在 Oracle 数据库中,我们通常在不同数据库的表间记录进行复制或迁移时会用以下几 ...

  6. Kibana + ElasticSearch

    上面一张介绍了ElasticSearch的安装和简单用法. 现在应该都知道ElasticSearch是用来做全文搜索的,那今天我就简单介绍下Kibana. 它是专门用来查看ElasticSearch内 ...

  7. php 获取优酷视频的真实地址(2014.6月新算法)

    上个礼拜发现优酷改版了,各种过滤优酷广告的插件都失效了,于是我百度了一下(谷歌也不能用了)发现优酷改算法了,在ckplayer论坛发现有人在6月25号发了个php 的优酷代理文件,下载下来发现,能用但 ...

  8. Git如何强制拉取一个远程分支到本地分支(转载)

    有时候,我们在使用git pull指令想把一个远程分支拉取到本地分支的时候,老是会拉取失败,这一般是因为某种原因,本地分支和远程分支的内容差异无法被git成功识别出来,所以git pull指令什么都不 ...

  9. NiFi汉化

    ①在源文件中的 source-nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src中修 ...

  10. HTML5模拟衣服撕扯效果

    在线演示 本地下载