Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

http://codeforces.com/contest/760/problem/E

题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶,

注意,已有操作要按它们的时间顺序进行。

思路:线段树好题啊啊,我们把push当成+1, pop当成-1,按操作的位置建立线段树,那么如何

寻找栈顶呢,我们计算每个点的后缀,栈顶就是下标最大的>0的后缀,我们拿后缀建立线段树,

剩下的就是区间加减法,和求区间最大值啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>

using namespace std;

const int N = 1e5 + 7;
const int M = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +7;

int n, lazy[N << 2], mx[N << 2], a[N];

void pushDown(int rt) {
    if(!lazy[rt]) return;
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    mx[rt << 1] += lazy[rt];
    mx[rt << 1 | 1] += lazy[rt];
    lazy[rt] = 0;
}

void update(int L, int R, int v, int l, int r, int rt) {
    if(l >= L && r <= R) {
        mx[rt] += v;
        lazy[rt] += v;
        return;
    }

    int mid = l + r >> 1;
    pushDown(rt);

    if(L <= mid) update(L, R, v, l, mid, rt << 1);
    if(R > mid) update(L, R, v, mid + 1, r, rt << 1 | 1);
    mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}

int query(int l, int r, int rt) {

    if(mx[rt] <= 0) return -1;
    if(l == r) return l;
    int mid = l + r >> 1;
    pushDown(rt);
    if(mx[rt << 1 | 1] > 0) return query(mid + 1, r, rt << 1 | 1);

    else return query(l, mid, rt << 1);
}

int main(){
    scanf("%d", &n);
    int T = n;
    while(T--) {
        int op, id, x;
        scanf("%d%d", &id, &op);
        if(op == 1) {
            scanf("%d", &x);
            a[id] = x;
            update(1, id, 1, 1, n, 1);
        } else {
            update(1, id, -1, 1, n, 1);
        }

        int idx = query(1, n, 1);
        if(idx == -1) puts("-1");
        else printf("%d\n", a[idx]);
    }
    return 0;
}

/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/9222251.html

时间: 2024-08-27 02:19:38

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题的相关文章

8VC Venture Cup 2016 - Final Round (Div2) E

贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define LL long long using namespace std; const int MAXN = 200050; int d,

codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre

C. Lieges of Legendre 题意:给n,m表示有n个为2的倍数,m个为3的倍数:问这n+m个数不重复时的最大值 最小为多少? 数据:(0 ≤ n, m ≤ 1 000 000, n + m > 0) ps:很水的题,主要是策略: 思路:由于里面每隔6就会重复一次,不好直接模拟,并且模拟的效率很低,那就二分吧!二分即上界为2单独的最大倍数与3单独时的最大倍数之和,下界为前面二者的max;之后利用判断是否mid/2 >= n && mid/3 >= m &am

Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

F. Group Projects There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th stude

8VC Venture Cup 2016 - Elimination Round

在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, int &y, char ch) { if (ch == 'U') x--; if (ch == 'D') x++; if (ch == 'L') y--; if (ch == 'R') y++; } int main(void) { int n; scanf ("%d", &

8VC Venture Cup 2016 - Elimination Round E. Simple Skewness(枚举+三分)

题目链接:点击打开链接 题意:给你n个数, 要求选若干个数, 使得这些数的平均数减去中位数尽量大. 思路:由于该题没有顺序问题, 排好序之后我们可以枚举中位数, 可以证明, 奇数个数一定比偶数优,然后三分中位数左右区间长度x(数的个数), 在中位数的右边选最大的x个数, 在左边也选最大的x个, 这样, 随着区间长度的增加, 平均数将先增大后减小, 或者一直减小,或者一直增大. 为什么呢? 假设第一次的区间长度是1, 那么我们选择了两边最大的两个数, 假设他们加起来取平均大于中位数, 那么对答案有

Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) D. Peculiar apple-tree

D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained i

cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) A. Contest for Robots(思维题)

Polycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, and the score of each robot in the competition is cal

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) 简单解析

A题 输入两个长度为n的数组a,b,(其中第i项是1的话代表有,0代表没有, 要求a比b数组大,对于每一项都有对应的价值pi, 尽可能控制每一项最小) 输出 要求出使得a数组比b大的pi中的最大项 思路:相同情况的就抵消掉(不许考虑),只需要考虑1.b有,a没有;2.  a有,b没有,两种情况,对于所有1情况都将对应的pi设置为1,计算总和,然后平分在情况2中 1 #include <iostream> 2 #include <bits/stdc++.h> 3 4 using na