codeforces E. Bombs

在这里插入图片描述

题目

题意:

给你一个序列 P P P和一个序列 Q Q Q,Q代表是炸弹的位置,在序列 P P P中如果这个位置没有炸弹,那就把 P P P放进 A A A中,否则的话,先放进 A A A再把 A A A中最大值删去,问每个位置的 A A A的最大的数值是多少。

思路:

我们看如果看 Q Q Q序列的话,会发现每次都是某个点出现一个炸弹,也就是每次都要更新这些点,如果这个点是炸弹的话,那么我们就将这个点的权值变成 − 1 -1 1,然后我们看 n o d e [ r t ] . M a x = m a x ( n o d e [ r t < < 1 ] . M a x + n o d e [ r t < < 1 ∣ 1 ] . s u m , n o d e [ r t < < 1 ∣ 1 ] . M a x ) ; node[rt].Max = max(node[rt << 1].Max + node[rt << 1 | 1].sum, node[rt << 1 | 1].Max); node[rt].Max=max(node[rt<<1].Max+node[rt<<11].sum,node[rt<<11].Max);这句话,很重要的一句,意思就是如果更新的左孩子的话,那么对右边是没有影响的,所以最后得到的最大值肯定只会是更大(左边的点更新,本来对删除最大值是没有影响的),但是如果是右孩子发生了变化,那么 n o d e [ r t < < 1 ∣ 1 ] . s u m , n o d e [ r t < < 1 ∣ 1 ] . M a x node[rt << 1 | 1].sum,node[rt << 1 | 1].Max node[rt<<11].sumnode[rt<<11].Max均发生了变化,因为刚开始的时候,我们会把最大值的点变成 1 1 1,也就是说加入会出现最后的最大值 < = 0 <=0 <=0的情况就是此时的最大值的点可能在更新的点左边,这样更新最大值可能出现 m a x ( 0 , − 1 ) max(0, -1) max(0,1)这样的情况,记住我们每次更新的是点!。举个例子如果最大值是第4个点,但是炸弹在第3个点的话,那么 n o d e [ r t ] . M a x = m a x ( n o d e [ r t < < 1 ] . M a x + n o d e [ r t < < 1 ∣ 1 ] . s u m , n o d e [ r t < < 1 ∣ 1 ] . M a x ) ; node[rt].Max = max(node[rt << 1].Max + node[rt << 1 | 1].sum, node[rt << 1 | 1].Max); node[rt].Max=max(node[rt<<1].Max+node[rt<<11].sum,node[rt<<11].Max);可能就是 n o d e [ r t ] . M a x = m a x ( − 1 + 1 , 1 ) node[rt].Max = max(-1+1, 1) node[rt].Max=max(1+1,1),反之就是 n o d e [ r t ] . M a x = m a x ( 1 − 1 , − 1 ) node[rt].Max = max(1-1,-1) node[rt].Max=max(11,1)或者就是 m a x ( 1 − 1 , 0 ) max(1-1,0) max(11,0)等等,但是最大值不可能是 1 1 1了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 3e5 + 10;
struct NODE {
    int l;
    int r;
    int Max;
    int sum;
    int mid () {
        return (l + r) >> 1;
    }
};
NODE node[maxn << 2];
void BuildTree(int l, int r, int rt) {
    node[rt].l = l;
    node[rt].r = r;
    node[rt].sum = 0;
    node[rt].Max = 0;
    if (l == r) return ;
    int mid = node[rt].mid();
    BuildTree(l, mid, rt << 1);
    BuildTree(mid + 1, r, rt << 1 | 1);
}
void pushup(int rt) {
    node[rt].sum = node[rt << 1].sum + node[rt << 1 | 1].sum;
    node[rt].Max = max(node[rt << 1].Max + node[rt << 1 | 1].sum, node[rt << 1 | 1].Max);
}
void updata(int pos, int val, int rt) {
    if (node[rt].l == pos && node[rt].r == pos) {
        node[rt].sum += val;
        node[rt].Max = max(0, node[rt].sum);
        return ;
    }
    int mid = node[rt].mid();
    if (pos <= mid) updata(pos, val, rt << 1);
    else updata(pos, val, rt << 1 | 1);
    pushup(rt);
}
int go[maxn], p[maxn], q[maxn];
int main() {
    int n;
    read(n);
    BuildTree(1, n, 1);
    for (int i = 1; i <= n; i++) read(p[i]), go[p[i]] = i;
    for (int i = 1; i <= n; i++) read(q[i]);
    int now = n;
    for (int i = 1; i <= n; i++) {
        while (node[1].Max <= 0) {
            updata(go[now], 1, 1);
            now--;
        }
        out(now + 1);
        putchar(' ');
        updata(q[i], -1, 1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值