Greedy Shopping(线段树)

题目链接: Greedy Shopping

大致题意

有n个商店, 去第i个商店购买物品需要花费a[i]元. 其中保证a序列为非增序列

两种操作:
① 1 x y 将[1, x] 区间的a[i]设置为 a[i] = max(a[i], y);
② 2 x y 你有y元钱, 从编号为x的商店出发, 依次遍历所有商店直至到n号商店为止, 如果能购买当前商店的物品则购买. 问你能买多少物品.

解题思路

这个题目有个很重要的性质, 最初时a序列是非增的, 那么考虑到修改操作是对于[1, x]区间取max(a[i], y), 因此我们可以得出结论, 无论何时我们的a序列也都应当是非增的.

首先分析操作1, 相当于对[1, x]区间进行修改, 那么对于每一个点而言, 如果当前点的a[i] > y, 则就不需要进行修改. 又因为整个区间是非增的, 因此对于不需要修改的区间和需要修改的区间, 如果存在那么一定是连续的两部分.

这样我们就很容易想到通过线段树去维护区间, 则需要维护一个区间的最大值和区间的最小值. 当我们当前所处的区间已经涵盖在需要修改的区间内部时:
​ ①当前区间的最小值已经比y大, 则本区间不需要修改.
​ ②当前区间的最大值比y小, 则应当修改当前区间.

其次再分析操作2, 相当于区间[x, n]进行询问. 且询问的情况应该是先左子树, 而后右子树. 因此我们应当维护当前区间和. 如果当前所处的区间包含在询问区间内部, 则我们可以直接买完整个区间. 否则应当先购买左子树, 再购买右子树.

AC代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 2E5 + 10;
int n, m;
int w[N];
struct node {
    int l, r;
    ll val; int fmin, fmax;
    ll lazy;
}t[N << 2];

void pushdown(node& op, ll lazy) {
    op.val = lazy * (op.r - op.l + 1);
    op.fmin = lazy, op.fmax = lazy;
    op.lazy = lazy;
}
void pushdown(int x) {
    if (!t[x].lazy) return;
    pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
    t[x].lazy = 0;
}

void pushup(node& p, node& l, node& r) {
    p.val = l.val + r.val;
    p.fmin = min(l.fmin, r.fmin), p.fmax = max(l.fmax, r.fmax);
}
void pushup(int x) { pushup(t[x], t[x << 1], t[x << 1 | 1]); }

void build(int l, int r, int x = 1) {
    if (l == r) { t[x] = { l, r, w[l], w[l], w[l], 0 }; return; }
    t[x] = { l, r, 0, 0, 0, 0 };
    int mid = l + r >> 1;
    build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
    pushup(x);
}

void modify(int l, int r, int c, int x = 1) {
    if (l <= t[x].l && r >= t[x].r) {
        if (t[x].fmin >= c) return; 
        if (t[x].fmax < c) { pushdown(t[x], c); return; }
    }
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    if (l <= mid) modify(l, r, c, x << 1);
    if (r > mid) modify(l, r, c, x << 1 | 1);
    pushup(x);
}

int ask(int l, int r, int& have, int x = 1) { //have表示当前的钱
    if (l <= t[x].l && r >= t[x].r) {
        if (have < t[x].fmin) return 0;
        if (have >= t[x].val) { have -= t[x].val; return t[x].r - t[x].l + 1; }
        if (t[x].l == t[x].r) return 0; //此时已经为叶子结点, 不能再向下递归
    }
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    int res = 0;
    if (l <= mid) res += ask(l, r, have, x << 1);
    if (r > mid) res += ask(l, r, have, x << 1 | 1);
    return res;
}

int main()
{
    cin >> n >> m;
    rep(i, n) scanf("%d", &w[i]);
    build(1, n);
    while (m--) {
        int op, x, y; scanf("%d %d %d", &op, &x, &y);
        if (op == 1) modify(1, x, y);
        else printf("%d\n", ask(x, n, y));
    }
    return 0;
}

呜呜呜, 太菜了TwT

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值