线段树

HDU 5306 Gorgeous Sequence

维护一个数组,支持以下几种操作

  • 给出l,r,xl,r,x,对于i[l,r]i\in[l,r]aia_i变为 min(ai,x)min(a_i,x)
  • 给出l,rl,r,对于i[l,r]i\in[l,r],询问区间内aia_i的最大值。
  • 给出l,rl,r,询问 i=lrai\sum_{i=l}^r a_i

我们可以用线段树来维护这个数组,线段树维护区间最大值 maxmax ,严格次大值 secsec最大值的个数cntcnt,和区间和sumsum.若使区间[l,r][l,r]xxminmin,先递归定位到这个区间。然后:

  • max<xmax<x,直接返回
  • sec<x<maxsec<x<max,我们将此时区间最大值改为xx,此时区间的和应该减去cnt(maxx)cnt*(max-x),同时打上标记.
  • 否则递归到左右儿子继续处理

整体时间复杂度O((n+m)logn)O((n+m)log_n),不会证。
对于标记我们不需要另开数组记录,当左右儿子的最值大于父亲节点的最值时,说明这个父亲节点一定被修改过,这时处理左右儿子即可。另外还要明确我们修改的区间一定是满足sec<x<maxsec<x<max,因此对于区间和sumsum的修改只和最大值有关。
这题时间卡的也比较紧


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e6 + 10;
typedef long long ll;
struct tree{
    ll sum, mx, se, cnt;
}T[N<<2];
ll add[N << 2],a[N];
int n, m;
void pushup(int rt)
{
    int lc = rt << 1, rc = rt << 1 | 1;
    if (T[lc].mx > T[rc].mx)
    {
        T[rt].mx = T[lc].mx;
        T[rt].cnt = T[lc].cnt;
        T[rt].se = max(T[lc].se, T[rc].mx);
    }
    else if (T[lc].mx < T[rc].mx)
    {
        T[rt].mx = T[rc].mx;
        T[rt].cnt = T[rc].cnt;
        T[rt].se = max(T[rc].se, T[lc].mx);
    }
    else
    {
        T[rt].mx = T[rc].mx;
        T[rt].cnt = T[rc].cnt+T[lc].cnt;
        T[rt].se = max(T[rc].se, T[lc].se);
    }
    T[rt].sum = T[lc].sum + T[rc].sum;
}
void pushdown(int rt)
{
    int lc = rt << 1, rc = rt << 1 | 1;
    if (T[lc].mx > T[rt].mx){
        T[lc].sum -= T[lc].cnt*(T[lc].mx - T[rt].mx);
        T[lc].mx = T[rt].mx;
    }
    if (T[rc].mx > T[rt].mx)
    {
        T[rc].sum -= T[rc].cnt*(T[rc].mx - T[rt].mx);
        T[rc].mx = T[rt].mx;
    }
}
void build(int rt, int l, int r)
{
    if (l == r){
        T[rt].sum = T[rt].mx = a[l];
        T[rt].cnt = 1;
        T[rt].se = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushup(rt);
}
void change(int rt, int l, int r, int x, int y, int val)
{
    if (T[rt].mx <= val) return;
    if (x <= l&&r <= y&&val>T[rt].se)
    {
        T[rt].sum -= (T[rt].mx - val)*T[rt].cnt;
        T[rt].mx = val;
        add[rt] = val;
        return;
    }
     pushdown(rt);
    int mid = (l + r) >> 1;
    if (x <= mid) change(rt << 1, l, mid, x, y, val);
    if (y > mid)  change(rt << 1 | 1, mid + 1, r, x, y, val);
    pushup(rt);
}
ll qs(int rt, int l, int r, int x, int y)
{
    if (x <= l&&r <= y) return  T[rt].sum;
     pushdown(rt);
    int mid = (l + r) >> 1;
    ll ans = 0;
    if (x <= mid) ans += qs(rt << 1, l, mid, x, y);
    if (y > mid) ans += qs(rt << 1 | 1, mid + 1, r, x, y);
    return ans;
}
ll qmx(int rt, int l, int r, int x, int y)
{
    if (x <= l&&r <= y) return  T[rt].mx;
    pushdown(rt);
    int mid = (l + r) >> 1;
    ll ans1 = 0,ans2=0;
    if (x <= mid) ans1 = qmx(rt << 1, l, mid, x, y);
    if (y > mid) ans2 = qmx(rt << 1 | 1, mid + 1, r, x, y);
    return max(ans1, ans2);
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        build(1, 1, n);
        while (m--)
        {
            int op, x, y, val;
            scanf("%d%d%d", &op, &x, &y);
            if (op == 0){
                scanf("%d", &val);
                change(1, 1, n, x, y, val);
            }
            else if (op == 1){
                printf("%lld\n", qmx(1, 1, n, x, y));
            }
            else if (op == 2){
                printf("%lld\n", qs(1, 1, n, x, y));
            }
        }
    }
        return 0;
}