关闭

HDU 1540 Tunnel Warfare xds

标签: acm
292人阅读 评论(0) 收藏 举报
分类:

G - Tunnel Warfare
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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
 

区间合并的题

被我xjb二分卡过去了


ACcode;

#include <cstdio>
#include <cstring>
#define tmp (st<<1)
#define mid ((l+r)>>1)
#define lson l,mid,tmp
#define rson mid+1,r,tmp|1
#define maxn 50002
#define len (r-l+1)
#define push_up(x) sum[x]=sum[tmp]+sum[tmp|1]
using namespace std;
int sum[maxn<<2],sta[maxn],top;
inline void build(int l,int r,int st){
    if(l==r){
        sum[st]=1;
        return ;
    }
    build(lson);
    build(rson);
    push_up(st);
}
inline void updata(int t,int add,int l,int r,int st){
    if(l==r){
        sum[st]=add;
        return ;
    }
    if(t<=mid)updata(t,add,lson);
    else updata(t,add,rson);
    push_up(st);
}
inline int query(int L,int R,int l,int r,int st){
    if(L<=l&&r<=R)return sum[st];
    int ret=0;
    if(L<=mid)ret+=query(L,R,lson);
    if(R>mid)ret+=query(L,R,rson);
    return ret;
}
int main(){
    int n,q,x;
    char str[4];
    while(~scanf("%d%d",&n,&q)){
        build(1,n,1);
        top=0;
        while(q--){
            scanf("%s",str);
            if(str[0]=='D'){
                scanf("%d",&x);
                updata(x,0,1,n,1);
                sta[++top]=x;
            }
            else if(str[0]=='R'&&top){
                    x=sta[top--];
                    updata(x,1,1,n,1);
            }
            else if(str[0]=='Q'){
                scanf("%d",&x);
                int ans=query(x,x,1,n,1);
                if(ans==0){
                    printf("0\n");
                    continue;
                }
                int l=1,r=x-1,t,k=0;
                while(l<=r){
                    t=query(mid,x-1,1,n,1);
                    if(t==x-mid){
                        k=x-mid;
                        r=mid-1;
                    }else l=mid+1;
                }
                ans+=k;
                l=x+1,r=n,k=0;
                while(l<=r){
                    t=query(x+1,mid,1,n,1);
                    if(t==mid-x){
                        k=mid-x;
                        l=mid+1;
                    }else r=mid-1;
                }
                ans+=k;
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

花了一下午学了下区间合并

对于这道题来说查询到t的位置的时候要考虑t~mid或mid~t是否连续如果是的话需要查询连续的那一块

ACcode:


#include <cstdio>
#include <algorithm>
#include <cstring>
#define tmp (st<<1)
#define mid ((l+r)>>1)
#define lson l,mid,tmp
#define rson mid+1,r,tmp|1
#define maxn 50002
#define len (r-l+1)
using namespace std;
int sum[maxn<<2],sta[maxn],top,lsum[maxn<<2],rsum[maxn<<2];
inline void push_up(int st,int m){
    lsum[st]=lsum[tmp];
    rsum[st]=rsum[tmp|1];
    sum[st]=max(max(sum[tmp],sum[tmp|1]),lsum[tmp|1]+rsum[tmp]);
    if(lsum[st]==m-(m>>1))lsum[st]+=lsum[tmp|1];
    if(rsum[st]==m>>1)rsum[st]+=rsum[tmp];
}
inline void build(int l,int r,int st){
    sum[st]=lsum[st]=rsum[st]=len;
    if(l==r)return ;
    build(lson);
    build(rson);
}
inline void updata(int t,int add,int l,int r,int st){
    if(l==r){
        sum[st]=lsum[st]=rsum[st]=add;
        return ;
    }
    if(t<=mid)updata(t,add,lson);
    else updata(t,add,rson);
    push_up(st,len);
}
inline int query(int t,int l,int r,int st){
    if(l==r||sum[st]==0||sum[st]==len) return sum[st];
    if(t<=mid){
        if(t>=mid-rsum[tmp]+1)///如果t到mid连续要查询mid右边
            return query(t,lson)+query(mid+1,rson);
        else
            return query(t,lson);
    }
    else {
        if(t<=mid+1+lsum[tmp|1]-1 )///同理
            return query(t,rson)+query(mid,lson);
        else
            return query(t,rson);
    }
}
int main(){
    int n,q,x;
    char str[4];
    while(~scanf("%d%d",&n,&q)){
        build(1,n,1);
        top=0;
        while(q--){
            scanf("%s",str);
            if(str[0]=='D'){
                scanf("%d",&x);
                updata(x,0,1,n,1);
                sta[++top]=x;
            }
            else if(str[0]=='R'&&top){
                    x=sta[top--];
                    updata(x,1,1,n,1);
            }
            else if(str[0]=='Q'){
                scanf("%d",&x);
               printf("%d\n",query(x,1,n,1));
            }
        }
    }
    return 0;
}


0
0
查看评论

hdu1540Tunnel Warfare 线段树

//Q pos 问包含pos的连续区间的长度 //D pos 删除pos位置的元素 //R 恢复上一个被删除的元素 //用栈维护被删除的元素位置 //用线段树维护左边连续区间,右边连续区间的长度 #include<cstdio> #include<cstring> #incl...
  • cq_pf
  • cq_pf
  • 2015-07-26 17:19
  • 552

hdu 1540 Tunnel Warfare (线段树区间合并)

Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6065 Accepted Submission(s...
  • qiqi_skystar
  • qiqi_skystar
  • 2015-12-17 10:45
  • 1372

HDU1540 Tunnel Warfare(线段树:维护最大连续子串)

HDU1540 Tunnel Warfare(线段树:区间合并) http://acm.hdu.edu.cn/showproblem.php?pid=1540 分析:        首先先来分析题目中的3种操作: 1.D x: 该操作就是单...
  • u013480600
  • u013480600
  • 2014-03-29 19:18
  • 1141

HDU1540: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. Ge...
  • libin56842
  • libin56842
  • 2013-11-03 15:05
  • 5502

POJ2892 HDU 1540 Tunnel Warfare, 树状数组

利用树状数组的find_k_th实现找到sum为k的最小位置,  时间复杂度为O(log(n)) /******************************************************************************* # Auth...
  • neofung
  • neofung
  • 2012-01-29 23:54
  • 967

Tunnel Warfare(hdu 1540)

Tunnel Warfare TimeLimit: 4000/2000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others) Total Submission(s): 10244 &...
  • xiao_k666
  • xiao_k666
  • 2017-11-05 19:47
  • 36

hdu 1540 Tunnel Warfare

线段树的区间更新,求得区间内的最长序列: 所以可以寻找两个端点,也就是被炸毁的村庄作为端点,然后再把0和n+1这两个点置为1, 0在这里表示村庄没有没炸毁,1表示被炸毁,-1表示这个区间内既有被炸毁的村庄,也有没有被炸毁的村庄, 那么查询两次找出两个端点,其区间也就确定了 //#pragma co...
  • moyan_min
  • moyan_min
  • 2013-10-28 14:31
  • 648

hdu 1540 Tunnel Warfare

题目 线段树. 三种操作:D x->摧毁村庄x,                 R ->  修复被毁时间最近的那个村庄         &#...
  • Zhuhuangjian
  • Zhuhuangjian
  • 2013-07-29 20:18
  • 716

Tunnel Warfare HDU - 1540

Tunnel Warfare HDU - 1540
  • Tsingcheng_liu
  • Tsingcheng_liu
  • 2017-08-12 21:16
  • 65

HDU 1540——Tunnel Warfare

Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9558 ...
  • qq_37257079
  • qq_37257079
  • 2017-08-16 15:52
  • 53
    个人资料
    • 访问:395207次
    • 积分:12003
    • 等级:
    • 排名:第1477名
    • 原创:808篇
    • 转载:2篇
    • 译文:0篇
    • 评论:111条
    最新评论