Description
lxhgww got a sequence contains n characters which are all '0's or '1's.
We have five operations here:
Change operations:
0 a b change all characters into '0's in [a , b]
1 a b change all characters into '1's in [a , b]
2 a b change all '0's into '1's and change all '1's into '0's in [a, b]
Output operations:
3 a b output the number of '1's in [a, b]
4 a b output the length of the longest continuous '1' string in [a , b]
We have five operations here:
Change operations:
0 a b change all characters into '0's in [a , b]
1 a b change all characters into '1's in [a , b]
2 a b change all '0's into '1's and change all '1's into '0's in [a, b]
Output operations:
3 a b output the number of '1's in [a, b]
4 a b output the length of the longest continuous '1' string in [a , b]
Input
T(T<=10) in the first line is the case number.
Each case has two integers in the first line: n and m (1 <= n , m <= 100000).
The next line contains n characters, '0' or '1' separated by spaces.
Then m lines are the operations:
op a b: 0 <= op <= 4 , 0 <= a <= b < n.
Each case has two integers in the first line: n and m (1 <= n , m <= 100000).
The next line contains n characters, '0' or '1' separated by spaces.
Then m lines are the operations:
op a b: 0 <= op <= 4 , 0 <= a <= b < n.
Output
For each output operation , output the result.
Sample Input
1 10 10 0 0 0 1 1 0 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
Sample Output
5 2 6 5
这种题做多了就熟练了,只是要注意一个点,不然就会TLE个很惨,在代码中已经标出。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int cover[maxn << 2];
int sum[maxn << 2], maxv[maxn << 2], lv[maxn << 2], rv[maxn << 2];
void PushUp(int rt, int l, int r)
{
int m = (l + r) >> 1;
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
maxv[rt] = max(max(maxv[rt << 1], maxv[rt << 1 | 1]), lv[rt << 1 | 1] + rv[rt << 1]);
lv[rt] = lv[rt << 1] == m - l + 1 ? lv[rt << 1] + lv[rt << 1 | 1] : lv[rt << 1];
rv[rt] = rv[rt << 1 | 1] == r - m ? rv[rt << 1] + rv[rt << 1 | 1] : rv[rt << 1 | 1];
cover[rt] = cover[rt << 1] == cover[rt << 1 | 1] ? cover[rt<<1] : -1;
}
void PushDown(int rt, int l, int r)
{
if (cover[rt] != -1)
{
int m = (l + r) >> 1;
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
rv[rt << 1] = lv[rt << 1] = sum[rt << 1] = maxv[rt << 1] = (m - l + 1)*cover[rt];
rv[rt << 1 | 1] = lv[rt << 1 | 1] = sum[rt << 1 | 1] = maxv[rt << 1 | 1] = (r - m)*cover[rt];
cover[rt] = -1;
}
}
void build(int l, int r, int rt)
{
if (l == r)
{
int a;
scanf("%d", &a);
sum[rt] = a;
cover[rt] = maxv[rt] = lv[rt] = rv[rt] = a;
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt, l, r);
}
void update1(int L, int R, int op, int l, int r, int rt)
{
if (cover[rt] == op)
return;
if (L <= l&&r <= R)
{
cover[rt] = op;
lv[rt] = rv[rt] = maxv[rt] = sum[rt] = (r - l + 1)*op;
return;
}
PushDown(rt, l, r);
int m = (l + r) >> 1;
if (L <= m) update1(L, R, op, lson);
if (R > m) update1(L, R, op, rson);
PushUp(rt, l, r);
}
void XOR(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R && cover[rt] >= 0)
{
cover[rt] ^= 1;
rv[rt] = lv[rt] = maxv[rt] = sum[rt] = (r - l + 1)*cover[rt];
return;
}
PushDown(rt, l, r);
int m = (l + r) >> 1;
if (L <= m) XOR(L, R, lson);
if (R > m) XOR(L, R, rson);
PushUp(rt, l, r);
return;
}
int calsum(int L, int R, int l, int r, int rt) //查询区间1的数量
{
if (L <= l&&r <= R)
{
return sum[rt];
}
PushDown(rt, l, r);
int ret = 0;
int m = (l + r) >> 1;
if (L <= m) ret += calsum(L, R, lson);
if (R > m) ret += calsum(L, R, rson);
return ret;
}
int query1(int L, int R, int l, int r, int rt) //查询区间连续1的数量
{
if (L <= l&&r <= R)<span style="white-space:pre"> </span>//就是这里,我之前写成了l==r结果就TLE了
{
return maxv[rt];
}
PushDown(rt, l, r);
int m = (l + r) >> 1;
if (R <= m) return query1(L, R, lson);
else if (L > m) return query1(L, R, rson);
else
{
int lx = query1(L, m, lson);
int rx = query1(m + 1, R, rson);
int tmp = min(rv[rt << 1], m - L + 1) + min(lv[rt << 1 | 1], R - m);
return max(tmp, max(lx, rx));
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d%d", &n, &m);
build(0, n - 1, 1);
while (m--)
{
int c, u, v;
scanf("%d%d%d", &c, &u, &v);
if (c == 0 || c == 1)
update1(u, v, c, 0, n - 1, 1);
else if (c == 2)
XOR(u, v, 0, n - 1, 1);
else if (c == 3)
printf("%d\n", calsum(u, v, 0, n - 1, 1));
else
printf("%d\n", query1(u, v, 0, n - 1, 1));
}
}
return 0;
}