poj 2892(二分+树状数组)

Tunnel Warfare

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7749   Accepted: 3195

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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO
D 3   OOXOOOO
D 6   OOXOOXO
D 5   OOXOXXO
R     OOXOOXO
R     OOXOOOO

题意:三种操作D x 摧毁城市xR 修复最后被摧毁的城市Q x 问 x 所在最长未被摧毁的区间长度是多少

题解:对于询问Q x 二分枚举左右边界,利用树状数组进行维护满足条件的区间满足此式子 SUM(r) - SUM(l-1) = r-l+1(SUM[i]代表前i个城市未被摧毁的城市数量,SUM(r) - SUM(l-1)代表[l,r]内城市的存留数量)

二分的时候注意一下下边界,初始值要在最小的基础上减一。
/**
题意:三种操作
D x 摧毁城市x
R 修复最后被摧毁的城市
Q x 问 x 所在最长未被摧毁的区间长度是多少

题解:对于询问Q x 二分枚举左右边界,利用树状数组进行维护
满足条件的区间满足此式子 SUM(r) - SUM(l-1) = r-l+1(SUM[i]代表前i个城市未被摧毁的城市数量,SUM(r) - SUM(l-1)代表[l,r]内城市的存留数量)
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
const int N = 50005;

int c[N];
bool destory[N];
stack<int> stk;
int n,q;
int lowbit(int x){
    return x&(-x);
}
void update(int idx,int v){
    for(int i=idx;i<=n;i+=lowbit(i)){
        c[i]+=v;
    }
}
int getsum(int idx){
    int v=0;
    for(int i=idx;i>=1;i-=lowbit(i)){
        v+=c[i];
    }
    return v;
}
int main()
{
    while(scanf("%d%d",&n,&q)!=EOF){
        while(!stk.empty()) stk.pop();
        memset(c,0,sizeof(c));
        memset(destory,false,sizeof(destory));
        for(int i=1;i<=n;i++){
            update(i,1);
        }
        while(q--){
            char s[5];
            int x;
            scanf("%s",s);
            if(s[0]==‘D‘){
                scanf("%d",&x);
                if(destory[x]) continue;
                destory[x] = true;
                update(x,-1);
                stk.push(x);

            }else if(s[0]==‘R‘){
                if(stk.empty()) continue;
                x = stk.top();
                stk.pop();
                update(x,1);
                destory[x] = false;
            }else{
                scanf("%d",&x);
                if(destory[x]){
                    printf("0\n");
                    continue;
                }
                int l=x,r=x;
                int low = 0,high = x;
                while(low<=high){
                    int mid = (low+high)>>1;
                    if(getsum(x)-getsum(mid)==x-mid){
                        l = mid+1;
                        high = mid-1;
                    }else low = mid+1;
                }
                low = x-1,high = n;
                while(low<=high){
                    int mid = (low+high)>>1;
                    if(getsum(mid)-getsum(x-1)==mid-x+1){
                        r = mid;
                        low = mid+1;
                    }else high = mid-1;
                }
                printf("%d\n",r-l+1);
            }
        }
    }
    return 0;
}
时间: 2024-08-05 07:04:08

poj 2892(二分+树状数组)的相关文章

POJ 2309 BST 树状数组基本操作

Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until t

11525 - Permutation(二分+树状数组)

题目链接:点击打开链接 题意:从1~k的所有排列中找到第n个排列, n由公式给出. 思路:可以发现, 这个公式就是康托展开公式(康托展开百科:点击打开链接). 那么s[i]的意思就是i个数中当前数排在第几. 如此, 可以用二分+树状数组快速求解, 和一道BC题目神似. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<st

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BIU的第i个成员国希望能够收集Pi单位的陨石样本.你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石. 输入 第一行是两个数N,M. 第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站. 第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量. 第四行有一个

Poj 2299 Ultra-QuickSort 树状数组 解法

本题的树状数组稍微有点特点,就是需要所谓的离散化一下,开始听这个名称好像很神秘的,不过其实很简单. 就是把一个数组arr的值,其中的值是不连续的,变成一组连续的值,因为这样他们的顺序是不变的,所以,不影响结果. 例如:9 1 0 5 4 ->变为:5 2 1 4 3看出他们的相对位置不变的. 9和5为最大值在第一个位置,1和2为第二大的值在第二个位置,0和1在第一个位置等,看出对应顺序了吗? 对,就是这么简单的方法, 就叫做离散化. 如果你对counting sort熟悉的话,那么这样的思想理解

【51nod】 第K大区间2(二分+树状数组)

[51nod] 第K大区间2(二分+树状数组) 第K大区间2 ﹡    LH (命题人) 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 定义一个长度为奇数的区间的值为其所包含的的元素的中位数.中位数_百度百科 现给出n个数,求将所有长度为奇数的区间的值排序后,第K大的值为多少. 样例解释: [l,r]表示区间的值 [1]:3 [2]:1 [3]:2 [4]:4 [1,3]:2 [2,4]:2 第三大是2 Input 第一行两个数n和k(1<=n<=100000,k&l

【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT

POJ 2481 Cows(树状数组)

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. Farmer John has N cows (we number the cows from 1 to N). Ea

【BZOJ-2527】Meteors 整体二分 + 树状数组

2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Status][Discuss] Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colo