题解:P16497 东风归故里,托此报微酬 / ARIS0_0 - 42

· · 题解

怎么都会 STL 和总增删个数分析,那我只会暴力数据结构不会这两个怎么办。

题中的「春风拂面」的位置就是非严格前缀最大值位置。

设 b_i 表示位置 i 是不是非严格前缀最大值位置,那么原题的操作就相当于每次单点修改后查询 \displaystyle \sum_{i = 1}^{n} b_i 的值。

那么每次更新 a_p 时,找到 p 左边满足更新前 i \le p \wedge b_i = 1 的最大的 i,可以用线段树二分找。

如果更新 a_p 后仍然有 a_i > a_p,那么说明更新后 p 依然不是非严格前缀最大值位置,就什么都不用做。

否则说明更新后 p 成为了非严格前缀最大值位置。

用线段树二分找到满足 j > p \wedge a_p \le a_j 的最小的 j,容易发现 j 也是非严格前缀最大值位置,但是 p + 1, p + 2, \dots, j - 1 都不再是非严格前缀最大值位置,用线段树把 b_{p + 1}, b_{p + 2}, \dots, b_{j - 1} 区间覆盖为 0 就好。

全程只用了一个线段树,时间复杂度是 O(n \log n) 但是常数有点大。

#include <cstdio>
template <typename _T> inline _T max(const _T &x, const _T &y) { return x > y ? x : y; }
namespace FastIn // 快读模板 qwq
{
    const int S = 4e6 + 7;
    char *p1, *p2, buf[S];
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, S, stdin), p1 == p2) ? EOF : *p1++)
    template <typename _T>
    inline void read(_T &x)
    {
        x = 0;
        char c = gc();
        while (c < '0' || c > '9')
            c = gc();
        for (; c >= '0' && c <= '9'; c = gc())
            x = (x << 3) + (x << 1) + (c & 15);
    }
    #undef gc
}
using FastIn::read;
const int MAXN = 2e5 + 7;
int n, m, c, ans;
long long a[MAXN];
namespace SgT
{
    struct SgTNode
    {
        bool tag; int sum; long long mx; // tag 是把 b 区间覆盖为 0 的标记,sum 是区间内 b 的和,mx 是区间内 a 的最大值 qwq
        inline void apltag() { sum = 0, tag = true; } // b 区间覆盖为 0,把 sum 置 0,tag 置 true qwq
    } tr[MAXN << 2];
    #define ls rt << 1
    #define rs (rt << 1) | 1
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, (rt << 1) | 1
    inline void pushup(const int &rt) { tr[rt].sum = tr[ls].sum + tr[rs].sum, tr[rt].mx = max(tr[ls].mx, tr[rs].mx); }
    inline void pushdown(const int &rt) { tr[rt].tag && (tr[ls].apltag(), tr[rs].apltag(), tr[rt].tag = false); }
    inline void updatepos(const int &pos, const int &l, const int &r, const int &rt) // 把 pos 位置的 b 置 true qwq
    {
        if (l == r)
            return (void)(tr[rt].sum = 1);
        int mid = (l + r) >> 1;
        pushdown(rt), pos <= mid ? updatepos(pos, lson) : updatepos(pos, rson), pushup(rt);
    }
    inline void updateval(const int &pos, const long long &val, const int &l, const int &r, const int &rt) // 把 pos 位置的 a 置 val qwq
    {
        if (l == r)
            return (void)(tr[rt].mx = val);
        int mid = (l + r) >> 1;
        pushdown(rt), pos <= mid ? updateval(pos, val, lson) : updateval(pos, val, rson), pushup(rt);
    }
    inline void updateclear(const int &L, const int &R, const int &l, const int &r, const int &rt) // 把 [L, R] 区间内的 b 置 false qwq
    {
        if (L <= l && r <= R)
            return tr[rt].apltag();
        int mid = (l + r) >> 1;
        pushdown(rt);
        if (L <= mid)
            updateclear(L, R, lson);
        if (R > mid)
            updateclear(L, R, rson);
        pushup(rt);
    }
    inline int querymax(const int &L, const int &R, const int &l, const int &r, const int &rt) // 查询区间 a 最大值 qwq (但是好像没用,不知道为什么要写这个 qwq)
    {
        if (L <= l && r <= R)
            return tr[rt].mx;
        int mid = (l + r) >> 1, res = 0;
        pushdown(rt);
        if (L <= mid)
            res = max(res, querymax(L, R, lson));
        if (R > mid)
            res = max(res, querymax(L, R, rson));
        return res;
    }
    inline int queryprev(const int &pos, const int &l, const int &r, const int &rt) // 查询 pos 前面离 pos 最近的 b 为 true 的位置 qwq
    {
        if (l > pos || !tr[rt].sum)
            return -1;
        if (l == r)
            return l;
        int mid = (l + r) >> 1;
        if (pos > mid && tr[rs].sum)
        {
            int tmp = queryprev(pos, rson);
            if (tmp != -1)
                return tmp;
        }
        return queryprev(pos, lson);
    }
    inline int querynext(const int &pos, const long long &val, const int &l, const int &r, const int &rt) // 查询 pos 后面第一个 a 大于 val 的位置 qwq
    {
        if (r < pos || tr[rt].mx < val)
            return -1;
        if (l == r)
            return l;
        int mid = (l + r) >> 1;
        if (pos <= mid && tr[ls].mx >= val)
        {
            int tmp = querynext(pos, val, lson);
            if (tmp != -1)
                return tmp;
        }
        return querynext(pos, val, rson);
    }
    #undef ls
    #undef rs
    #undef lson
    #undef rson
}
int main()
{
    read(n), read(m), read(c);
    long long premax = 0;
    for (int i = 1; i <= n; ++i)
        read(a[i]), a[i] >= premax && (SgT::updatepos(i, 0, n + 1, 1), premax = a[i]), SgT::updateval(i, a[i], 0, n + 1, 1);
    SgT::updatepos(0, 0, n + 1, 1), SgT::updatepos(n + 1, 0, n + 1, 1), SgT::updateval(n + 1, a[n + 1] = 0x3f3f3f3f3f3f3f3fll, 0, n + 1, 1);
    for (int t = 1, p, x, nxtp, lst = 0; t <= m; ++t)
    {
        read(p), read(x), p ^= c * (lst % 3), SgT::updateval(p, a[p] += x, 0, n + 1, 1);
        if (a[p] < a[SgT::queryprev(p, 0, n + 1, 1)]) // 如果 p 更新后仍然不是前缀最大值就直接什么都不干就好啦 qwq
        {
            printf("%d\n", lst = SgT::tr[1].sum - 2); // -2 的原因是减去 0 和 n + 1 qwq
            continue;
        }
        SgT::updatepos(p, 0, n + 1, 1); // 否则的话 p 就变成了前缀最大值了,把 p 位置的 b 置 true qwq
        nxtp = SgT::querynext(p + 1, a[p], 0, n + 1, 1); // 查询 p 后面第一个 a 大于 a[p] 的位置 nxtp qwq
        SgT::updateclear(p + 1, nxtp - 1, 0, n + 1, 1); // 把 b[p + 1], b[p + 2], ..., b[nxtp - 1] 都设为 false qwq
        printf("%d\n", lst = SgT::tr[1].sum - 2);
    }
    return 0;
}
// qwq
// 42