HDU 4031 线段树

Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1869    Accepted Submission(s): 540


Problem Description
Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack American again. However, American is protected by a high wall this time, which can be treating as a segment with length N. Al Qaeda has a super weapon, every second it can attack a continuous range of the wall. American deployed N energy shield. Each one defends one unit length of the wall. However, after the shield defends one attack, it needs t seconds to cool down. If the shield defends an attack at kth second, it can’t defend any attack between (k+1)th second and (k+t-1)th second, inclusive. The shield will defend automatically when it is under attack if it is ready.

During the war, it is very important to understand the situation of both self and the enemy. So the commanders of American want to know how much time some part of the wall is successfully attacked. Successfully attacked means that the attack is not defended by the shield.
 


Input
The beginning of the data is an integer T (T ≤ 20), the number of test case.
The first line of each test case is three integers, N, Q, t, the length of the wall, the number of attacks and queries, and the time each shield needs to cool down.
The next Q lines each describe one attack or one query. It may be one of the following formats
1. Attack si ti
  Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N
2. Query p
  How many times the pth unit have been successfully attacked. 1 ≤ p ≤ N
The kth attack happened at the kth second. Queries don’t take time.
1 ≤ N, Q ≤ 20000
1 ≤ t ≤ 50
 


Output
For the ith case, output one line “Case i: ” at first. Then for each query, output one line containing one integer, the number of time the pth unit was successfully attacked when asked.
 


Sample Input
  
  
2 3 7 2 Attack 1 2 Query 2 Attack 2 3 Query 2 Attack 1 3 Query 1 Query 3 9 7 3 Attack 5 5 Attack 4 6 Attack 3 7 Attack 2 8 Attack 1 9 Query 5 Query 3
 


Sample Output
  
  
Case 1: 0 1 0 1 Case 2: 3 2
 


Source


题意,有一段墙,有给出n次攻击和询问,每次攻击攻击一小段墙,墙上每点都有防御设施,每次可以挡住一次攻击,但要冷却t的时间才能挡住下次攻击。每次询问询问墙上一个点,问这个点有几次被攻击但没有防御住。


刚开始拿到题目打算用线段树,但如何恢复防御设施不好处理,打算在插入操作时加上个参数:当前时间,线段树维护每段墙的被成功攻击的次数和恢复防御能力的时间,但这样每次插入操作都是nlogn,于是记录了个lazy标记allfail,如果这个区间的墙全部失效,那就不用继续往下进行插入,标记该区间的被攻击次数+1。询问时把询问走过的区间上的攻击次数值全部加起来就行,但这样TLE。。。。。。

TLE代码。。。

Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>

using namespace std;

#define MAXN 20010

int n, q, t, cnt;

class Tree
{
    public:
    bool allfail;
    int num, recover;
    Tree()
    {
        num = 0;
        allfail = 0;
    }
    void init()
    {
        num = 0;
        allfail = 0;
        recover = 0;
    }
};
Tree tree[MAXN*4];

void insert(int p, int l, int r, int x, int y, int time)
{

//cout << l << " " << r << " " << tree[p].num << endl;
    if(tree[p].recover <= time)
    {
        tree[p].allfail = 0;
    }

    if(x <= l && y >= r)
    {
        if(tree[p].allfail)
        {
            tree[p].num++;
            return;
        }
        else if(l == r && !tree[p].allfail)
        {
            tree[p].allfail = 1;
            tree[p].recover = time+t;
            return;
        }
    }

    int mid = (l+r) >> 1;

    if(x <= mid)
    {
        insert(p<<1, l, mid, x, y, time);
    }
    if(y > mid)
    {
        insert(p<<1|1, mid+1, r, x, y, time);
    }

    if(tree[p<<1].allfail && tree[p<<1|1].allfail) tree[p].allfail = 1;
    tree[p].recover = min(tree[p<<1].recover, tree[p<<1|1].recover);

}

