SegmentTree_ @ 2024-01-17 20:51:22
程序输出
5
2
4
3
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lid (id << 1)
#define rid (id << 1 | 1)
const int N = 1e5+5;
struct node{
int l, r;
int lw, lb, rw, rb;
int w, b, mw, mb;
int lazy, rev;
node(){
w = b = mw = mb = lw = lb = rw = rb = rev = 0;
lazy = -1;
}
}tr[N << 2];
int n, q, a[N];
void pushup(node &id, node ls, node rs){
id.w = ls.w + rs.w;
id.b = ls.b + rs.b;
id.lw = (ls.b > 0 ? ls.lw : ls.w + rs.lw);
id.lb = (ls.w > 0 ? ls.lb : ls.b + rs.lb);
id.rw = (rs.b > 0 ? rs.rw : rs.w + ls.rw);
id.rb = (rs.w > 0 ? rs.rb : rs.b + ls.rb);
id.mw = max(max(ls.mw, rs.mw), ls.rw + rs.lw);
id.mb = max(max(ls.mb, rs.mb), ls.rb + rs.lb);
}
void pushdown(int id){
if (tr[id].lazy != -1){
tr[lid].lazy = tr[rid].lazy = tr[id].lazy;
tr[id].rev = 0;
if (tr[id].lazy == 0){
tr[lid].w = tr[lid].lw = tr[lid].rw = tr[lid].mw = 0;
tr[lid].b = tr[lid].lb = tr[lid].rb = tr[lid].mb = tr[lid].r - tr[lid].l + 1;
tr[rid].w = tr[rid].lw = tr[rid].rw = tr[rid].mw = 0;
tr[rid].b = tr[rid].lb = tr[rid].rb = tr[rid].mb = tr[rid].r - tr[rid].l + 1;
}
else{
tr[id].b = tr[id].lb = tr[id].rb = tr[id].mb = 0;
tr[id].w = tr[id].lw = tr[id].rw = tr[id].mw = tr[id].r - tr[id].l + 1;
tr[id].b = tr[id].lb = tr[id].rb = tr[id].mb = 0;
tr[id].w = tr[id].lw = tr[id].rw = tr[id].mw = tr[id].r - tr[id].l + 1;
}
tr[id].lazy = -1;
}
if (tr[id].rev){
if (tr[lid].lazy != -1)
tr[lid].lazy ^= 1;
else
tr[lid].rev ^= 1;
if (tr[lid].lazy != -1)
tr[lid].lazy ^= 1;
else
tr[lid].rev ^= 1;
tr[id].rev = 0;
swap(tr[lid].b, tr[lid].w);
swap(tr[lid].lb, tr[lid].lw);
swap(tr[lid].rb, tr[lid].rw);
swap(tr[lid].mb, tr[lid].mw);
swap(tr[rid].b, tr[rid].w);
swap(tr[rid].lb, tr[rid].lw);
swap(tr[rid].rb, tr[rid].rw);
swap(tr[rid].mb, tr[rid].mw);
}
}
void build(int id, int l, int r){
tr[id].l = l;
tr[id].r = r;
tr[id].lazy = -1;
tr[id].rev = 0;
if (l == r){
if (a[l] == 0){
tr[id].b = 1;
tr[id].mb = 1;
tr[id].lb = tr[id].rb = 1;
}
else{
tr[id].w = 1;
tr[id].mw = 1;
tr[id].lw = tr[id].rw = 1;
}
return;
}
int mid = (tr[id].l + tr[id].r) >> 1;
build(lid, l, mid);
build(rid, mid + 1, r);
pushup(tr[id], tr[lid], tr[rid]);
}
void modify(int id, int l, int r, int k){
//cout << id << ' ' << l << ' ' << r << ' ' << tr[id].l << ' ' << tr[id].r << '\n';
if (l <= tr[id].l && tr[id].r <= r){
tr[id].lazy = k;
if (k == 0){
tr[id].w = tr[id].lw = tr[id].rw = tr[id].mw = 0;
tr[id].b = tr[id].lb = tr[id].rb = tr[id].mb = tr[id].r - tr[id].l + 1;
}
else{
tr[id].b = tr[id].lb = tr[id].rb = tr[id].mb = 0;
tr[id].w = tr[id].lw = tr[id].rw = tr[id].mw = tr[id].r - tr[id].l + 1;
}
return;
}
pushdown(id);
int mid = (tr[id].l + tr[id].r) >> 1;
if (r <= mid) modify(lid, l, r, k);
else if (l > mid) modify(rid, l, r, k);
else modify(lid, l, mid, k), modify(rid, mid + 1, r, k);
pushup(tr[id], tr[lid], tr[rid]);
}
void rev(int id, int l, int r){
if (l <= tr[id].l && tr[id].r <= r){
if (tr[id].lazy != -1){
tr[id].lazy ^= 1;
}
else{
tr[id].rev ^= 1;
swap(tr[id].b, tr[id].w);
swap(tr[id].lb, tr[id].lw);
swap(tr[id].rb, tr[id].rw);
swap(tr[id].mb, tr[id].mw);
}
return;
}
pushdown(id);
int mid = (tr[id].l + tr[id].r) >> 1;
if (r <= mid) rev(lid, l, r);
else if (l > mid) rev(rid, l, r);
else rev(lid, l, mid), rev(rid, mid + 1, r);
pushup(tr[id], tr[lid], tr[rid]);
}
int query1(int id, int l, int r){
if (l <= tr[id].l && tr[id].r <= r) return tr[id].w;
pushdown(id);
int mid = (tr[id].l + tr[id].r) >> 1;
if (r <= mid) return query1(lid, l, r);
if (l > mid) return query1(rid, l, r);
else return query1(lid, l, mid) + query1(rid, mid + 1, r);
}
node query2(int id, int l, int r){
if (l <= tr[id].l && tr[id].r <= r) return tr[id];
pushdown(id);
int mid = (tr[id].l + tr[id].r) >> 1;
if (r <= mid) return query2(lid, l, r);
if (l > mid) return query2(rid, l, r);
node t;
pushup(t, query2(lid, l, mid), query2(rid, mid + 1, r));
return t;
}
void print(int id){
cout << "l:" << tr[id].l << " r:" << tr[id].r << '\n';
cout << "w:" << tr[id].w << " b:" << tr[id].b << '\n';
cout << "mw:" << tr[id].mw << " mb:" << tr[id].mb << '\n';
cout << "lw:" << tr[id].lw << " lb:" << tr[id].lb << '\n';
cout << "rw:" << tr[id].rw << " rb:" << tr[id].rb << '\n';
cout << '\n';
}
void printtree(int id = 1){
if (tr[id].l == tr[id].r){
print(id);
return;
}
pushdown(id);
print(id);
printtree(lid);
printtree(rid);
}
int main(){
cin >> n;
cin >> q;
for (int i = 1;i <= n;i++) cin >> a[i];
build(1, 1, n);
while (q--){
int op, l, r;
cin >> op >> l >> r;
++l, ++r;
if (op == 0){
modify(1, l, r, 0);
}
else if (op == 1){
modify(1, l, r, 1);
}
else if (op == 2){
rev(1, l, r);
}
else if (op == 3){
cout << query1(1, l, r) << '\n';
}
else{
cout << query2(1, l, r).mw << '\n';
}
}
return 0;
}