51nod 1287 加农炮 【线段树】

题目来源:  Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 40  难度:4级算法

一个长度为M的正整数数组A,表示从左向右的地形高度。测试一种加农炮,炮弹平行于地面从左向右飞行,高度为H,如果某处地形的高度大于等于炮弹飞行的高度H(A[i] >= H),炮弹会被挡住并落在i - 1处,则A[i - 1] + 1。如果H <= A[0],则这个炮弹无效,如果H > 所有的A[i],这个炮弹也无效。现在给定N个整数的数组B代表炮弹高度,计算出最后地形的样子。
例如:地形高度A = {1, 2, 0, 4, 3, 2, 1, 5, 7}, 炮弹高度B = {2, 8, 0, 7, 6, 5, 3, 4, 5, 6, 5},最终得到的地形高度为:{2, 2, 2, 4, 3, 3, 5, 6, 7}。
Input
第1行:2个数M, N中间用空格分隔,分别为数组A和B的长度(1 <= m, n <= 50000)
第2至M + 1行:每行1个数,表示对应的地形高度(0 <= A[i] <= 1000000)。
第M + 2至N + M + 1行,每行1个数,表示炮弹的高度(0 <= B[i] <= 1000000)。
Output
输出共M行,每行一个数,对应最终的地形高度。
Input示例
9 11
1
2
0
4
3
2
1
5
7
2
8
0
7
6
5
3
4
5
6
5
Output示例
2
2
2
4
3
3
5
6
7

水过------线段树查找与更新


代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
    int l,r,shu;
}pp[200000];
int p[50100];
void query(int x)
{
    pp[x].shu=max(pp[x*2].shu,pp[x*2+1].shu);
}
void create(int l,int r,int x)
{
    pp[x].l=l;pp[x].r=r;
    if (l==r)
    {
        pp[x].shu=p[l];return;
    }
    int m=(l+r)>>1;
    create(l,m,x*2);
    create(m+1,r,x*2+1);
    query(x);
}
int search_max(int x,int a)
{
    if (pp[x].l==pp[x].r) return pp[x].l;
    if (pp[x*2].shu>=a) return search_max(x*2,a);
    return search_max(x*2+1,a);
}
void update(int x,int a)
{
    if (pp[x].l==pp[x].r)
    {
        pp[x].shu=p[a];return ;
    }
    if (a<=(pp[x].l+pp[x].r)>>1)
        update(x*2,a);
    else
        update(x*2+1,a);
    query(x);
}
int main()
{
    int n,m,a;
    cin>>m>>n;
    for (int i=1;i<=m;i++)
        cin>>p[i];
    create(1,m,1);
    for (int i=0;i<n;i++)
    {
        cin>>a;
        if (a<=p[1]||a>pp[1].shu)
            continue;
        int k=search_max(1,a);
        p[k-1]++;
        update(1,k-1);
    }
    for (int i=1;i<=m;i++)
        cout<<p[i]<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值