`
java-mans
  • 浏览: 11416077 次
文章分类
社区版块
存档分类
最新评论

hdu1540 Tunnel Warfare 线段树中等难题

 
阅读更多

Tunnel Warfare

Time Limit : 4000/2000ms (Java/Other)Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1Accepted 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!

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
/*一路参考人家的代码 经历无数障碍终于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];

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;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics