HDU 1540 Tunnel Warfare (线段树)

本文介绍了一个模拟抗日战争期间地道战的算法问题。通过线段树数据结构实现村庄连接状态的实时更新与查询,解决了因村庄被破坏导致的连接中断问题。

Tunnel Warfare

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
题目大意:有n个村庄,初始的时候都连通,有m次操作,每一次不同的操作会改变村庄的连通情况或者是查询某个村庄的情况。
思路:啊!真的是蠢到家了,一开始想的是用线段树+二分来做,后头想了一想好像有点不对劲,明明线段树就是一次二分了,为啥还要加一重二分呢?。。。(我怕真是个傻子)
  后来借鉴了一下大佬的博客  https://blog.youkuaiyun.com/libin56842/article/details/14105071 哎呀,用了肥虎之力,总算是把这个题给ac了。
  总的来说,在进行每一次点更新的时候我们维护三个值,对于当前的这个点,维护左边最大连续的区间,右边最大的连续区间,和当前最大的连续区间,在点更新的时候不断更新父亲节点的区间即可
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<stack>
 6 
 7 using namespace std;
 8 const int maxn = 50005;
 9 struct node{
10     int l,r;
11     int ls,rs,ms;//ls左端最大连续区间,rs右边最大连续区间,ms最大连续区间
12 }sum[maxn<<2];
13 void build(int l,int r,int rt)
14 {
15     sum[rt].l=l;sum[rt].r=r;
16     sum[rt].ls=sum[rt].rs=sum[rt].ms=(r-l+1);
17     if(l!=r){
18         int mid = (l+r)>>1;
19         build(l,mid,rt<<1);
20         build(mid+1,r,rt<<1|1);
21     }
22 }
23 void update(int L,int R,int rt)
24 {
25     if(sum[rt].l==sum[rt].r){//单点更新操作的村庄
26         if(R==1)
27             sum[rt].ls = sum[rt].rs = sum[rt].ms = 1;
28         else
29             sum[rt].ls = sum[rt].rs = sum[rt].ms = 0;
30         return;
31     }
32     int mid = (sum[rt].l+sum[rt].r)>>1;
33     if(L<=mid) update(L,R,rt<<1);//向左边找
34     else update(L,R,rt<<1|1);//右边找
35     //更新子区间之后再更新当前的区间
36     sum[rt].ls = sum[rt<<1].ls;
37     sum[rt].rs = sum[rt<<1|1].rs;
38     sum[rt].ms = max(max(sum[rt<<1].ms,sum[rt<<1|1].ms),sum[rt<<1].rs+sum[rt<<1|1].ls);
39     if(sum[rt<<1].ls==sum[rt<<1].r-sum[rt<<1].l+1)//如果左子树满了需要加上右子树的左区间
40         sum[rt].ls += sum[rt<<1|1].ls;
41     if(sum[rt<<1|1].rs==sum[rt<<1|1].r-sum[rt<<1|1].l+1)//同理
42         sum[rt].rs += sum[rt<<1].rs;
43 }
44 int query(int L,int rt)
45 {
46     if(sum[rt].l==sum[rt].r||sum[rt].ms==0||sum[rt].ms==(sum[rt].r-sum[rt].l+1))
47         return sum[rt].ms;//返回的条件
48     int mid = (sum[rt].l+sum[rt].r)>>1;
49     if(L<=mid){//如果要查的点再mid的左子树
50         if(L>=sum[rt<<1].r-sum[rt<<1].rs+1)return query(L,rt<<1)+query(mid+1,rt<<1|1);//左区间已满需要加上右子树的左边
51         else return query(L,rt<<1);//左子树未满,只需要加左子树的值
52     }else{
53         if(L<=sum[rt<<1|1].l+sum[rt<<1|1].ls-1)return query(L,rt<<1|1)+query(mid,rt<<1);//同理
54         else query(L,rt<<1|1);
55     }
56 }
57 int main()
58 {
59     int n,m,x;char op;
60     while(scanf("%d%d",&n,&m)!=EOF){
61         stack<int>Q;
62         build(1,n,1);
63         while(m--){
64             scanf(" %c",&op);//去掉行末换行符的影响
65             if(op=='D'){
66                 scanf("%d",&x);
67                 Q.push(x);
68                 update(x,0,1);
69             }else if(op=='Q'){
70                 scanf("%d",&x);
71                 printf("%d\n",query(x,1));
72             }else if(op=='R'){
73                 if(!Q.empty()&&x>0){
74                     update(Q.top(),1,1);
75                     Q.pop();
76                 }
77             }
78         }
79     }
80     return 0;
81 }

 

转载于:https://www.cnblogs.com/wangrunhu/p/9637371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值