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;
}






深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值