51nod1287 加农炮(线段树)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuxiyulu/article/details/77679089
/*
线段树
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e4+5;
int n,m;
int a[maxn];

struct node
{
    int left,right;
    int h;

}tree[maxn<<2];

void push_up(int i)
{
    tree[i].h=max(tree[i<<1].h,tree[i<<1|1].h);
}
void build(int i,int L,int R)
{
    tree[i].left=L;
    tree[i].right=R;
    if(L==R)
    {
        tree[i].h=a[L];
        return ;
    }
    int mid=(tree[i].left+tree[i].right)>>1;

    build(i<<1,L,mid);
    build(i<<1|1,mid+1,R);
    push_up(i);
}
int search_pos(int i,int height)
{
    if(tree[i].left==tree[i].right)
    {
        return tree[i].left;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(height<=tree[i<<1].h) search_pos(i<<1,height);
    else search_pos(i<<1|1,height);
}
void update(int i,int ql,int qr)
{
    if(tree[i].left==tree[i].right)
    {
        tree[i].h++;
        return ;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(qr<=mid) update(i<<1,ql,qr);
    else update(i<<1|1,ql,qr);

    push_up(i);
}
int main()
{
   while(~scanf("%d%d",&n,&m))
   {
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&a[i]);
       }
       build(1,1,n);
       int b;
       for(int i=1;i<=m;i++)
       {
           scanf("%d",&b);
           if(b<=a[1]||b>tree[1].h)
              continue;
           int idx=search_pos(1,b);
           a[idx-1]++;
           update(1,1,idx-1);
       }
       for(int i=1;i<=n;i++)
       {
           printf("%d\n",a[i]);
       }
   }
   return 0;
}
/*
预处理
预处理每个炮弹轰炸位置,然后对于每次轰炸更新位置信息
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5e4+5;
const int maxm=1e6+5;
int n,m;
int a[maxn],b[maxn];
int pos[maxm];//注意高度范围

int main()
{
   while(~scanf("%d%d",&n,&m))
   {
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&a[i]);
       }
       int maxh=0;//最大高度
       for(int i=1;i<=m;i++)
       {
           scanf("%d",&b[i]);
           maxh=max(b[i],maxh);
       }
       int p=1;
       memset(pos,0,sizeof(pos));
       for(int i=1;i<=maxh;i++)//预处理轰炸位置
       {
           while(p<=n&&a[p]<i)
           {
               p++;
           }
           pos[i]=p-1;
       }
       for(int i=1;i<=m;i++)//每个炸弹进行轰炸
       {
           p=pos[b[i]];
           if(p==0||p==n)
            continue;
           a[p]++;
           pos[a[p]]=min(pos[a[p]],p-1);//更新位置信息
       }
       for(int i=1;i<=n;i++)
       {
           printf("%d\n",a[i]);
       }
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuxiyulu/article/details/77679089
今日推荐