Mayor's posters (线段树+离散化)

Mayor's posters

 

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

这是一道很经典的线段树离散化问题,题目中给出的区间[li,ri]的数据都很大,直接用线段树做的话至少需要10000000*4的空间,因此,我们首先要对数据进行离散化处理
首先,我们要弄清楚离散化的概念,我们根据题目样例进行分析,可以看到[1,4],[2,6],[8,10],[3,4],[7,10]这五个区间,我们将这10个数都拿出来排个序,创建序列a
1 2 3 4 4 6 7 8 10 10
接下来我们对序列a去下重
1 2 3 4 6 7 8 10
然后我们用他们的相对大小(例如:6在这个序列中是第5大,10在这个序列中是第8大)建立另一个序列b
1 2 3 4 5 6 7 8
最后我们用每个数对应的相对大小来替换原来的区间就变成了这样
[1,4],[2,5],[7,8],[3,4],[6,8]
我们用这个区间来计算一下最后能观察到的海报数的话会发现,结果仍然是4

我个人对离散化的理解就是用数字的相对大小来替换他的实际大小从而达到减小其值,却不破坏它的位置关系的目的
这道题我们利用这样的方法就能将li,ri这么大的数缩小到最大只有20000(因为最多有20000个点)
理解题意后我们直接看代码,至于线段树部分就是普通的区间修改而已
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'
#define ll long long

struct node
{
    int l, r, flag;
} tree[800005];

int base[20005], ls[20005];
set<int> s;

void tree_build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].flag = 0;
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    tree_build(i << 1, l, mid);
    tree_build(i << 1 | 1, mid + 1, r);
}

void push_down(int i)
{
    if (tree[i].flag)
    {
        tree[i << 1].flag = tree[i].flag;
        tree[i << 1 | 1].flag = tree[i].flag;
        tree[i].flag = 0;
    }
}

void update(int i, int l, int r, int num)
{
    if (tree[i].l >= l && tree[i].r <= r)
    {
        tree[i].flag = num;
        return;
    }
    if (tree[i].r < l || tree[i].l > r)
    {
        return;
    }
    push_down(i);
    if (tree[i << 1].r >= l)
    {
        update(i << 1, l, r, num);
    }
    if (tree[i << 1 | 1].l <= r)
    {
        update(i << 1 | 1, l, r, num);
    }
}

void search(int i, int l, int r)
{
    if (tree[i].flag)
    {
        s.insert(tree[i].flag);//利用set自动去重的性质来记录海报数量
        return;
    }
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    search(i << 1, l, mid);
    search(i << 1 | 1, mid + 1, r);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, x, y, pos = 1;
        cin >> n;
        s.clear();
        tree_build(1, 1, 200005);
        for (int i = 1; i <= n; ++i)
        {
            //这地方我们开两个数组,base用与计算每个数的相对位置,ls是离散化后的数组
            cin >> x >> y;
            base[pos] = x;
            ls[pos++] = x;
            base[pos] = y;
            ls[pos++] = y;
        }
        //无论是sort还是unique还是lower_bound区间设定都是左闭右开的形式,品,你细细的品
        sort(base + 1, base + pos);
        int num = unique(base + 1, base + pos) - base;//对base排序并去重,必须排序后才能用unique
        for (int i = 1; i < pos; ++i)
        {
            ls[i] = lower_bound(base + 1, base + num, ls[i]) - base;
        }
        for (int i = 2; i < pos; i += 2)
        {
            update(1, ls[i - 1], ls[i], i);
        }
        search(1, 1, 200005);
        cout << s.size() << endl;
    }
    return 0;
}
 

 

你以为这就完事了?其实这样离散化在这道题中会有一些bug,我们看这组数据[1,10],[1,3],[6,10],很明显答案是3
但是离散化之后为[1,4],[1,2],[3,4],答案变成了2
为解决这种问题,我们可以在更新线段树的时候将区间从[l,r]变成[l,r-1],就将区间转化成了[1,3],[1,1],[3,3]这样的树
但是当我们遇到这样的数据[1,3],[1,1],[2,2],[3,3],就会导致区间更新时出错,我们可以将初始数据的r都加上1,就排除了li和ri相等的情况,如果没有这种情况,离散化后的区间也都是一样的
其实这道题数据很弱,不管这样的情况也能过(逃
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'
#define ll long long

struct node
{
    int l, r, flag;
} tree[100005];

int base[20005], ls[20005];
set<int> s;

void tree_build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].flag = 0;
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    tree_build(i << 1, l, mid);
    tree_build(i << 1 | 1, mid + 1, r);
}

void push_down(int i)
{
    if (tree[i].flag)
    {
        tree[i << 1].flag = tree[i].flag;
        tree[i << 1 | 1].flag = tree[i].flag;
        tree[i].flag = 0;
    }
}

void update(int i, int l, int r, int num)
{
    if (tree[i].l >= l && tree[i].r <= r)
    {
        tree[i].flag = num;
        return;
    }
    if (tree[i].r < l || tree[i].l > r)
    {
        return;
    }
    push_down(i);
    if (tree[i << 1].r >= l)
    {
        update(i << 1, l, r, num);
    }
    if (tree[i << 1 | 1].l <= r)
    {
        update(i << 1 | 1, l, r, num);
    }
}

void search(int i, int l, int r)
{
    //cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
    if (tree[i].flag)
    {
        //cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
        s.insert(tree[i].flag);
        return;
    }
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    search(i << 1, l, mid);
    search(i << 1 | 1, mid + 1, r);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, x, y, pos = 1;
        cin >> n;
        s.clear();
        tree_build(1, 1, 20005);
        for (int i = 1; i <= n; ++i)
        {
            cin >> x >> y;
            base[pos] = x;
            ls[pos++] = x;
            base[pos] = y + 1;
            ls[pos++] = y + 1;
        }
        sort(base + 1, base + pos);
        int num = unique(base + 1, base + pos) - base;
        for (int i = 1; i < pos; ++i)
        {
            ls[i] = lower_bound(base + 1, base + num, ls[i]) - base;
        }
        for (int i = 2; i < pos; i += 2)
        {
            update(1, ls[i - 1], ls[i] - 1, i);
        }
        search(1, 1, 20005);
        cout << s.size() << endl;
    }
    return 0;
}
 

 

 
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
应用背景为变电站电力巡检,基于YOLO v4算法模型对常见电力巡检目标进行检测,并充分利用Ascend310提供的DVPP等硬件支持能力来完成流媒体的传输、处理等任务,并对系统性能做出一定的优化。.zip深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 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、付费专栏及课程。

余额充值