hdu1540 Tunnel Warfare 线段树中等难题
来源:互联网 发布:柬埔寨网络博客犯法吗 编辑:程序博客网 时间:2024/06/07 22:57
Tunnel Warfare
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 2
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.
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
/*一路参考人家的代码 经历无数障碍终于AC了 以后要常看 主要的对我有启发的应该在query查找*/
#include<stdio.h>
struct haha
{
int left;
int right;
int l_1;//从左边开始的1的连续长度
int r_1;//从右边开始的1的连续长度
int max_1;//最大的连续1的长度
}node[50000*4];
#include<stdio.h>
struct haha
{
int left;
int right;
int l_1;//从左边开始的1的连续长度
int r_1;//从右边开始的1的连续长度
int max_1;//最大的连续1的长度
}node[50000*4];
int max(int x,int y)
{
if(x>y) return x;
else return y;
}
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].max_1=node[nd].l_1=node[nd].r_1=right-left+1;
if(left==right)
return;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}
void update(int pos,int flag,int nd)
{
int mid,l_len,r_len;
if(node[nd].left==pos&&node[nd].right==pos)
{
if(flag)
node[nd].l_1=node[nd].r_1=node[nd].max_1=1;
else node[nd].l_1=node[nd].r_1=node[nd].max_1=0;
return;
}
l_len=node[nd*2].right-node[nd*2].left+1;
r_len=node[nd*2+1].right-node[nd*2+1].left+1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid) update(pos,flag,nd*2);
else if(pos>mid) update(pos,flag,nd*2+1);
node[nd].max_1=max((node[nd*2].r_1+node[nd*2+1].l_1),max(node[nd*2].max_1,node[nd*2+1].max_1));
node[nd].l_1=node[nd*2].l_1;
if(node[nd*2].l_1==l_len) node[nd].l_1+=node[nd*2+1].l_1;
node[nd].r_1=node[nd*2+1].r_1;
if(node[nd*2+1].r_1==r_len) node[nd].r_1+=node[nd*2].r_1;
}
int query(int pos,int nd)
{
int mid,len;
len=node[nd].right-node[nd].left+1;
if(node[nd].left==node[nd].right||node[nd].max_1==0||node[nd].max_1==len)//node[nd].max_1==len 查找到某段都是连续的 就return
return node[nd].max_1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid)
{
if(pos>mid-node[nd*2].r_1)
/*例如1-10中 2-10均为1 pos=4 那么查找4的时候 要进入二个分支 1个是左分支
1个是右分支 在右分支中查找最左边的点 */
return query(pos,nd*2)+query(node[nd*2+1].left,nd*2+1);
else
return query(pos,nd*2);
}
else if(pos>mid)
{
if(pos<node[nd*2+1].left+node[nd*2+1].l_1)
return query(pos,nd*2+1)+query(node[nd*2].right,nd*2);
else
return query(pos,nd*2+1);
}
}
int main()
{
int n,m,k,a[50000],pos,ans;
char s[2];
while(scanf("%d %d",&n,&m)!=EOF)
{
build(1,n,1);
k=0;
while(m--)
{
scanf("%s",s);
if(s[0]=='D')
{
scanf("%d",&pos);
a[k++]=pos;
update(pos,0,1);//如果是被破坏了 就修改值为0
}
else
if(s[0]=='R')
{
k--;
pos=a[k];
update(pos,1,1);//如果没有被破坏依然是1
}
else
if(s[0]=='Q')
{
scanf("%d",&pos);
ans=query(pos,1);
printf("%d\n",ans);
}
}
}
return 0;
}
{
int mid,l_len,r_len;
if(node[nd].left==pos&&node[nd].right==pos)
{
if(flag)
node[nd].l_1=node[nd].r_1=node[nd].max_1=1;
else node[nd].l_1=node[nd].r_1=node[nd].max_1=0;
return;
}
l_len=node[nd*2].right-node[nd*2].left+1;
r_len=node[nd*2+1].right-node[nd*2+1].left+1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid) update(pos,flag,nd*2);
else if(pos>mid) update(pos,flag,nd*2+1);
node[nd].max_1=max((node[nd*2].r_1+node[nd*2+1].l_1),max(node[nd*2].max_1,node[nd*2+1].max_1));
node[nd].l_1=node[nd*2].l_1;
if(node[nd*2].l_1==l_len) node[nd].l_1+=node[nd*2+1].l_1;
node[nd].r_1=node[nd*2+1].r_1;
if(node[nd*2+1].r_1==r_len) node[nd].r_1+=node[nd*2].r_1;
}
int query(int pos,int nd)
{
int mid,len;
len=node[nd].right-node[nd].left+1;
if(node[nd].left==node[nd].right||node[nd].max_1==0||node[nd].max_1==len)//node[nd].max_1==len 查找到某段都是连续的 就return
return node[nd].max_1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid)
{
if(pos>mid-node[nd*2].r_1)
/*例如1-10中 2-10均为1 pos=4 那么查找4的时候 要进入二个分支 1个是左分支
1个是右分支 在右分支中查找最左边的点 */
return query(pos,nd*2)+query(node[nd*2+1].left,nd*2+1);
else
return query(pos,nd*2);
}
else if(pos>mid)
{
if(pos<node[nd*2+1].left+node[nd*2+1].l_1)
return query(pos,nd*2+1)+query(node[nd*2].right,nd*2);
else
return query(pos,nd*2+1);
}
}
int main()
{
int n,m,k,a[50000],pos,ans;
char s[2];
while(scanf("%d %d",&n,&m)!=EOF)
{
build(1,n,1);
k=0;
while(m--)
{
scanf("%s",s);
if(s[0]=='D')
{
scanf("%d",&pos);
a[k++]=pos;
update(pos,0,1);//如果是被破坏了 就修改值为0
}
else
if(s[0]=='R')
{
k--;
pos=a[k];
update(pos,1,1);//如果没有被破坏依然是1
}
else
if(s[0]=='Q')
{
scanf("%d",&pos);
ans=query(pos,1);
printf("%d\n",ans);
}
}
}
return 0;
}
- hdu1540 Tunnel Warfare 线段树中等难题
- HDU1540:Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare 线段树区间合并
- poj2892 同hdu1540 Tunnel Warfare 线段树
- HDU1540-Tunnel Warfare-线段树区间合并
- hdu1540 Tunnel Warfare(线段树)
- HDU1540 Tunnel Warfare(线段树+set)
- hdu1540 Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare (线段树区间合并)
- poj2892&&HDU1540 Tunnel Warfare(线段树)
- hdu1540-Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare(线段树,区间合并)
- HDU1540 Tunnel Warfare (线段树区间合并)
- HDU1540 Tunnel Warfare(线段树+区间合并)
- HDU1540 Tunnel Warfare(线段树区间合并)
- HDU1540 Tunnel Warfare(线段树:维护最大连续子串)
- hdu1540 poj2892 Tunnel Warfare 线段树端点更新 SBT
- hdu1540 Tunnel Warfare(线段树区间合并详解)
- hdu 1166 敌兵布阵 基础线段树 2种做法
- Android进度条 多线程登录或下载
- hdu1698 Just a Hook 线段树之经典
- 安全服务体验
- 程序员求职之道(《程序员面试笔试宝典》)之数据结构与算法(数组和链表的区别)?
- hdu1540 Tunnel Warfare 线段树中等难题
- 22/7/2012 ICPC培训 第七天
- 湘潭市赛 Josephus Problem 线段树
- 开发流程
- 决定写个天气预报
- 新浪微博客户端 -- OAuth认证
- Mayor's posters 线段树+离散化+曾经RE无数边
- 部分复杂类型的总结
- linux进程的休眠(等待队列)