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
/*一路参考人家的代码 经历无数障碍终于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;
}
分享到:
相关推荐
hdu 1166线段树代码
hdu 1166线段树
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
【hdu5306】Gorgeous Sequence 线段树区间最值操作-附件资源
线段树入门资料,有利于初学者学习,让他们详细了解线段树。
NULL 博文链接:https://128kj.iteye.com/blog/1738772
大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同->注意这组数据 3 1 10 1 3 6 10 the ans is 3. hdu 1754 求区间最大值 hdu 1166 求区间和 hdu 1698 成段更新 ...
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
题面 【题目描述】 有nnn个营地,已知每个营地的人数,有四条命令: (1)Add(1) Add(1)Add iii jjj,iii和jjj为正整数,表示第iii个营地增加jjj个人(jjj不超过303030) (2)Sub(2)Sub(2)Sub iii jjj ,iii和jjj为正...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。