[SCOI2010]序列操作 线段树

时间:2021-11-10 16:40:35

~~~题面~~~

题解:

  在考场上打的这道题,出人意料的很快就打完了?!

  直接用线段树,维护几个东西:

  1,lazy标记 : 表示区间赋值

  2,mark标记:表示区间翻转

  3,l1:前缀最长连续的1的子段长度

  4,l0:前缀最长连续的0的子段长度

  5,m0:区间内最长的全为0的子段的长度

  6,r0:后缀最长连续的0的子段长度

  7,r1:后缀最长连续的1的子段长度

  8,m1:区间内最长的全为1的子段的长度

  9,sum :区间和

  维护起来比较繁琐,细节较多,但是都不难,是可以自己想出来的。

  这里提一个可以忽略标记处理顺序的小技巧:

  因为lazy标记是可以覆盖mark标记的,因此一个节点在得到lazy标记时,清空mark标记。

  因为mark标记可以看做是直接对lazy标记进行翻转,因此如果一个节点已经有lazy标记,那么在打上mark标记时,可以选择不得到mark标记,而是直接对lazy标记进行修改。

  因此不论是什么情况,mark标记和lazy标记都不会同时存在,也就不会有处理的先后顺序问题了。

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 101000
#define ac 800000
#define LL long long
int n, m, ans;
int s[AC];
int sum[ac], lazy[ac], l1[ac], r1[ac], l0[ac], r0[ac], m1[ac], m0[ac], l[ac], r[ac];
bool mark[ac]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void upmin(int &a, int b)
{
if(b < a) a = b;
} inline void upmax(int &a, int b)
{
if(b > a) a = b;
} inline int Max(int a, int b)
{
if(a > b) return a;
else return b;
} void update(int x)//更新信息
{
int ll = x * , rr = x * + ;
m1[x] = Max(m1[ll], m1[rr]);
upmax(m1[x], r1[ll] + l1[rr]);
m0[x] = Max(m0[ll], m0[rr]);
upmax(m0[x], r0[ll] + l0[rr]);
if(sum[rr] == r[rr] - l[rr] + ) r1[x] = sum[rr] + r1[ll];
else r1[x] = r1[rr];
if(!sum[rr]) r0[x] = r[rr] - l[rr] + + r0[ll];
else r0[x] = r0[rr];
if(sum[ll] == r[ll] - l[ll] + ) l1[x] = sum[ll] + l1[rr];
else l1[x] = l1[ll];
if(!sum[ll]) l0[x] = r[ll] - l[ll] + + l0[rr];
else l0[x] = l0[ll];
sum[x] = sum[ll] + sum[rr];
} void getlazy(int x, int go)
{
if(go == )
{
lazy[x] = , sum[x] = ;
l1[x] = r1[x] = m1[x] = ;
l0[x] = r0[x] = m0[x] = r[x] - l[x] + ;
}
else if(go == )
{
lazy[x] = ;
sum[x] = r[x] - l[x] + ;
l1[x] = r1[x] = m1[x] = sum[x];
l0[x] = r0[x] = m0[x] = ;
}
mark[x] = ;//区间赋值可以抵消区间反转
} void getmark(int x)
{
mark[x] ^= ;//翻转翻转标记
if(lazy[x]) getlazy(x, lazy[x] % + );
else
{
sum[x] = r[x] - l[x] + - sum[x];
swap(l1[x], l0[x]), swap(r1[x], r0[x]);
swap(m1[x], m0[x]);
}
} void pushdown(int x)//下传标记
{
if(lazy[x])
{
int ll = x * , rr = ll + ;
if(lazy[x] == )//change to 0
getlazy(ll, ), getlazy(rr, );
else//change to 1
getlazy(ll, ), getlazy(rr, );
lazy[x] = ;
}
if(mark[x])
{
int ll = x * , rr = ll + ;
getmark(ll), getmark(rr);
mark[x] = ;
}
} void change(int x, int ll, int rr, int go)//区间修改
{
pushdown(x);
if(l[x] == ll && r[x] == rr)
{
if(go <= ) getlazy(x, go);
else getmark(x);
return;
}
int mid = (l[x] + r[x]) >> ;
if(rr <= mid) change(x * , ll, rr, go);
else if(ll > mid) change(x * + , ll, rr, go);
else
{
change(x * , ll, mid, go);
change(x * + , mid + , rr, go);
}
update(x);
} void getsum(int x, int ll, int rr)
{
pushdown(x);
if(l[x] == ll && r[x] == rr)
{
ans += sum[x];
return ;
}
int mid = (l[x] + r[x]) >> ;
if(rr <= mid) getsum(x * , ll, rr);
else if(ll > mid) getsum(x * + , ll, rr);
else
{
getsum(x * , ll, mid);
getsum(x * + , mid + , rr);
}
} void find(int x, int ll, int rr)//询问连续1的长度
{
pushdown(x);
if(l[x] == ll && r[x] == rr)
{
upmax(ans, m1[x]);
return ;
}
int mid = (l[x] + r[x]) >> ;
if(rr <= mid) find(x * , ll, rr);
else if(ll > mid) find(x * + , ll, rr);
else
{
int tmp = min(mid - ll + , r1[x * ]) + min(rr - mid, l1[x * + ]);
find(x * , ll, mid);
find(x * + , mid + , rr);
upmax(ans, tmp);
}
} void build(int x, int ll ,int rr)
{
l[x] = ll, r[x] = rr;
if(l[x] == r[x])
{
sum[x] = l1[x] = r1[x] = m1[x] = s[l[x]];
if(!sum[x]) l0[x] = r0[x] = m0[x] = ;
return ;
}
int mid = (ll + rr) >> ;
build(x * , ll, mid);
build(x * + , mid + , rr);
update(x);
} void pre()
{
n = read(), m = read();
for(R i = ; i <= n; i ++) s[i] = read();
} void work()
{
int opt, a, b;
for(R i = ; i <= m; i ++)
{
// printf("!!!%d\n", i);
opt = read(), a = read() + , b = read() + ;
if(!opt) change(, a, b, );
else if(opt == ) change(, a, b, );
else if(opt == ) change(, a, b, );
else if(opt == )
{
ans = ;
getsum(, a, b);
printf("%d\n", ans);
}
else if(opt == )
{
ans = ;
find(, a, b);
printf("%d\n", ans);
}
}
} int main()
{
//freopen("operation.in", "r", stdin);
// freopen("operation.out", "w", stdout);
pre();
build(, , n);
work();
//fclose(stdin);
//fclose(stdout);
return ;
}