HDU 1540 Tunnel Warfare 地道战 线段树

本文介绍了一个模拟抗日战争期间地道战的算法题目。该题目通过维护一系列村庄间的地道连接状态,来模拟地道被破坏和重建的过程,并能快速查询任意村庄的连接情况。采用了一种区间合并的数据结构实现高效的查询和更新。

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11746    Accepted Submission(s): 4596


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
 

Source
 
只要维护左右最长连续长度就可以过了 区间合并

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 50050;
int lmax[MAX_N<<2],rmax[MAX_N<<2];
int Stack[MAX_N];
void up(int p,int l,int r){
    int mid = (l+r)>>1;
   lmax[p] = lmax[p*2];
   if(lmax[p]==(mid-l+1)){
       lmax[p]+=lmax[p*2+1];
   }
   rmax[p]= rmax[p*2+1];
   if(rmax[p]==(r-mid)){
       rmax[p]+=rmax[p*2];
   }
}
void build(int p,int l,int r){
   if(l==r){
    lmax[p]=rmax[p] = 1;
    return ;
   }
   int mid = (l+r)>>1;
   build(p*2,l,mid);
   build(p*2+1,mid+1,r);
   up(p,l,r);
}
void change(int p,int l,int r,int x,int v){
    if(l==r){
        lmax[p] = rmax[p] = v;
        return;
    }
    int mid =(l+r)>>1;
    if(x<=mid) change(p*2,l,mid,x,v);
    else change(p*2+1,mid+1,r,x,v);
    up(p,l,r);
}

int lnum,rnum;

int query(int p,int l,int r,int x){
   if(l==r){
    lnum = rnum = x;
    return lmax[p];
   }
   int mid = (l+r)>>1;
   if(x<=mid){
    int ans = query(p*2,l,mid,x);
    if(!ans) return 0;
    else if(rnum==mid&&lmax[p*2+1]){
        rnum+=lmax[p*2+1];
        ans+=lmax[p*2+1];
    }
            return ans;
   }
   else {
    int ans = query(p*2+1,mid+1,r,x);
    if(!ans) return 0;
    else if(lnum == mid+1 && rmax[p*2]){
        lnum-=rmax[p*2];
        ans+=rmax[p*2];
    }
            return ans;
   }
}

int main(){
    char str[5];
   int n,m,cnt;
   while(~scanf("%d%d",&n,&m)){
   cnt = 0;
   build(1,1,n);
   while(m--){
    scanf("%s",str);
    if(str[0]=='D'){
        int d;
        scanf("%d",&d);
        Stack[cnt++] = d;
        change(1,1,n,d,0);
    }
    else if(str[0]=='Q'){
        int d;
        scanf("%d",&d);
        printf("%d\n",query(1,1,n,d));
    }
    else {
        if(cnt==0) continue;
        change(1,1,n,Stack[cnt-1],1);
        cnt--;
    }
   }
   }
   return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值