HDU-1540(线段树区间合并)

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

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
思路:利用线段树维护左端点起最长连续区间ll,右区间最长连续区间rr以及节点内最长连续区间sum

#include<iostream>
#include<stack>
using namespace std;
stack<int>st;
struct data
{
 int l,r;
 int ll,rr,sum;
}t[100010<<2];
void build(int i,int l,int r)
{
 t[i].l=l;
 t[i].r=r;
 t[i].ll=t[i].rr=t[i].sum=r-l+1;
 if(l==r)return;
 build(i*2,l,(l+r)/2);
 build(i*2+1,(l+r)/2+1,r);
}
void pushup(int i)
{
 t[i].ll=t[i*2].ll;
 t[i].rr=t[i*2+1].rr;
 t[i].sum=max(max(t[i*2].sum,t[i*2+1].sum),t[i*2].rr+t[i*2+1].ll);//当前区间最长连续区间长度一定为左子树最长区间,右子树最长区间以及两子树的横跨区间三者之一 
 if(t[i*2].ll==t[i*2].r-t[i*2].l+1)//当左子树区间满时,需要将右子树左区间也考虑到 
  t[i].ll+=t[i*2+1].ll;
 if(t[i*2+1].rr==t[i*2+1].r-t[i*2+1].l+1)//同理 
  t[i].rr+=t[i*2].rr;
}
void change(int i,int x,int y)
{
 if(t[i].l==t[i].r)
 {
  if(y)t[i].ll=t[i].rr=t[i].sum=1;
  else t[i].ll=t[i].rr=t[i].sum=0;
  return;
 }
 int mid=(t[i].l+t[i].r)/2;
 if(x<=mid)change(i*2,x,y);
 else change(i*2+1,x,y);
 pushup(i);
}
int query(int i,int r)
{
 if(t[i].l==t[i].r||t[i].sum==0||t[i].sum==t[i].r-t[i].l+1)return t[i].sum;//当查询到叶节点或者当前节点已经没有必要继续向下查找时返回当前节点值 
 int mid=(t[i].r+t[i].l)/2;
 if(r<=mid)
 {
  if(r>=t[i*2].r-t[i*2].rr+1)//当目标点位于左子树的右区间内则查询右子树左区间 
   return query(i*2,r)+query(i*2+1,mid+1);
  else 
   return query(i*2,r);
 }
 else
 {
  if(r<=t[i*2+1].l+t[i*2+1].ll-1)//同理 
   return query(i*2+1,r)+query(i*2,mid);
  else 
   return query(i*2+1,r);
 }
}
int main()
{
 char c;
 int n,m,p;
 cin>>n>>m;
 build(1,1,n);
 while(m--)
 {
  cin>>c;
  if(c=='D')
  {
   cin>>p;
   st.push(p);
   change(1,p,0);
  }
  else if(c=='Q')
  {
   cin>>p;
   cout<<query(1,p)<<endl;
  }
  else 
  {
   p=st.top();
   st.pop();
   change(1,p,1);
  }
 }
 return 0; 
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值