首页 > 其他分享 >P2572 [SCOI2010] 序列操作 题解

P2572 [SCOI2010] 序列操作 题解

时间:2024-01-16 20:12:25浏览次数:36  
标签:curr int 题解 mid ans const include P2572 SCOI2010

题解:序列操作

比较综合的 ds 题,综合了线段树常见的几种操作:维护最大子段和、区间翻转、区间求和、区间覆盖

维护子段和常见的我们维护三类东西:

前缀最长连续段、后缀最长连续段、当前区间上的最大子段和。

在 pushUp 时,对于一个区间的前后缀最值首先等于左右子树的最长前后缀,如果填满了一棵子树以后会得到:\(pre_{curr}=pre_{left}+pre_{right}\),当 \(pre_{left}==len_{left}\) 就会加上右子树的前缀最值。后缀同理,而答案显然为左右区间的答案加上上图中绿色的 \(suf_{left}+pre_{right}\) 三者中取最值。

现在,来考虑下每种标记和操作之间的影响:

  1. 对于覆盖标记,如果前面已经有翻转标记了,显然翻转标记需要直接清空。

  2. 对于翻转标记和最大子段和的标记,由于只涉及到 \(0/1\) 的翻转,所以我们在维护一组最大子段和标记,其中 \(0\) 是作为贡献的存在,即前缀最长的 \(0\),后缀最长的 \(0\),当前区间最大的 \(0\) 的段。

前后缀受到翻转影响,就直接交换两组标记即可,而最长子段和显然变成了最长 \(0\) 段和。剩余的就是常规的最大子段和查询等常规操作了,不熟的可以去温习下,关于最大子段和的查询实际上是维护一个线段树节点不断地 \(merge\) 答案节点区间形成最终答案节点,状态量的合并。剩余见代码注释即可。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize(2)

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 1e5 + 10;

struct Node
{
    int sum;//区间和
    int preMax, nxtMax, Max;//区间最大子段和的三个标记,前后缀最长和最大子段和
    int len;//区间长度
    int rev;//翻转标记
    int cov;//覆盖标记,-1表示没有覆盖
    int preZero, nxtZero;//前后缀最长0段
    int MaxZero;//最长0段
    Node() = default;
} node[N << 2];

#define sum(x) node[x].sum
#define preMax(x) node[x].preMax
#define nxtMax(x) node[x].nxtMax
#define Max(x) node[x].Max
#define len(x) node[x].len
#define rev(x) node[x].rev
#define cov(x) node[x].cov
#define preZero(x) node[x].preZero
#define nxtZero(x) node[x].nxtZero
#define MaxZero(x) node[x].MaxZero

inline void push_up(const int curr)
{
    sum(curr) = sum(ls(curr)) + sum(rs(curr));
    preMax(curr) = preMax(ls(curr)), nxtMax(curr) = nxtMax(rs(curr));
    if (preMax(curr) == len(ls(curr)))
        preMax(curr) += preMax(rs(curr));
    if (nxtMax(curr) == len(rs(curr)))
        nxtMax(curr) += nxtMax(ls(curr));
    Max(curr) = max({Max(ls(curr)),Max(rs(curr)),nxtMax(ls(curr)) + preMax(rs(curr))});
    preZero(curr) = preZero(ls(curr)), nxtZero(curr) = nxtZero(rs(curr));
    if (preZero(curr) == len(ls(curr)))
        preZero(curr) += preZero(rs(curr));
    if (nxtZero(curr) == len(rs(curr)))
        nxtZero(curr) += nxtZero(ls(curr));
    MaxZero(curr) = max({MaxZero(ls(curr)),MaxZero(rs(curr)),nxtZero(ls(curr)) + preZero(rs(curr))});
}

