一个长度为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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#define max_ 50010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
struct node
{
int l,r,w;
int order;
};
struct node tree[max_*4];
int n,m,cnt=1;
int num[max_];
void built(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].order=0;
if(l==r)
{
scanf("%d",&tree[i].w);
tree[i].order=cnt;
num[cnt++]=tree[i].w;
return;
}
int mid=(l+r)>>1;
built(i<<1,l,mid);
built(i<<1|1,mid+1,r);
tree[i].w=max(tree[i<<1].w,tree[i<<1|1].w);
}
void query(int i,int f)
{
if(tree[i<<1].order!=0&&tree[i<<1|1].order!=0)
{
tree[i<<1].w=num[tree[i<<1].order];
tree[i<<1|1].w=num[tree[i<<1|1].order];
tree[i].w=max(tree[i<<1].w,tree[i<<1|1].w);
return;
}
if(tree[i].l==tree[i].r)
{
tree[i].w=num[tree[i].order];
return;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(f<=mid)
query(i<<1,f);
else
query(i<<1|1,f);
tree[i].w=max(tree[i<<1].w,tree[i<<1|1].w);
}
int flag=0;
void update(int i,int x)
{
if(tree[i].l==tree[i].r)
{
num[tree[i].order-1]++;
flag=tree[i].order-1;
return;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(x<=tree[i<<1].w)
update(i<<1,x);
else
update(i<<1|1,x);
}
int main(int argc, char const *argv[])
{
scanf("%d%d",&m,&n);
built(1,1,m);
while(n--)
{
int x;
flag=0;
scanf("%d",&x);
if(x<=num[1]||x>tree[1].w)
continue;
else
{
update(1,x);
query(1,flag);
}
}
for(int i=1;i<=m;i++)
printf("%d\n",num[i]);
return 0;
}