xudongyi1 @ 2024-07-19 10:05:47
rt
#include <iostream>
#include <stack>
#include <cmath>
#include <vector>
#include <map>
#include <string.h>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#define int long long
using namespace std;
const int N = 3e5 + 10;
const int INF = 0x3f3f3f3f;
int n, m;
struct node
{
int l, r, len;
int lz, co;
int num[3];// 数字数量
int ml[3];// 左边最长
int mr[3];// 右边最长
int mx[3];// 最大字段和
}tr[N << 2];
int a[N];
void push_up(int u)
{
for(int i = 0; i <= 1; i++)
{
tr[u].num[i] = tr[u << 1].num[i] + tr[u << 1 | 1].num[i];
tr[u].ml[i] = tr[u << 1].ml[i];
if(tr[u << 1].num[i] == tr[u << 1].len) tr[u].ml[i] = tr[u << 1].len + tr[u << 1 | 1].ml[i];
tr[u].mr[i] = tr[u << 1 | 1].mr[i];
if(tr[u << 1 | 1].num[i] == tr[u << 1 | 1].len) tr[u].mr[i] = tr[u << 1 | 1].len + tr[u << 1].mr[i];
tr[u].mx[i] = max(max(tr[u << 1].mr[i] + tr[u << 1 | 1].ml[i], max(tr[u << 1].mx[i], tr[u << 1 | 1].mx[i])), max(tr[u].ml[i], tr[u].mr[i]));
}
}
void colour(int u)
{
int co = tr[u].co;
tr[u << 1].co = tr[u << 1 | 1].co = co;
tr[u << 1].num[!co] = tr[u << 1 | 1].num[!co] = 0;
tr[u << 1].ml[!co] = tr[u << 1 | 1].ml[!co] = 0;
tr[u << 1].mr[!co] = tr[u << 1 | 1].mr[!co] = 0;
tr[u << 1].mx[!co] = tr[u << 1 | 1].mx[!co] = 0;
tr[u << 1].num[co] = tr[u << 1].ml[co] = tr[u << 1].mr[co] = tr[u << 1].mx[co] = tr[u << 1].len;
tr[u << 1 | 1].num[co] = tr[u << 1 | 1].ml[co] = tr[u << 1 | 1].mr[co] = tr[u << 1 | 1].mx[co] = tr[u << 1 | 1].len;
tr[u].co = -1;
tr[u << 1].lz = tr[u << 1 | 1].lz = tr[u].lz = 0;
}
void reverse(int u)
{
if(tr[u << 1].co != -1) tr[u << 1].co ^= 1;
else tr[u << 1].lz ^= 1;
if(tr[u << 1 | 1].co != -1) tr[u << 1 | 1].co ^= 1;
else tr[u << 1 | 1].lz ^= 1;
swap(tr[u << 1].num[0], tr[u << 1].num[1]);
swap(tr[u << 1].ml[0], tr[u << 1].ml[1]);
swap(tr[u << 1].mr[0], tr[u << 1].mr[1]);
swap(tr[u << 1].mx[0], tr[u << 1].mx[1]);
swap(tr[u << 1 | 1].num[0], tr[u << 1 | 1].num[1]);
swap(tr[u << 1 | 1].ml[0], tr[u << 1 | 1].ml[1]);
swap(tr[u << 1 | 1].mr[0], tr[u << 1 | 1].mr[1]);
swap(tr[u << 1 | 1].mx[0], tr[u << 1 | 1].mx[1]);
tr[u].lz = 0;
}
void push_down(int u)
{
if(tr[u].co != -1) colour(u);
if(tr[u].lz) reverse(u);
}
void build(int u, int l, int r)
{
tr[u].l = l, tr[u].r = r;
tr[u].len = r - l + 1;
tr[u].co = -1, tr[u].lz = 0;
if(l == r)
{
int v = a[l];
tr[u].num[v] = tr[u].ml[v] = tr[u].mr[v] = tr[u].mx[v] = 1;
return ;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
push_up(u);
// cout << tr[u].l << " " << tr[u].r << " " << tr[u].mx[0] << " " << tr[u].mx[1] << endl;
}
void change(int u, int l, int r, int x)
{
push_down(u);
if(l <= tr[u].l && tr[u].r <= r)
{
tr[u].co = x;
tr[u].num[x] = tr[u].ml[x] = tr[u].mr[x] = tr[u].mx[x] = tr[u].len;
tr[u].num[!x] = tr[u].ml[!x] = tr[u].ml[!x] = tr[u].mx[!x] = 0;
return ;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if(l <= mid) change(u << 1, l, r, x);
if(mid < r) change(u << 1 | 1, l, r, x);
push_up(u);
}
void rever(int u, int l, int r)
{
push_down(u);
if(l <= tr[u].l && tr[u].r <= r)
{
tr[u].lz ^= 1;
swap(tr[u].num[0], tr[u].num[1]);
swap(tr[u].ml[0], tr[u].ml[1]);
swap(tr[u].mr[0], tr[u].mr[1]);
swap(tr[u].mx[0], tr[u].mx[1]);
return ;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if(l <= mid) rever(u << 1, l, r);
if(mid < r) rever(u << 1 | 1, l, r);
push_up(u);
}
int query_sum(int u, int l, int r)
{
if(l <= tr[u].l && tr[u].r <= r)
{
return tr[u].num[1];
}
push_down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
int ans = 0;
if(l <= mid) ans += query_sum(u << 1, l, r);
if(mid < r) ans += query_sum(u << 1 | 1, l, r);
return ans;
}
node query_part(int u, int l, int r)
{
if(l <= tr[u].l && tr[u].r <= r)
{
return tr[u];
}
push_down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(r <= mid) return query_part(u << 1, l, r);
if(mid < l) return query_part(u << 1 | 1, l, r);
node now;
node ls = query_part(u << 1, l, r), rs = query_part(u << 1 | 1, l, r);
for(int i = 0; i <= 1; i++)
{
now.num[i] = ls.num[i] + rs.num[i];
now.ml[i] = ls.ml[i];
if(ls.num[i] == ls.len) now.ml[i] = ls.len + rs.ml[i];
now.mr[i] = rs.mr[i];
if(rs.num[i] == rs.len) now.mr[i] = rs.len + ls.mr[i];
now.mx[i] = max(max(ls.mr[i] + rs.ml[i], max(ls.mx[i], rs.mx[i])), max(now.ml[i], now.mr[i]));
}
return now;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
while(m--)
{
int opt, l, r;
cin >> opt >> l >> r;
opt++;
l++, r++;
if(opt == 1)
{
change(1, l, r, 0);
}
if(opt == 2)
{
change(1, l, r, 1);
}
if(opt == 3)
{
rever(1, l, r);
}
if(opt == 4)
{
cout << query_sum(1, l, r) << endl;
}
if(opt == 5)
{
cout << query_part(1, l, r).mx[1] << endl;
}
// for(int i = 1; i <= n; i++) cout << query_sum(1, i, i) << " ";
// puts("");
}
return 0;
}
/*
10 10
0 0 0 1 1 1 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
*/
by bangdeng @ 2024-07-19 10:43:20
建议重构
by _buzhidao_ @ 2024-07-19 16:34:45
@xudongyi1 能否给出提交记录?
by xudongyi1 @ 2024-07-19 18:09:04
@buzhidao here
by _buzhidao_ @ 2024-07-19 20:05:38
@xudongyi1 其实代码应该没什么大问题了,看到点里面一大半都过的,就到后面挂了。
by _buzhidao_ @ 2024-07-19 20:06:29
@xudongyi1
by _buzhidao_ @ 2024-07-19 20:07:40
@xudongyi1 对拍here
by xudongyi1 @ 2024-07-19 20:43:48
@buzhidao 对不起,调出来了没和您说。错误是 change
里面有一个 mr
写成 ml
了。
by _buzhidao_ @ 2024-07-19 21:12:12
@xudongyi1 恭喜。过了吗?
by _buzhidao_ @ 2024-07-19 21:12:57
@xudongyi1 看来本蒟蒻确实没有什么用处
by xudongyi1 @ 2024-07-19 21:23:43
@buzhidao 过了,谢谢啊,在翻讨论的时候找到一组 hack 了。