//区间覆盖以后会影响标记
inline void Change(const int curr, const int val)
{
    if (val)
    {
        sum(curr) = preMax(curr) = nxtMax(curr) = Max(curr) = len(curr);
        preZero(curr) = nxtZero(curr) = MaxZero(curr) = 0;
    }
    else
    {
        preZero(curr) = nxtZero(curr) = MaxZero(curr) = len(curr);
        sum(curr) = preMax(curr) = nxtMax(curr) = Max(curr) = 0;
    }
}
//区间翻转就两组标记对换
inline void Rev(const int curr)
{
    swap(preMax(curr),preZero(curr));
    swap(nxtMax(curr),nxtZero(curr));
    swap(Max(curr),MaxZero(curr));
    sum(curr) = len(curr) - sum(curr);
}
//先考虑覆盖标记再考虑翻转标记
inline void push_down(const int curr)
{
    if (cov(curr) != -1)
    {
        rev(ls(curr)) = rev(rs(curr)) = 0;
        cov(ls(curr)) = cov(rs(curr)) = cov(curr);
        Change(ls(curr),cov(curr));
        Change(rs(curr),cov(curr));
        cov(curr) = -1;
    }
    if (rev(curr))
    {
        rev(ls(curr)) ^= 1, rev(rs(curr)) ^= 1;
        Rev(ls(curr)), Rev(rs(curr));
        rev(curr) = 0;
    }
}

int n;
int a[N];
//建数
inline void Build(const int curr, const int l = 1, const int r = n)
{
    len(curr) = r - l + 1;
    cov(curr) = -1;
    const int mid = l + r >> 1;
    if (l == r)
    {
        Change(curr, a[l]);
        return;
    }
    Build(ls(curr), l, mid);
    Build(rs(curr), mid + 1, r);
    push_up(curr);
}
//覆盖
inline void Cover(const int curr, const int l, const int r, const int val, const int s = 1, const int e = n)
{
    const int mid = s + e >> 1;
    if (l <= s and e <= r)
    {
        rev(curr) = 0;
        cov(curr) = val;
        Change(curr, val);
        return;
    }
    push_down(curr);
    if (l <= mid)Cover(ls(curr), l, r, val, s, mid);
    if (r > mid)Cover(rs(curr), l, r, val, mid + 1, e);
    push_up(curr);
}
//翻转
inline void Reverse(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
    const int mid = s + e >> 1;
    if (l <= s and e <= r)
    {
        rev(curr) ^= 1;
        Rev(curr);
        return;
    }
    push_down(curr);
    if (l <= mid)Reverse(ls(curr), l, r, s, mid);
    if (r > mid)Reverse(rs(curr), l, r, mid + 1, e);
    push_up(curr);
}

//查询节点状态
inline Node Query(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
    const int mid = s + e >> 1;
    if (l <= s and e <= r)return node[curr];
    push_down(curr);
    auto ans = Node();
    if (r <= mid)ans = Query(ls(curr), l, r, s, mid);
    else if (l > mid)ans = Query(rs(curr), l, r, mid + 1, e);
    else
    {
        //合并状态量
        const auto left = Query(ls(curr), l, r, s, mid);
        const auto right = Query(rs(curr), l, r, mid + 1, e);
        ans.sum = left.sum + right.sum;
        ans.len = left.len + right.len;
        ans.preMax = left.preMax;
        ans.nxtMax = right.nxtMax;
        if (ans.preMax == left.len)ans.preMax += right.preMax;
        if (ans.nxtMax == right.len)ans.nxtMax += left.nxtMax;
        ans.Max = max({left.Max, right.Max, left.nxtMax + right.preMax});
    }
    return ans;
}

int q;

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)cin >> a[i];
    Build(1);
    while (q--)
    {
        int op, l, r;
        cin >> op >> l >> r;
        l++, r++;
        if (op == 0 or op == 1)Cover(1, l, r, op);
        else if (op == 2)Reverse(1, l, r);
        else
        {
            auto ans = Query(1, l, r);
            cout << (op == 3 ? ans.sum : ans.Max) << endl;
        }
    }
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为 \ O((n+m)\log{n}) \]

标签:curr,int,题解,mid,ans,const,include,P2572,SCOI2010
From: https://www.cnblogs.com/Athanasy/p/17968452

