(线段树+加map)1287 加农炮

1287 加农炮

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 40 分
  6.  
  7. 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}。

 收起

输入

第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)。

输出

输出共M行,每行一个数,对应最终的地形高度。

输入样例

9 11
1
2
0
4
3
2
1
5
7
2
8
0
7
6
5
3
4
5
6
5

输出样例

2
2
2
4
3
3
5
6
7

题解:用线段树维护区间最大值,不过找的时候要找最靠左的且大于等于 b[i]的值,当然要用map预处理每个值出现的,在最左边的位置。

​
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int m,n;
int a[N],b[N];
int tree[N<<2];

void sett(int l,int r,int rt){
    if(l==r){
        tree[rt]=a[l];
        return ;
    }
    int mid=l+r>>1;
    sett(l,mid,rt<<1);
    sett(mid+1,r,rt<<1|1);
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}

void upset(int x,int l,int r,int rt){
    if(l==r){
        tree[rt]++;
        return ;
    }
    int mid=l+r>>1;
    if(x<=mid) upset(x,l,mid,rt<<1);
    else upset(x,mid+1,r,rt<<1|1);
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int ans=-1,leap=0;
void findd(int x,int l,int r,int rt){
    if(tree[rt]<x||leap)   
        return ;
    if(l==r){
        if(tree[rt]>=x){
            leap=1;
            ans=tree[rt];
            return ;
        }
    }
    int mid=l+r>>1;
    findd(x,l,mid,rt<<1);       // 先找左边,再找右边
    findd(x,mid+1,r,rt<<1|1);
}
map<int,int>mapp;

int main(){
    scanf("%d %d",&m,&n);
    for(int i=1;i<=m;i++){
        scanf("%d",&a[i]);
        if(mapp.count(a[i])) continue;
        mapp[a[i]]=i;
    }
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);

    sett(1,m,1);

    for(int i=1;i<=n;i++){
        if(b[i]<=a[1])
            continue;
        else{
            ans=-1; leap=0;            // 记得一定要重置
            findd(b[i],1,m,1);
            int d=ans;
            if(d==-1) continue;
            int h=mapp[d];
            upset(h-1,1,m,1);
            a[h-1]++;
            if(mapp.count(a[h-1]))
                mapp[a[h-1]]=min(mapp[a[h-1]],h-1);
            else
                mapp[a[h-1]]=h-1;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d\n",a[i]);
    return 0;
}

​

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值