Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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!

 
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
 
Source
思路:线段数区间合并;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9+,mod=;
int max(int x,int y,int z)
{
return max(x,max(y,z));
}
struct is
{
int l,r,len,lans,rans,sans;//这个区间的左端连续最大,右端,区间连续最大
}tree[N*];
void buildtree(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=(r-l+);
if(l==r)
return;
int mid=(l+r)>>;
buildtree(l,mid,pos<<);
buildtree(mid+,r,pos<<|);
}
void update(int point,int change,int pos)
{
if(tree[pos].l==point&&tree[pos].r==tree[pos].l)
{
tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=change;
return;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(point<=mid)
update(point,change,pos<<);
else
update(point,change,pos<<|);
tree[pos].lans=tree[pos<<].lans;
tree[pos].rans=tree[pos<<|].rans;
tree[pos].sans=max(tree[pos<<].sans,tree[pos<<|].sans,tree[pos<<].rans+tree[pos<<|].lans);
if(tree[pos<<].lans==tree[pos<<].r-tree[pos<<].l+)
tree[pos].lans+=tree[pos<<|].lans;
if(tree[pos<<|].rans==tree[pos<<|].r-tree[pos<<|].l+)
tree[pos].rans+=tree[pos<<].rans;
}
int query(int x,int pos)
{
if(tree[pos].l==x&&tree[pos].r==x||tree[pos].sans==||tree[pos].sans==tree[pos].r-tree[pos].l+)
{
return tree[pos].sans;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(x<=mid)
{
if(x>=tree[pos<<].r-tree[pos<<].rans+)
return query(x,pos<<)+query(mid+,pos<<|);
else
return query(x,pos<<);
}
else
{
if(x<=tree[pos<<|].l+tree[pos<<|].lans-)
return query(x,pos<<|)+query(mid,pos<<);
else
return query(x,pos<<|);
}
}
char a[];
int main()
{
int x,y,z,i,t;
int T;
while(~scanf("%d%d",&x,&y))
{
stack<int>s;
buildtree(,x,);
for(i=;i<y;i++)
{
scanf("%s",a);
if(a[]=='D')
{
scanf("%d",&z);
update(z,,);
s.push(z);
}
else if(a[]=='Q')
{
scanf("%d",&z);
printf("%d\n",query(z,));
}
else
{
update(s.top(),,);
s.pop();
}
}
}
return ;
}

最新文章

  1. 第一个shell脚本
  2. Java中MyEclipse快捷键整理
  3. SQL NOT EXISTS
  4. 每用户订阅上的所有者 SID 不存在 (异常来自 HRESULT:0x80040207)
  5. Ubuntu装机必备
  6. Html笔记(二)字体
  7. 线性链表的双向链表——java实现
  8. 【Maven】Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0
  9. WCF技术剖析之十五:数据契约代理(DataContractSurrogate)在序列化中的作用
  10. [TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
  11. Debian 安装 vmware-tools 手记
  12. django MVC模式 数据库的操作mysql
  13. 关于vue中如何配置echarts以及使用方法
  14. std unorder_map insert 和 emplace的区别
  15. 第七周博客作业 &lt;西北师范大学| 周安伟&gt;
  16. 20175316盛茂淞 2018-2019-2 《Java程序设计》第8周学习总结
  17. Armitage攻击winxp——P201421410029
  18. 关于session,cookie,Cache
  19. VS2017+mysql5.7 连接数据库生成实体
  20. golang 中处理大规模tcp socket网络连接的方法,相当于c语言的 poll 或 epoll

热门文章

  1. oninput事件(解决onkeyup无法监听到复制黏贴)
  2. python中的URL编码和解码
  3. 第三课补充01——set类型 sorted类型命令操作详解,redis管道及事务
  4. Linux测试UDP端口(nc)
  5. &lt;2014 08 28&gt; 大学学习小结
  6. Mysql学习笔记—视图
  7. Java并发—java.util.concurrent.locks包
  8. 用仿ActionScript的语法来编写html5——第六篇,TextField与输入框
  9. Django:学习笔记(2)——创建第一个应用
  10. jsp、freemarker、velocity对比