题目链接
时隔一年之后再回来写这道题,仍可以说是感受深刻。
我们要处理区间的0、1连续关系,以及最长的时候,我们很容易想到的是去维护区间左连续、右连续还有一个区间最长连续长度这样的三个值,然后更新操作上也一般没有多大问题。
但是,有这样的细节总是被我们遗忘,在查询答案的时候,我们很容易遗忘掉假如查询的区间是两边的时候,我们很容易遗忘掉中间的这段左子树的右端和右子树的左端,并且在查询操作上很容易出现对应的问题。
譬如说,查左子树的右端点的时候,我们是不是要去判断左子树的左连续,但是假如左子树的左连续超出了查询的(mid - ql - 1)这么长的话,是不是多余的部分就得舍去了,同理我们知道右子树也是一样的。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, a[maxN], lazy[maxN<<2];
struct node
{
int lx1, rx1, mx1;
int lx0, rx0, mx0;
node(int a=0, int b=0, int c=0, int x=0, int y=0, int z=0):lx1(a), rx1(b), mx1(c), lx0(x), rx0(y), mx0(z) {}
}tree[maxN<<2];
inline void pushup(int rt, int l, int r)
{
int mid = HalF;
//处理1
tree[rt] = node(tree[lsn].lx1, tree[rsn].rx1, max(tree[lsn].mx1, tree[rsn].mx1), tree[lsn].lx0, tree[rsn].rx0, max(tree[lsn].mx0, tree[rsn].mx0));
if(tree[rt].lx1 == mid - l + 1) tree[rt].lx1 += tree[rsn].lx1;
if(tree[rt].rx1 == r - mid) tree[rt].rx1 += tree[lsn].rx1;
tree[rt].mx1 = max(tree[rt].mx1, max(tree[rt].lx1, tree[rt].rx1));
tree[rt].mx1 = max(tree[rt].mx1, tree[lsn].rx1 + tree[rsn].lx1);
//处理0
if(tree[rt].lx0 == mid - l + 1) tree[rt].lx0 += tree[rsn].lx0;
if(tree[rt].rx0 == r - mid) tree[rt].rx0 += tree[lsn].rx0;
tree[rt].mx0 = max(tree[rt].mx0, max(tree[rt].lx0, tree[rt].rx0));
tree[rt].mx0 = max(tree[rt].mx0, tree[lsn].rx0 + tree[rsn].lx0);
}
inline void pushdown(int rt, int l, int r)
{
if(lazy[rt])
{
tree[lsn] = node(tree[lsn].lx0, tree[lsn].rx0, tree[lsn].mx0, tree[lsn].lx1, tree[lsn].rx1, tree[lsn].mx1);
tree[rsn] = node(tree[rsn].lx0, tree[rsn].rx0, tree[rsn].mx0, tree[rsn].lx1, tree[rsn].rx1, tree[rsn].mx1);
lazy[lsn] ^= 1; lazy[rsn] ^= 1;
lazy[rt] = 0;
}
}
inline void buildTree(int rt, int l, int r)
{
lazy[rt] = 0;
if(l == r)
{
tree[rt] = node(a[l], a[r], a[l], 1-a[l], 1-a[r], 1-a[l]);
return;
}
int mid = HalF;
buildTree(Lson); buildTree(Rson);
pushup(myself);
}
inline void update(int rt, int l, int r, int ql, int qr)
{
if(ql <= l && qr >= r)
{
tree[rt] = node(tree[rt].lx0, tree[rt].rx0, tree[rt].mx0, tree[rt].lx1, tree[rt].rx1, tree[rt].mx1);
lazy[rt] ^= 1;
return;
}
pushdown(myself);
int mid = HalF;
if(qr <= mid) update(QL);
else if(ql > mid) update(QR);
else { update(QL); update(QR); }
pushup(myself);
}
inline int query(int rt, int l, int r, int ql, int qr)
{
if(ql <= l && qr >= r) return tree[rt].mx1;
pushdown(myself);
int mid = HalF;
if(qr <= mid) return query(QL);
else if(ql > mid) return query(QR);
else
{
int TL = min(mid - ql + 1, tree[lsn].rx1), TR = min(qr - mid, tree[rsn].lx1);
int ans = max(query(QL), query(QR));
if(TL && TR) ans = max(ans, TL + TR);
return ans;
}
}
int main()
{
while(scanf("%d", &N) != EOF)
{
for(int i=1; i<=N; i++) scanf("%d", &a[i]);
buildTree(1, 1, N);
scanf("%d", &M);
int op, l, r;
while(M--)
{
scanf("%d%d%d", &op, &l, &r);
if(op)
{
update(1, 1, N, l, r);
}
else
{
printf("%d\n", query(1, 1, N, l, r));
}
}
}
return 0;
}