51NOD1287 加农炮(线段树)

来源:互联网 发布:mac更新后重启黑屏 编辑:程序博客网 时间:2024/04/30 02:45
一个长度为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}Input1行:2个数M, N中间用空格分隔,分别为数组A和B的长度(1 <= m, n <= 50000)第2至M + 1行:每行1个数,表示对应的地形高度(0 <= A[i] <= 1000000)。第M + 2N + M + 1行,每行1个数,表示炮弹的高度(0 <= B[i] <= 1000000)。Output输出共M行,每行一个数,对应最终的地形高度。Input示例9 1112043215728076534565Output示例222433567
好多种做法吧,不过我用的线段树。这道题的题意是其实就是找到第一个大于等于B[i]的点的位置posA,然后这一个点的前一个点+1,既A[posA-1]+1.用线段树其实很好做(我从1开始算的,既[1,N])。re[maxn]是标记每个点的位置.然后我们找到第一个大于等于B的点,然后再更新前一个点就行了。注意当r==2的时候,一开始wa了是没有主要到A[1],因为里面有句。如果H<=A[1](题目是A[0],从0开始的,我是从1开始的),则这个炮弹无效,我用A[1]判断该弹有没有效,却忘记他也会变了,所以加个判断r==2时候,A[1]+1就行了。
    #include<iostream>    #include<cstring>    #include<cstdlib>    #include<algorithm>    #include<cctype>    #include<cmath>    #include<ctime>    #include<string>    #include<stack>    #include<deque>    #include<queue>    #include<list>    #include<set>    #include<map>    #include<cstdio>    #include<limits.h>    #define MOD 1000000007    #define fir first    #define sec second    #define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)    #define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)    #define mes(x, m) memset(x, m, sizeof(x))    #define Pii pair<int, int>    #define Pll pair<ll, ll>    #define INF 1e9+7    #define Pi 4.0*atan(1.0)    #define lowbit(x) (x&(-x))    #define lson l,m,rt<<1    #define rson m+1,r,rt<<1|1    typedef long long ll;    typedef unsigned long long ull;    const double eps = 1e-12;    const int maxn = 50010;    using namespace std;    inline int read(){        int x(0),f(1);        char ch=getchar();        while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();        return x*f;    }    int k;    int A[maxn];    int sum[maxn<<2];       int re[maxn<<2];    inline void PushUp(int rt)    {        sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);    }    inline void buildTree(int l,int r,int rt)    {        if(l==r){               sum[rt]=A[l];            re[rt]=++k;            return;        }        int m=(l+r)>>1;        buildTree(lson);        buildTree(rson);        PushUp(rt);     }    inline int update(int wi,int l,int r,int rt)    {           if(l==r){            return re[rt];        }        int m=(l+r)>>1;         int res=0;        if(sum[rt<<1]>=wi){            res=update(wi,lson);        }        else if(sum[rt<<1|1]>=wi){            res=update(wi,rson);            }        return res;     }    inline void update_r(int p,int l,int r,int rt)    {        if(l==r){            sum[rt]+=1;            return;        }        int m=(l+r)>>1;        if(p<=m){            update_r(p,lson);        }        else{            update_r(p,rson);        }        PushUp(rt);    }    void playTree(int l,int r,int rt)    {        if(l==r){            printf("%d\n",sum[rt]);            return;        }        int m=(l+r)>>1;        playTree(lson);        playTree(rson);    }    int main()    {        int M,N,B,r;        k=0;        M=read(),N=read();        for(int i=1;i<=M;++i){            A[i]=read();        }        buildTree(1,M,1);        while(N--){            B=read();            if(B<=A[1]){                continue;            }            else if(B>sum[1]){                continue;            }            else{                r=update(B,1,M,1);                if(2==r){                    A[1]+=1;                }                update_r(r-1,1,M,1);            }        }        playTree(1,M,1);        return 0;    }
0 0
原创粉丝点击