zoukankan      html  css  js  c++  java
  • codeforces 374D. Inna and Sequence 线段树

    题目链接

    给m个数, n个操作, 一个数列, 初始为空。一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生。

    求最后的数列。

    用线段树, 因为最多只有n个操作, 也就是说最后的01串最大长度为n, 那么可以用一个变量now表示当前插入的话应该插入到哪个位置, 每插入一个数, now就加1,并且now最终不会超过n。

    删除操作的话, 递归的进行, 如果sum[rt<<1]大于要删除的下标那么就往左儿子递归, 反之往右儿子, 递归到叶子节点的时候将sum[rt]置为0。 具体看代码。

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 1e6+5;
    int sum[maxn<<2], val[maxn], a[maxn], now = 1;
    void pushUp(int rt) {
        sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    }
    void update(int p, int x, int l, int r, int rt) {
        if(l == r) {
            sum[rt] = 1;
            val[l] = x;
            return ;
        }
        int m = l+r>>1;
        if(p<=m)
            update(p, x, lson);
        else
            update(p, x, rson);
        pushUp(rt);
    }
    void remove(int p, int l, int r, int rt) {
        if(l == r) {
            sum[rt] = 0;
            return ;
        }
        int m = l+r>>1;
        if(sum[rt<<1]>=p) {
            remove(p, lson);
        } else {
            remove(p-sum[rt<<1], rson);
        }
        pushUp(rt);
    }
    void output(int l, int r, int rt) {
        if(l == r) {
            printf("%d", val[l]);
            return ;
        }
        int m = l+r>>1;
        if(sum[rt<<1])
            output(lson);
        if(sum[rt<<1|1])
            output(rson);
    }
    int main()
    {
        int n, m, sign;
        scanf("%d%d", &n, &m);
        for(int i = 0; i<m; i++) {
            scanf("%d", &a[i]);
        }
        for(int j = 0; j<n; j++) {
            scanf("%d", &sign);
            if(sign>=0) {
                update(now++, sign, 1, n+2, 1);
            } else {
                for(int i = 0; i<m; i++) {
                    if(a[i]-i>sum[1])           //这里注意是a[i]-i, 因为之前已经删了i个数
                        break;
                    remove(a[i]-i, 1, n+2, 1);
                }
            }
        }
        if(sum[1]) {
            output(1, n+2, 1);
        } else {
            puts("Poor stack!");
        }
        return 0;
    }
  • 相关阅读:
    ASP.NET MVC 与 Web Forms
    去除两端margin的方法
    Media Queries之Respond.js
    ECMAScript5严格模式
    用rem设置文字大小
    BFC与hasLayout
    快速把项目部署到webLogic上
    判断一个坐标点是否在不规则多边形内部的算法
    Git 工作流的正确打开方式
    Java设计模式六大原则
  • 原文地址:https://www.cnblogs.com/yohaha/p/5093558.html
Copyright © 2011-2022 走看看