20pts 未过样例求助

P2572 [SCOI2010] 序列操作

Melo_DDD @ 2024-09-07 01:16:34

快死了救救我吧救救我吧救救我吧救救我吧救救我吧救救我吧救救我吧

#include <bits/stdc++.h>
#define f(i ,m ,n ,x) for (int i = (m) ;i <= (n) ;i += (x))
#define ls (cur) << 1
#define rs (cur) << 1 | 1  
using namespace std ;
template < typename T > inline void read ( T &x ) {
    x = 0 ;
    bool flag (0) ;
    register char ch = getchar () ;
    while (! isdigit (ch)) {
        flag = ch == '-' ;
        ch = getchar () ;
    }
    while (isdigit (ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48) ;
        ch = getchar () ; 
    }
    flag ? x = -x : 0 ;
}
const int N = 1e5 + 7 ;
int n ,m ,a[N] ;
struct segment_tree {
    int l ,r ,sum ,con1 ,con0 ,lcon1 ,lcon0 ,rcon1 ,rcon0 ,la ,fl ;
    #define l(cur) t[cur].l
    #define r(cur) t[cur].r
    #define sum(cur) t[cur].sum
    #define con1(cur) t[cur].con1
    #define con0(cur) t[cur].con0
    #define lcon1(cur) t[cur].lcon1
    #define lcon0(cur) t[cur].lcon0
    #define rcon1(cur) t[cur].rcon1 
    #define rcon0(cur) t[cur].rcon0
    #define la(cur) t[cur].la
    #define fl(cur) t[cur].fl
    #define len(cur) (t[cur].r - t[cur].l + 1)
    inline void init () {
        l = r = sum = con1 = con0 = lcon1 = lcon0 = rcon1 = rcon0 = la = fl = 0 ;
    }
} t[N << 2] ;
namespace melo {
    inline void pushup (int cur) {
        sum (cur) = sum (ls) + sum (rs) ;
        con1 (cur) = max (con1 (ls) ,max (con1 (rs) ,rcon1 (ls) + lcon1 (rs))) ;
        con0 (cur) = max (con0 (ls) ,max (con0 (rs) ,rcon0 (ls) + lcon0 (rs))) ;
        lcon1 (cur) = lcon1 (ls) ;
        if (lcon1 (ls) == len (ls)) lcon1 (cur) += lcon1 (rs) ;
        lcon0 (cur) = lcon0 (ls) ;
        if (lcon0 (ls) == len (ls)) lcon0 (cur) += lcon0 (rs) ;
        rcon1 (cur) = rcon1 (rs) ;
        if (rcon1 (rs) == len (rs)) rcon1 (cur) += rcon1 (ls) ;
        rcon0 (cur) = rcon0 (rs) ;
        if (rcon0 (rs) == len (rs)) rcon0 (cur) += rcon0 (ls) ;
    }
    inline void pushdown (int cur) {
        if (la (cur) == -1) goto there ;
        if (! la (cur)) {
            t[ls] = (segment_tree) { l (ls) ,r (ls) ,0 ,0 ,len (ls) ,0 ,len (ls) ,0 ,len (ls) ,0 ,0 } ;
            t[rs] = (segment_tree) { l (rs) ,r (rs) ,0 ,0 ,len (rs) ,0 ,len (rs) ,0 ,len (rs) ,0 ,0 } ; 
        } else if (la (cur) == 1){
            t[ls] = (segment_tree) { l (ls) ,r (ls) ,len (ls) ,len (ls) ,0 ,len (ls) ,0 ,len (ls) ,1 ,0 } ;
            t[rs] = (segment_tree) { l (rs) ,r (rs) ,len (rs) ,len (rs) ,0 ,len (rs) ,0 ,len (rs) ,1 ,0 } ;
        } 
        la (cur) = -1 ;
        there : ;
        if (fl (cur)) {
            sum (ls) = len (ls) - sum (ls) ;
            swap (con1 (ls) ,con0 (ls)) ,swap (lcon1 (ls) ,lcon0 (ls)) ,swap (rcon1 (ls) ,rcon0 (ls)) ;
            fl (ls) ^= 1 ;
            sum (rs) = len (rs) - sum (rs) ;
            swap (con1 (rs) ,con0 (rs)) ,swap (lcon1 (rs) ,lcon0 (rs)) ,swap (rcon1 (rs) ,rcon0 (rs)) ;
            fl (rs) ^= 1 ;
            fl (cur) = 0 ;
        }
    }
    inline void build (int cur ,int l ,int r) { 
        if (l == r) {
            t[cur] = (segment_tree) { l ,r ,a[l] ,a[l] ,a[l] ^ 1 ,a[l] ,a[l] ^ 1 ,a[l] ,a[l] ^ 1 ,-1 ,0 } ;
            return ;
        }
        l (cur) = l ,r (cur) = r ,la (cur) = -1 ;
        int mid = l + r >> 1 ;
        melo :: build (ls ,l ,mid) ,melo :: build (rs ,mid + 1 ,r) ;
        melo :: pushup (cur) ;
    }
    inline void all_1 (int cur ,int nowl ,int nowr) {
        if (nowl <= l (cur) && nowr >= r (cur)) {
            t[cur] = (segment_tree) { l (cur) ,r (cur) ,len (cur) ,len (cur) ,0 ,len (cur) ,0 ,len (cur) ,0 ,1 ,0 } ;
            return ; 
        }
        int mid = l (cur) + r (cur) >> 1 ;
        melo :: pushdown (cur) ;
        if (nowl <= mid && nowr >= l (cur)) melo :: all_1 (ls ,nowl ,nowr) ;
        if (nowr > mid && nowl <= r (cur)) melo :: all_1 (rs ,nowl ,nowr) ;
        melo :: pushup (cur) ;
    } 
    inline void all_0 (int cur ,int nowl ,int nowr) {
        if (nowl <= l (cur) && nowr >= r (cur)) {
            t[cur] = (segment_tree) { l (cur) ,r (cur) ,0 ,0 ,len (cur) ,0 ,len (cur) ,0 ,len (cur) ,0 ,0 } ;
            return ; 
        }
        int mid = l (cur) + r (cur) >> 1 ;
        melo :: pushdown (cur) ;
        if (nowl <= mid && nowr >= l (cur)) melo :: all_0 (ls ,nowl ,nowr) ;
        if (nowr > mid && nowl <= r (cur)) melo :: all_0 (rs ,nowl ,nowr) ;
        melo :: pushup (cur) ;
    } 
    inline void reverse (int cur ,int nowl ,int nowr) {
        if (nowl <= l (cur) && nowr >= r (cur)) {
            sum (cur) = len (cur) - sum (cur) ;
            swap (con1 (cur) ,con0 (cur)) ,swap (lcon1 (cur) ,lcon0 (cur)) ,swap (rcon1 (cur) ,rcon0 (cur)) ;
            fl (cur) ^= 1 ;
            return ;
        }
        int mid = l (cur) + r (cur) >> 1 ;
        melo :: pushdown (cur) ;
        if (nowl <= mid) melo :: reverse (ls ,nowl ,nowr) ;
        if (nowr > mid) melo :: reverse (rs ,nowl ,nowr) ;
        melo :: pushup (cur) ;
    }
    inline int Sum (int cur ,int nowl ,int nowr) {
        if (nowl <= l (cur) && nowr >= r (cur)) return sum (cur) ;
        int ans = 0 ,mid = l (cur) + r (cur) >> 1 ;
        melo :: pushdown (cur) ;
        if (nowl <= mid) ans += melo :: Sum (ls ,nowl ,nowr) ;
        if (nowr > mid) ans += melo :: Sum (rs ,nowl ,nowr) ;
        return ans ;
    } 
    inline segment_tree query (int cur ,int nowl ,int nowr) {
        if (nowl <= l (cur) && nowr >= r (cur)) return t[cur] ;
        int mid = l (cur) + r (cur) >> 1 ;
        melo :: pushdown (cur) ;
        segment_tree t1 ,t2 ,ans ;
        t1.init () ,t2.init () ,ans.init () ;
        if (nowl <= mid) t1 = melo :: query (ls ,nowl ,nowr) ;
        if (nowr > mid) t2 = melo :: query (rs ,nowl ,nowr) ;
        ans.sum = t1.sum + t2.sum ;
        ans.con1 = max (t1.con1 ,max (t2.con1 ,t1.rcon1 + t2.lcon1)) ;
        ans.con0 = max (t1.con0 ,max (t2.con0 ,t1.rcon0 + t2.lcon0)) ;
        ans.lcon1 = t1.lcon1 ;
        if (t1.lcon1 == t1.r - t1.l + 1) ans.lcon1 += t2.lcon1 ;
        ans.lcon0 = t1.lcon0 ;
        if (t1.lcon0 == t1.r - t1.l + 1) ans.lcon0 += t2.lcon0 ;
        ans.rcon1 = t2.rcon1 ;
        if (t2.rcon1 == t2.r - t2.l + 1) ans.rcon1 += t1.rcon1 ;
        ans.rcon0 = t2.rcon0 ;
        if (t2.rcon0 == t2.r - t2.l + 1) ans.rcon0 += t1.rcon0 ;
        return ans ;
    } 
}
int main () {
    read (n) ,read (m) ;
    f (i ,1 ,n ,1) read (a[i]) ;
    melo :: build (1 ,1 ,n) ;
    while (m --) {
        int op ,l ,r ;
        read (op) ,read (l) ,read (r) ;
        l ++ ,r ++ ;
        switch (op) {
            case 0 :
                melo :: all_0 (1 ,l ,r) ;
                break ;
            case 1 :
                melo :: all_1 (1 ,l ,r) ;
                break ;
            case 2 :
                melo :: reverse (1 ,l ,r) ;
                break ;
            case 3 :
                cout << melo :: Sum (1 ,l ,r) << '\n' ;
                break ;
            case 4 :
                cout << melo :: query (1 ,l ,r).con1 << '\n' ;
                break ;
        }
    }
    return 0 ;
}

by TLE_AK @ 2024-09-07 02:03:16

@Melo_DDD pushdown的首个else if少了个0(相当于rcon0没改)


by TLE_AK @ 2024-09-07 02:08:01

具体来说就是

t[ls] = (segment_tree) { l (ls) ,r (ls) ,len (ls) ,len (ls) ,0 ,len (ls) ,0 ,len (ls) ,1 ,0 } ;
t[rs] = (segment_tree) { l (rs) ,r (rs) ,len (rs) ,len (rs) ,0 ,len (rs) ,0 ,len (rs) ,1 ,0 } ;

改为

t[ls] = (segment_tree) { l (ls) ,r (ls) ,len (ls) ,len (ls) ,0 ,len (ls) ,0 ,len (ls) ,0,1 ,0 } ;
t[rs] = (segment_tree) { l (rs) ,r (rs) ,len (rs) ,len (rs) ,0 ,len (rs) ,0 ,len (rs) ,0,1 ,0 } ;

by Melo_DDD @ 2024-09-07 08:19:29

@TLE_AK 啊,我知道了,谢谢您!谢谢您!谢谢您!谢谢您!谢谢您!谢谢您!已关


|