求大佬改线段树pushup操作

P2572 [SCOI2010] 序列操作

_Goodnight @ 2022-01-01 17:44:43

今天把这题搞完再AFO了

#include <bits/stdc++.h>
using namespace std;
#define lk k<<1
#define rk k<<1|1
#define mlr l+r>>1
typedef long long ll;
const int N = 1e5 + 5;
int a[N], num[4 * N], add[4 * N];
struct node {
    int l, mid, r;
}mx[4 * N], mx0[4 * N];//储存最长1,最长0
void swap(int& a, int& b) { a = a + b; b = a - b; a = a - b; }
void pushup(int k, int l, int r) {
    /*
    mx[k].l = mx[lk].l; mx[k].r = mx[rk].r;
    mx[k].mid = max(mx[lk].r + mx[rk].l, max(mx[lk].mid, mx[rk].mid));
    mx0[k].l = mx0[lk].l; mx0[k].r = mx0[rk].r;
    mx0[k].mid = max(mx0[lk].r + mx0[rk].l, max(mx0[lk].mid, mx0[rk].mid));
    return;
    */
    //但是这部分是错的。
}
void modify(int k, int l, int r, int v) {
    //pushup(k, l, r);
    if (v == -1) { num[k] = 0;  mx[k].l = mx[k].mid = mx[k].r = 0; mx0[k].l = mx0[k].mid = mx0[k].r = (r - l + 1);
    }
    if (v == 1) { num[k] = (r - l + 1);  mx[k].l = mx[k].mid = mx[k].r = (r - l + 1); mx0[k].l = mx0[k].mid = mx0[k].r = 0;
    }
    if (v == 2) {
        num[k] = (r - l + 1) - num[k]; 
        swap(mx[k].l, mx0[k].l);
        swap(mx[k].mid, mx0[k].mid);
        swap(mx[k].r, mx0[k].r);
    }
    add[k] = v;
}

void pushdown(int k, int l, int r, int mid) {
    if (add[k] != 0) {
        modify(lk, l, mid, add[k]);
        modify(rk, mid + 1, r, add[k]);
        add[k] = 0;
    }
}
void build(int k, int l, int r) {
    if (l == r) {
        num[k] = a[l];
        mx[k].l = mx[k].r = mx[k].mid = a[l];
        mx0[k].l = mx0[k].r = mx0[k].mid = (1-a[l]);
        return;
    }
    int mid = mlr;
    build(lk, l, mid); build(rk, mid + 1, r);
    num[k] = num[lk] + num[rk];
    pushup(k, l, r);
}
void update(int k, int l, int r, int x, int y, int v) {
    if (l > y || r < x) {
        return;
    }//-1->0 1->1 2->fanzhuan
    if (l >= x && r <= y) {

        modify(k, l, r, v);
        return;
    }
    int mid = mlr;
    pushdown(k, l, r, mid);
    update(lk, l, mid, x, y, v); update(rk, mid + 1, r, x, y, v);
    num[k] = num[lk] + num[rk];
    pushup(k, l, r);
}
ll query(int k,int l,int r,int x,int y){
    if (l > y || r < x)return 0;
    if (l >= x && r <= y) {
        return num[k];
    }
    int mid = mlr;
    pushdown(k, l, r, mid);
    return query(lk, l, mid, x, y) + query(rk, mid + 1, r, x, y);
}
node querymx(int k, int l, int r, int x, int y) {
    if (l > y || r < x)return {0,0,0};
    if (l >= x && r <= y) {
        return mx[k];
    }
    int mid = mlr;
    pushdown(k, l, r, mid);
    node t1=querymx(lk, l, mid, x, y),t2=querymx(rk, mid + 1, r, x, y);

    node tmp;
    tmp.l = t1.l, tmp.r = t2.r;
    tmp.mid = max(t1.r + t2.l, max(t1.mid, t2.mid));
    return tmp;
}

int n, m,l,r,opt;
node t;
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    build(1, 1, n);
    while (m--) {
        scanf("%d%d%d", &opt, &l, &r);
        l++, r++;
        switch (opt){
            case 0:
                update(1, 1, n, l, r, -1);
                break;
            case 1:
                update(1, 1, n, l, r, 1);
                break;
            case 2:
                update(1, 1, n, l, r, 2);
                break;
            case 3:
                printf("%d\n", query(1, 1, n, l, r));
                break;
            case 4:
                t = querymx(1, 1, n, l, r);
                printf("%d\n",max(t.mid,max(t.l,t.r)));
                break;
            default:
                break;

        }
    }
}

by _Goodnight @ 2022-01-01 18:48:13

@zwx2007 样例过不去(

输出的第三行是 5 而答案应该是6


by _Goodnight @ 2022-01-01 18:49:03

@zwx2007 不过好像是lazytag有问题,他们说lazytag要分开来


by __zzy__ @ 2022-01-01 18:50:09

@_Goodnight 操作4你要把所有询问完全覆盖的区间合并成一个大区间,跟pushup一样从左往右去合并


by __zzy__ @ 2022-01-01 18:51:36

@_Goodnight lazy你要开两个数组,一个是覆盖的,一个是反转的


by zwx2007 @ 2022-01-01 18:53:38

@_Goodnight 好了现在push_down的问题又来了


by _Goodnight @ 2022-01-01 18:54:38

@zzy 输出从5变成7了((


by zwx2007 @ 2022-01-01 18:54:49

@_Goodnight 别忘记优先级和赋初值


by _Goodnight @ 2022-01-01 18:55:09

@zzy 为什么 (


by __zzy__ @ 2022-01-01 18:55:24

pushdown先下传覆盖的标记再下传反转的标记,并且你覆盖操作时反转标记要清空,而反转时覆盖标记不要动,或者直接对覆盖标记异或上一个1


by __zzy__ @ 2022-01-01 18:57:14

??哪个?操作4你要是理解不了我的可以先不改,用你那个试试


上一页 | 下一页