zoukankan      html  css  js  c++  java
  • Nikita and stack

    Nikita and stack
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer x on the top of the stack, and operation pop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operation pop() does nothing.

    Nikita made m operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on thei-th step he remembers an operation he made pi-th. In other words, he remembers the operations in order of some permutationp1, p2, ..., pm. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him!

    Input

    The first line contains the integer m (1 ≤ m ≤ 105) — the number of operations Nikita made.

    The next m lines contain the operations Nikita remembers. The i-th line starts with two integers pi and ti (1 ≤ pi ≤ mti = 0 or ti = 1) — the index of operation he remembers on the step i, and the type of the operation. ti equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer xi (1 ≤ xi ≤ 106) — the integer added to the stack.

    It is guaranteed that each integer from 1 to m is present exactly once among integers pi.

    Output

    Print m integers. The integer i should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to i. If the stack is empty after performing all these operations, print -1.

    Examples
    input
    2
    2 1 2
    1 0
    output
    2
    2
    input
    3
    1 1 2
    2 1 3
    3 0
    output
    2
    3
    2
    input
    5
    5 0
    4 0
    3 1 1
    2 1 1
    1 1 2
    output
    -1
    -1
    -1
    -1
    2
    Note

    In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is2. After he remembers the operation pop() which was done before push(2), answer stays the same.

    In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed.

    In the third example Nikita remembers the operations in the reversed order.

    分析:对于已知操作,答案为最右的使得之后push次数=pop次数的push的位置;

       若push=1,pop=-1,即求最右的后缀和为1的位置;

       所以线段树区间修改+最值查询;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    inline ll read()
    {
        ll x=0;int 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 n,m,k,t,val[maxn];
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    struct Node
    {
        int Max, lazy;
    } T[maxn<<2];
    
    void PushUp(int rt)
    {
        T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max);
    }
    
    void PushDown(int L, int R, int rt)
    {
        int mid = (L + R) >> 1;
        int t = T[rt].lazy;
        T[rt<<1].Max += t;
        T[rt<<1|1].Max += t;
        T[rt<<1].lazy +=t;
        T[rt<<1|1].lazy += t;
        T[rt].lazy = 0;
    }
    
    void Update(int l, int r, int v, int L, int R, int rt)
    {
        if(l==L && r==R)
        {
            T[rt].lazy += v;
            T[rt].Max += v;
            return ;
        }
        int mid = (L + R) >> 1;
        if(T[rt].lazy) PushDown(L, R, rt);
        if(r <= mid) Update(l, r, v, Lson);
        else if(l > mid) Update(l, r, v, Rson);
        else
        {
            Update(l, mid, v, Lson);
            Update(mid+1, r, v, Rson);
        }
        PushUp(rt);
    }
    
    int Query(int l, int r, int rt)
    {
        if(l==r)
        {
            return T[rt].Max >0? val[l]:-1;
        }
        int mid = (l + r) >> 1;
        if(T[rt].lazy) PushDown(l, r, rt);
        if(T[rt<<1|1].Max>0) return Query(mid+1, r ,rt<<1|1);
        else return Query(l, mid, rt<<1);
    }
    
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)
        {
            scanf("%d%d",&j,&k);
            if(k==0)
            {
                Update(1,j,-1,1,n,1);
            }
            else
            {
                scanf("%d",&k);
                val[j]=k;
                Update(1,j,1,1,n,1);
            }
            printf("%d
    ",Query(1,n,1));
        }
        return 0;
    }
  • 相关阅读:
    最短路径之spfa
    最短路径之Bellman-Ford——解决负权边
    最短路径之Floyd-Warshall算法
    图上最短路径问题
    它们其实都是图(二分图)
    记忆化结果再利用 进一步探讨递推关系
    leetcode 376. 摆动序列 java
    leetcode 368. 最大整除子集 java
    leetcode 96. 不同的二叉搜索树 java
    leetcode 454. 四数相加 II java
  • 原文地址:https://www.cnblogs.com/dyzll/p/6349997.html
Copyright © 2011-2022 走看看