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

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. mnesia的脏读和事物读的测试

    在mnesia中,有脏读脏写等以及事物读写,它们的差异通过测试不难发现: 代码如下: -module(mnesia_read_test). -compile(export_all). -record( ...

  2. Quart 2D 绘制图形简单总结(转)

    0  CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGConte ...

  3. Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Bessie is ...

  4. 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题

    https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.3.md 9.3 锁和 sync 包 在一些复杂的程序中,通常通 ...

  5. 基于Flume的美团日志收集系统 架构和设计 改进和优化

    3种解决办法 https://tech.meituan.com/mt-log-system-arch.html 基于Flume的美团日志收集系统(一)架构和设计 - https://tech.meit ...

  6. Hadoop实战-Flume之Source multiplexing(十五)

    a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...

  7. 洛谷3243 [HNOI2015]菜肴制作

    题目戳这里 Solution 错误的想法:正向建图,然后从入度为0的点选出最小u的开始输出,然后找出u连接的点v,并把v的度数减一,再次把入度为0的点加入小根堆,这样显然有错,因为只能局部保证最小,后 ...

  8. (图解)Description Resource Path Location Type Java compiler level does not match the version of

    Description Resource Path Location Type Java compiler level does not match the version of project 编译 ...

  9. CALL FUNCTION 'BAPI_PO_CREATE1' 相关报错

    *&---------------------------------------------------------------------**& Report  ZQJ06*&am ...

  10. PYTHON调用C接口(基于Ctypes)实现stein算法最大公约数的计算

    相关环境配置 mingw,选择相应的32位.64位的版本,主要用于编译动态链接库dll文件,可用vs替代,这里我选择轻量级的mingw windows64位地址:https://sourceforge ...