相关文章

  • P6054 题解
    blog。网络流——最小割。每个选手做某一套题的期望奖励固定,计算方式参考样例解释。这个假期望被去掉了。发现是典型的「\(m\)种强制选一」问题。考虑每个人都建一条链,跑最小割,每条链必定割\(\ge1\)条边,割哪条边就表示选哪套题。code,时间复杂度\(O(\text{能过})\)。......
  • 洛谷P10058 题解
    这种翻转的题明显已经做烂了好吧……首先显而易见,翻转偶数次对结果没有影响,只需要考虑奇数次翻转的情况。由于是整体移动的操作,可以抓住一个点来移动,然后还原出原来的序列。需要注意的是字符串是环形移动,因此如果当前点的位置大于字符串长度,要对字符串的长度进行取余操作。写......
  • Atcoder Beginner Contest 330 题解
    AtCoderBeginnerContest330题解A-CountingPasses签到voidShowball(){intn,l;cin>>n>>l;intcnt=0;for(inti=0;i<n;i++){intx;cin>>x;cnt+=(x>=l);}cout<<cnt<<endl;}B-Minimize......
  • CF1437F Emotional Fishermen 题解
    题意:有\((n\le5000)\)个渔民,每个渔民钓了一条重\(a_i\)的鱼,渔民按任意顺序展示他们的鱼。若当前渔民的鱼的重量为\(x\),之前展示过的鱼的最大重量\(y\)。一个排列满足条件当且仅当对于每个\(x\),满足\(2y\lex\)或\(2x\ley\)。问有多少个排列满足条件,对\(998244353......
  • ABC336 F Rotation Puzzle 题解
    QuestionABC336FRotationPuzzle给出一个\(H\timesW\)的矩阵,里面填有数字,有一种操作选定一个\((x,y)\)交换\((i+x,j+y)\)和\((H-i+x,W-j+y)\)对于每一个\(1\lei\leH-1,1\lej\leW-1\)问,是否能经过\(20\)次以内的操作使得,最后的矩形变成\((i,j)=((i-1)\t......
  • P1002题解
    思路设\(dp_{i,j}\)表示第\(i\)行\(j\)列卒走到这里有多少种方式。卒是可以向右和下走,所以到这个点只能从左或上来,不难得出转移公式:\(dp_{i,j}=dp_{i-1,j}+dp_{i,j-1}\)。如果马在这个点上或者说马能到这个点上,那么卒不能到这个点,也就是卒到这个点的方式为\(0\)。如......
  • P1003题解
    简单模拟题。思路枚举每一个地毯,因为后面的会覆盖前面的,所以从正序枚举。如果要求的点的坐标在当前地毯上,则将答案赋值为当前地毯编号。最后输出答案。那如果这个点没有地毯呢?答案初始设为\(-1\),这样没有地毯覆盖的话,答案不会改变,这样输出答案就会是\(-1\)。注意:记得赋初......
  • P10058题解
    怎么前三题都是思维题啊。思路总共有三个操作,先不看翻转操作。如果你右移\(x\)位之后,左移\(x\)位,那么就相当于没有操作。这个应该是很好理解的。我们根据上面的结论,能得出以下代码。if(op==">"){cin>>x;f+=x;}elseif(op=="<"){......
  • CF1409D题解
    思路因为数据较大,使用字符串读入。考虑使用贪心。先统计出当前数码之和。然后从低位往高位枚举,看一下把当前位改了之后是否小于等于\(s\)。如果小于的话,则统计出把当前位往后所有位都改为0,\(k\)为多少,求出的\(k\)就是最优解。说明一下为什么要从低位往高位枚举,这样如果成......
  • AT_arc060_c题解
    纪念模拟考考挂。思路首先二分查找出当前点往后走最远能去哪个点,当前点为\(i\),记最远能去的那个点为\(nt_i\)。考虑建一棵树,将\(nt_i\)设为\(i\)点的父节点。暴力的话直接从当前点往上找,找到目标节点看几次就好了。但显然是过不了的。考虑使用倍增优化。设\(g_{i,j}......