POJ2892 Tunnel Warfare

题解:

树状数组+二分裸题。

对于二分时的细节问题,要仔细分析再确定.

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define LL long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=50010;
const int mod=1e9+7;
const int INF=1e9;

int n,m,c[maxn];
int pre[maxn],x,cnt;
char op;

int lowbit(int x){return x&-x;}

void add(int x,int v){
    while(x<=n){
        c[x]+=v;
        x+=lowbit(x);
    }
}

int sum(int x){
    int num=0;
    while(x){
        num+=c[x];
        x-=lowbit(x);
    }
    return num;
}

int L_Bin(int l,int r){
   int R=r,m,goal=r;
   while(l<r){
         m=(l+r)>>1;
         if(sum(R)-sum(m)==R-m){
              goal=m+1;
              r=m;
         }
         else l=m+1;
   }
   return goal;
}

int R_Bin(int l,int r){
   int L=l,m,goal=l;
   while(l<=r){
         m=(l+r)>>1;
         if(sum(m)-sum(L)==m-L){
              goal=m;
              l=m+1;
         }
         else r=m-1;
   }
   return goal;
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        CLR(c);
        cnt=0;
        for(int i=1;i<=n;i++) add(i,1);
        for(int i=1;i<=m;i++){
           scanf(" %c",&op);
           if(op==‘D‘){
                 scanf("%d",&x);
                 add(x,-1);
                 pre[cnt++]=x;
           }
           if(op==‘R‘) add(pre[--cnt],1);
           if(op==‘Q‘){
                scanf("%d",&x);
                if(sum(x)-sum(x-1)==0){
                    printf("0\n");
                    continue;
                }
                int L=L_Bin(0,x);
                int R=R_Bin(x,n);
                //cout<<L<<" "<<R<<endl;
                printf("%d\n",R-L+1);
           }
        }
    }
    return 0;
}
时间: 2024-11-05 12:29:57

POJ2892 Tunnel Warfare的相关文章

POJ2892 Tunnel Warfare 题解

POJ2892 Tunnel Warfare 线段树例题解析合集 题意:有一条相邻节点相连的链(1和2,2和3,...n-1和n相连),有3种操作:1.破坏某一个节点 2.问从某个点可以到的点有多少个,即中间没有被破坏的点(包括自己) 3.重建当前最后一个被破坏的点 建立线段树维护每个点能到达的左右边界(初始值都为1,n),破坏一个点k时(设k能到达的左右边界为l1,r1),将l1到k-1的区间内的点的右边界修改为k,将k+1到r1的区间内的点的左边界修改为k 用一个栈存储点的破坏顺序,重建一个

hdu1540 &amp;&amp; POJ2892 Tunnel Warfare

[比赛提醒]BestCoder 你报名了吗?(点击报名) [科普]什么是BestCoder?如何参加? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4513    Accepted Submission(s): 1725 Problem Description During the War of Res

hdu1540 Tunnel Warfare 线段树/树状数组

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 con

HDU 1540 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 vill

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

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

HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7307   Accepted: 2997 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene