线段树 0 分求调+求 #1 测试点数据或我的代码通过不了的较小数据

P2572 [SCOI2010] 序列操作

幸存者 @ 2022-07-07 16:20:16

原贴

更改之后的代码(过了样例但依然 0 分):

#include <bits/stdc++.h>
using namespace std;
int w1[400010], w2[400010], w3[400010], w4[400010], b1[400010], b2[400010], b3[400010], b4[400010];
bool a[100010], lzy1[400010], lzy2[400010], lzy3[400010];
void maketag1(int u, int len)
{
    lzy1[u] = 1;
    lzy2[u] = lzy3[u] = w1[u] = w2[u] = w3[u] = w4[u] = 0;
    b1[u] = b2[u] = b3[u] = b4[u] = len;
}
void maketag2(int u, int len)
{
    lzy2[u] = 1;
    lzy1[u] = lzy3[u] = b1[u] = b2[u] = b3[u] = b4[u] = 0;
    w1[u] = w2[u] = w3[u] = w4[u] = len;
}
void maketag3(int u, int len)
{
    lzy3[u] ^= 1;
    w1[u] = len - w1[u];
    swap(w2[u], b2[u]);
    swap(w3[u], b3[u]);
    swap(w4[u], b4[u]);
}
void pushdown(int u, int L, int R)
{
    int M = L + R >> 1;
    if (lzy1[u])
    {
        maketag1(u << 1, M - L + 1);
        maketag1(u << 1 | 1, R - M);
    }
    if (lzy2[u])
    {
        maketag2(u << 1, M - L + 1);
        maketag2(u << 1 | 1, R - M);
    }
    if (lzy3[u])
    {
        maketag3(u << 1, M - L + 1);
        maketag3(u << 1 | 1, R - M);
    }
    lzy1[u] = lzy2[u] = lzy3[u] = 0;
}
void pushup(int u, int L, int R)
{
    int M = L + R >> 1;
    w1[u] = w1[u << 1] + w1[u << 1 | 1];
    w2[u] = max(max(w2[u << 1], w2[u << 1 | 1]), w3[u << 1] + w4[u << 1 | 1]);
    if (w3[u << 1 | 1] == R - M) w3[u] = w3[u << 1] + w3[u << 1 | 1];
    else w3[u] = w3[u << 1 | 1];
    if (w4[u << 1] == M - L + 1) w4[u] = w4[u << 1] + w4[u << 1 | 1];
    else w4[u] = w4[u << 1];
    b1[u] = b1[u << 1] + b1[u << 1 | 1];
    b2[u] = max(max(b2[u << 1], b2[u << 1 | 1]), b3[u << 1] + b4[u << 1 | 1]);
    if (b3[u << 1 | 1] == R - M) b3[u] = b3[u << 1] + b3[u << 1 | 1];
    else b3[u] = b3[u << 1 | 1];
    if (b4[u << 1] == M - L + 1) b4[u] = b4[u << 1] + b4[u << 1 | 1];
    else b4[u] = b4[u << 1];
}
void build(int u, int L, int R)
{
    if (L == R)
    {
        w1[u] = w2[u] = w3[u] = w4[u] = a[L];
        b1[u] = b2[u] = b3[u] = b4[u] = !a[L];
        return;
    }
    int M = L + R >> 1;
    build(u << 1, L, M);
    build(u << 1 | 1, M + 1, R);
    pushup(u, L, R);
}
void update1(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) maketag1(u, R - L + 1);
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        update1(u << 1, L, M, l, r);
        update1(u << 1 | 1, M + 1, R, l, r);
        pushup(u, L, R);
    }
}
void update2(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) maketag2(u, R - L + 1);
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        update2(u << 1, L, M, l, r);
        update2(u << 1 | 1, M + 1, R, l, r);
        pushup(u, L, R);
    }
}
void update3(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) maketag3(u, R - L + 1);
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        update3(u << 1, L, M, l, r);
        update3(u << 1 | 1, M + 1, R, l, r);
        pushup(u, L, R);
    }
}
int query1(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) return w1[u];
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        return query1(u << 1, L, M, l, r) + query1(u << 1 | 1, M + 1, R, l, r);
    }
    else return 0;
}
int query3(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) return w3[u];
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        int x = query3(u << 1, L, M, l, r), y = query3(u << 1 | 1, M + 1, R, l, r);
        return (y == R - M ? x + y : max(x, y));
    }
    else return 0;
}
int query4(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) return w3[u];
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        int x = query4(u << 1, L, M, l, r), y = query4(u << 1 | 1, M + 1, R, l, r);
        return (x == M - L + 1 ? x + y : max(x, y));
    }
    else return 0;
}
int query2(int u, int L, int R, int l, int r)
{
    if (l <= L && R <= r) return w2[u];
    else if (L <= r && R >= l)
    {
        int M = L + R >> 1;
        pushdown(u, L, R);
        return max(max(query2(u << 1, L, M, l, r), query2(u << 1 | 1, M + 1, R, l, r)), query3(u << 1, L, M, l, r) + query4(u << 1 | 1, M + 1, R, l, r));
    }
    else return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while (m--)
    {
        int op, l, r;
        cin >> op >> l >> r;
        if (op == 0) update1(1, 1, n, l + 1, r + 1);
        else if (op == 1) update2(1, 1, n, l + 1, r + 1);
        else if (op == 2) update3(1, 1, n, l + 1, r + 1);
        else if (op == 3) cout << query1(1, 1, n, l + 1, r + 1) << endl;
        else cout << query2(1, 1, n, l + 1, r + 1) << endl;
    }
    return 0;
}

by 听取MLE声一片 @ 2022-07-07 16:21:54

@幸存者 需要对拍吗

你可以试试拉一个代码对拍


by 幸存者 @ 2022-07-07 16:23:10

@听取MLE声一片 懒得对拍了,现在改了一下代码 10 分了


by 幸存者 @ 2022-07-07 16:35:33

改了一下样例都过不了了……


by 幸存者 @ 2022-07-07 16:46:08

AC 了,此贴终。


|