You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
// 11个测试点只能通过3个测试点
// 柯朵莉树模板
struct Node {
int l, r; // l和r表示这一段的起点和终点
mutable int v; // v表示这一段上所有元素相同的值是多少,注意关键字 mutable,使得set中结构体属性可修改
bool operator<(const Node &b) const {
return l < b.l; // 规定按照每段的左端点排序
}
};
set<Node> s; // 柯朵莉树的区间集合
// 分裂:[l,x-1],[x,r]
set<Node>::iterator split(int x) {
auto it = s.lower_bound({x});
if (it != s.end() && it->l == x) return it; // 一击命中
it--; // 没有找到就减1个继续找
if (it->r < x) return s.end(); // 真的没找到,返回s.end()
int l = it->l, r = it->r, v = it->v; // 没有被返回,说明找到了,记录下来,防止后面删除时被破坏
s.erase(it); // 删除整个区间
s.insert({l, x - 1, v}); //[l,x-1]拆分
// insert函数返回pair其中的first是新插入结点的迭代器
return s.insert({x, r, v}).first; //[x,r]拆分
}
// 区间加
void add(int l, int r, int v) {
// 必须先计算itr,后计算itl
auto R = split(r + 1), L = split(l);
for (auto it = L; it != R; it++) it->v += v;
}
// 区间赋值
void assign(int l, int r, int v) {
auto R = split(r + 1), L = split(l);
s.erase(L, R); // 删除旧区间
s.insert({l, r, v}); // 增加新区间
}
void change(int l, int r) {
auto R = split(r + 1), L = split(l);
for (auto it = L; it != R; it++) it->v = !it->v; // 取反,暴力
}
int count1(int l, int r) {
int res = 0;
auto R = split(r + 1), L = split(l);
for (auto it = L; it != R; it++)
res += (it->r - it->l + 1) * it->v;
return res;
}
// 多少个连续的1
int count2(int l, int r) {
int res = 0;
int t = 0;
auto R = split(r + 1), L = split(l);
for (auto it = L; it != R; it++)
if (it->v) {
t += it->r - it->l + 1;
res = max(res, t); // 一定要在t变大后马上取max,不能在下面else里取 max,那样最后的区间会无法得到,造成错误!
} else
t = 0;
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("P2572.in", "r", stdin);
#endif
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
// 下标从 0 开始
int x;
cin >> x;
s.insert({i, i, x}); // 初始化柯朵莉树
}
while (m--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 0) assign(l, r, 0);
if (op == 1) assign(l, r, 1);
if (op == 2) change(l, r);
if (op == 3) cout << count1(l, r) << endl;
if (op == 4) cout << count2(l, r) << endl;
}
return 0;
}