码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1540 Tunnel Warfare

时间:2015-08-03 16:48:37      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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
 
题意:有n个村庄相连,现在有些村庄会遭到破坏,而每一次修复的是最近一个被破坏的村庄,(先进后出,可以使用数组模拟栈,也可以直接用栈)现在查询与k村庄相连的有几个村庄(如果k村庄本身被破坏还未修复,则输出0
 
#include<stdio.h>
#include<algorithm>
#include<stack>
#define N 50010
using namespace std;
struct node
{
    int left, right, sum, Lsum, Rsum; //sum存放与该点相连的点的总个数,Lsum存放从左端点开始向右与之相连的点的最大个数,Rsum存放从右端点开始向左与之相连的点的最大个数
}no[4*N];
void Bulid(int left, int right, int root)
{
    int mid;
    no[root].left = left;
    no[root].right = right;
    no[root].sum = no[root].Lsum = no[root].Rsum = (no[root].right-no[root].left+1); //开始时所有点相连的个数都等于其区间的长度
    if (left == right) return ;
    mid = (left+right)/2;
    Bulid(left, mid, root*2);
    Bulid(mid+1, right, root*2+1);
}
void Pushup(int root) //向上更新
{
    no[root].Lsum = no[root*2].Lsum;
    no[root].Rsum = no[root*2+1].Rsum;
    if (no[root*2].Lsum == no[root*2].right-no[root*2].left+1) //只有当左结点的Lsum与区间长度相等了才能加上右结点的部分,否则会有重复现象
        no[root].Lsum = no[root*2].Lsum + no[root*2+1].Lsum;
    if (no[root*2+1].Rsum == no[root*2+1].right-no[root*2+1].left+1)
        no[root].Rsum = no[root*2+1].Rsum + no[root*2].Rsum;
    no[root].sum = max(no[root].Lsum, max(no[root].Rsum, no[root*2].Rsum+no[root*2+1].Lsum));
}
void Update(int k, int num, int root)
{
    int mid;
    if (no[root].left == no[root].right)
    {
        no[root].Lsum = no[root].Rsum = num;
        return ;
    }
    mid = (no[root].left+no[root].right)/2;
    if (k <= mid) Update(k, num, root*2);
    else Update(k, num, root*2+1);
    Pushup(root);
}
int Query(int k, int root)
{
    int mid;
    if (no[root].sum == 0) return 0;
    if (k < no[root].left+no[root].Lsum) return no[root].Lsum;
    if (k > no[root].right-no[root].Rsum) return no[root].Rsum;
    if (k > no[root*2].right-no[root*2].Rsum && k < no[root*2+1].left+no[root*2+1].Lsum)
        return no[root*2].Rsum+no[root*2+1].Lsum;
    mid = (no[root].left+no[root].right)/2;
    if (k <= mid) return Query(k, root*2);
    else return Query(k, root*2+1);
}
int main ()
{
    int n, m, k, Sum;
    char s[10];
    stack<int>Q;
    while (scanf("%d%d", &n, &m) != EOF)
    {
        Bulid(1, n, 1);
        while (m--)
        {
            scanf("%s", s);
            if (s[0] == D)
            {
                scanf("%d", &k);
                Update(k, 0, 1);
                Q.push(k);
            }
            else if (s[0] == Q)
            {
                scanf("%d", &k);
                Sum = Query(k, 1);
                printf("%d\n", Sum);
            }
            else if (s[0] == R)
            {
                Update(Q.top(), 1, 1);
                Q.pop();
            }
        }
    }
    return 0;
}

HDU 1540 Tunnel Warfare

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4699510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!