zoukankan      html  css  js  c++  java
  • codeforces 760E or 759C 【线段树维护后缀和】

    C. 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. Operationpush(x) puts an integerx 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 operationpop() 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 madepi-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. Thei-th line starts with two integerspi andti (1 ≤ pi ≤ m,ti = 0 orti = 1) — the index of operation he remembers on the stepi, and the type of the operation.ti equals0, if the operation is pop(), and 1, is the operation is push(x). If the operation ispush(x), the line also contains the integerxi (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 from1 toi. 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
    

    题意:

    大致是说有个脑残,他忘了自己对一个栈的操作顺序了。现在他只能想起他第几步的操作是什么 (1 : push, 0: pop)。现在问你在他想起来的这前 i 步操做后,栈顶元素是几,栈空输出-1;

    题解:

    从后往前来维护一个操作数的后缀,push +1, pop -1。那么每次从后往前的第一个后缀和 > 0的位置,就应该是当前的答案。用线段树来维护后缀和(区间最大值),每次更新 1 ~ pos (+1 , -1);判断区间最大值是否 > 0,以及获得第一个 > 0的后缀的pos;那么就是区间更新维护最大值的裸题了。

    代码:

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstring>
      4 #include <cstdio>
      5 #include <bitset>
      6 #include <vector>
      7 #include <queue>
      8 #include <stack>
      9 #include <cmath>
     10 #include <list>
     11 #include <set>
     12 #include <map>
     13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
     14 #define per(i,a,b) for(int i = a;i >= b;-- i)
     15 #define mem(a,b) memset((a),(b),sizeof((a)))
     16 #define FIN freopen("in.txt","r",stdin)
     17 #define FOUT freopen("out.txt","w",stdout)
     18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
     19 #define mid ((l+r)>>1)
     20 #define ls (id<<1)
     21 #define rs ((id<<1)|1)
     22 #define N 100005
     23 #define INF 0x3f3f3f3f
     24 #define INFF ((1LL<<62)-1)
     25 typedef long long LL;
     26 using namespace std;
     27 
     28 int n, step, op, x, ans[N];
     29 struct Node{
     30     int lazy, maxn;
     31 }node[N*4];
     32 void pushUp(int id, int l, int r){
     33     node[id].maxn = max(node[ls].maxn, node[rs].maxn);
     34 }
     35 void pushDown(int id, int l, int r){
     36     node[ls].lazy += node[id].lazy;
     37     node[rs].lazy += node[id].lazy;
     38     node[ls].maxn += node[id].lazy;
     39     node[rs].maxn += node[id].lazy;
     40     node[id].lazy = 0;
     41 }
     42 void build(int id, int l, int r){
     43     if(l == r){
     44         node[id].lazy = 0;
     45         node[id].maxn = 0;
     46         return ;
     47     }
     48     build(ls, l, mid);
     49     build(rs, mid+1, r);
     50     node[id].lazy = 0;
     51     pushUp(id, l, r);
     52 }
     53 void update(int id, int l, int r, int ql, int qr, int p){
     54     if(ql == l && qr == r){
     55         node[id].lazy += p;
     56         node[id].maxn += p;
     57         return ;
     58     }
     59     if(node[id].lazy)    pushDown(id, l, r);
     60     if(qr <= mid)    update(ls, l, mid, ql, qr, p);
     61     else if(ql > mid)
     62         update(rs, mid+1, r, ql, qr, p);
     63     else{
     64         update(ls, l, mid, ql, mid, p);
     65         update(rs, mid+1, r, mid+1, qr, p);
     66     }
     67     pushUp(id, l, r);
     68 }
     69 int query(int id, int l, int r){
     70     if(l == r)    return l;
     71     if(node[id].lazy)    pushDown(id, l, r);
     72     
     73     if(node[rs].maxn > 0)
     74         query(rs, mid+1, r);
     75     else
     76         query(ls, l, mid);
     77 }
     78 int main()
     79 {IO;
     80     //FIN;
     81     while(cin >> n){
     82         mem(ans, 0);
     83         build(1, 1, n);
     84         rep(i, 1, n){
     85             cin >> step >> op;
     86             if(op == 1){
     87                 cin >> x;
     88                 ans[step] = x;
     89                 update(1, 1, n, 1, step, 1);
     90             }
     91             else{
     92                 update(1, 1, n, 1, step, -1);
     93             }
     94             if(node[1].maxn > 0){
     95                 int pos = query(1, 1, n);
     96                 cout << ans[pos] << endl;
     97             }
     98             else{
     99                 cout << -1 << endl;
    100             }
    101         }
    102     }
    103     return 0;
    104 }
    View Code
  • 相关阅读:
    senlin __init__() got an unexpected keyword argument 'additional_headers'
    Delphi 全局画点TCanvas.Pixels[X,Y]
    sql server 列修改null 变成not null
    Delphi记录record中的变体
    delphi无边框可拖动窗体
    bootstrap相关资料
    重装windows导致grub损坏
    RabbitMQ的安装(Docker安装)
    RabbitMQ的安装以及使用(Windows环境)
    如果安装rabittmq后,输入http://localhost:15672不出页面的
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351915.html
Copyright © 2011-2022 走看看