void query(int p, int l, int r, int x, int& ans)
{
    ans += tree[p].num;

    if(l == r) return;


    int mid = (l+r) >> 1;

    if(x <= mid)
    {
        query(p<<1, l, mid, x, ans);
    }
    else query(p<<1|1, mid+1, r, x, ans);

}




int main()
{
    //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);
    int tt; scanf("%d", &tt);
    int cas = 0;
    while(tt--)
    {

        scanf("%d%d%d", &n, &q, &t);
        memset(tree, 0, sizeof(tree));

        printf("Case %d:\n", ++cas);

        string s;
        int x, y, time = 0;
        for(int i = 0; i < q; i++)
        {
            cin >> s;
            if(s[0] == 'A')
            {
                scanf("%d%d", &x, &y);
                //cout << s << " " << i+1  << " " << x << " " << y << endl;
                insert(1, 1, n, x, y, ++time);
            }
            else
            {
                int ans = 0;
                scanf("%d", &x);
                query(1, 1, n, x, ans);

                printf("%d\n", ans);
            }
        }


    }


    return 0;
}

于是上网看题解,将每次攻击记录,对每段墙维护一个下次恢复防御功能的时间和成功防御次数,询问时用当前询问之前的所有攻击来更新墙的属性,再通过线段树求出这段墙被攻击的总次数sum,用sum减去墙的成功防御次数,就得到询问的值。

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>

using namespace std;

#define MAXN 20010
class Attack
{
    public:
    int l, r;
};
Attack attack[MAXN];
class Wall
{
    public:
    int nowattack, defindnum;
};
Wall wall[MAXN];


int n, q, t, cnt, cnta;

class Tree
{
    public:
    bool allfail;
    int num;
    Tree()
    {
        num = 0;
        allfail = 0;
    }
    void init()
    {
        num = 0;
        allfail = 0;
    }
};
Tree tree[MAXN*4];

void insert(int p, int l, int r, int x, int y, int time)
{

//cout << l << " " << r << " " << tree[p].num << endl;

    if(x <= l && y >= r)
    {
        tree[p].num++;
        return;
    }

    int mid = (l+r) >> 1;

    if(x <= mid)
    {
        insert(p<<1, l, mid, x, y, time);
    }
    if(y > mid)
    {
        insert(p<<1|1, mid+1, r, x, y, time);
    }
}

void query(int p, int l, int r, int x, int& ans)
{
    ans += tree[p].num;

    if(l == r) return;


    int mid = (l+r) >> 1;

    if(x <= mid)
    {
        query(p<<1, l, mid, x, ans);
    }
    else query(p<<1|1, mid+1, r, x, ans);

}




int main()
{
    //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);
    int tt; scanf("%d", &tt);
    int cas = 0;
    while(tt--)
    {

        scanf("%d%d%d", &n, &q, &t);
        memset(tree, 0, sizeof(tree));
        memset(wall, 0, sizeof(wall));

        cnta = 0;

        printf("Case %d:\n", ++cas);

        string s;
        int x, y, time = 0;
        for(int i = 0; i < q; i++)
        {
            cin >> s;
            if(s[0] == 'A')
            {
                scanf("%d%d", &x, &y);
                //cout << s << " " << i+1  << " " << x << " " << y << endl;
                insert(1, 1, n, x, y, ++time);
                attack[cnta].l = x;
                attack[cnta++].r = y;
            }
            else
            {
                int sum = 0;
                scanf("%d", &x);
                query(1, 1, n, x, sum);

                for(int i = wall[x].nowattack; i < cnta; i++)
                {
                    if(attack[i].l <= x && attack[i].r >= x)
                    {
                        wall[x].defindnum++;
                        wall[x].nowattack = i+t;
                        i += t-1;
                    }
                }

                printf("%d\n", sum-wall[x].defindnum);
            }
        }


    }


    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值