Sequence operation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8463 Accepted Submission(s): 2555
Problem 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
Author
lxhgww&&shǎ崽
Source
Recommend
lcy
题意:给你个01串。有以下几种操作:
1.把[l,r]区间的所有数都置为0或1
2.把[l,r]区间的所有数都置为其相反数
3.求[l,r]区间内的1的个数
4.求[l,r]区间内的最长的连续的'1'的个数
解题思路:线段树,因为只有0和1,所以只要记录一下一个区间是否为同一个数,然后成段更新即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int ma[100009 * 4], lr[100009 * 4], rl[100009 * 4];
int sum[100009 * 4], lazy[100009 * 4];
int x, y, z, n, q, a[100009];
void Merge(int k, int l, int r)
{
sum[k] = sum[k << 1] + sum[k << 1 | 1];
lazy[k] = lazy[k << 1] == lazy[k << 1 | 1] ? lazy[k << 1] : -1;
ma[k] = max(ma[k << 1], ma[k << 1 | 1]);
ma[k] = max(ma[k], rl[k << 1] + lr[k << 1 | 1]);
lr[k] = lr[k << 1], rl[k] = rl[k<<1|1];
int mid = (l + r) >> 1;
if (lr[k << 1] == mid - l + 1) lr[k] += lr[k << 1 | 1];
if (rl[k << 1 | 1] == r - mid) rl[k] += rl[k << 1];
}
void Push(int k, int l, int r)
{
int mid = (l + r) >> 1;
sum[k << 1] = ma[k << 1] = lr[k << 1]=rl[k<<1] = lazy[k] * (mid - l + 1);
sum[k << 1 | 1]=ma[k<<1|1]=lr[k<<1|1]=rl[k<<1|1]= (r - mid)*lazy[k];
lazy[k << 1] = lazy[k << 1 | 1] = lazy[k];
lazy[k] = -1;
}
void build(int k, int l, int r)
{
if (l == r) { lr[k] = rl[k] = sum[k] = ma[k] = lazy[k] = a[l]; return; }
int mid = (l + r) >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
Merge(k, l, r);
}
void update(int k, int l, int r, int ll, int rr, int type)
{
if (l >= ll&&r <= rr&&lazy[k] != -1)
{
if (type < 2) lazy[k] = type;
else lazy[k] ^= 1;
lr[k] = rl[k] = sum[k] = ma[k] = (r - l + 1)*lazy[k];
return;
}
int mid = (l + r) >> 1;
if (lazy[k] != -1) Push(k, l, r);
if (ll <= mid) update(k << 1, l, mid, ll, rr, type);
if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, type);
Merge(k, l, r);
}
int query1(int k, int l, int r, int ll, int rr)
{
if (l >= ll&&r <= rr) return sum[k];
int mid = (l + r) >> 1,ans=0;
if (lazy[k] != -1) Push(k, l, r);
if (ll <= mid) ans += query1(k << 1, l, mid, ll, rr);
if (rr > mid) ans += query1(k << 1 | 1, mid + 1, r, ll, rr);
return ans;
}
int query2(int k, int l, int r, int ll, int rr)
{
if (l >= ll&&r <= rr) return ma[k];
int mid = (l + r) >> 1;
if (lazy[k] != -1) Push(k, l, r);
if (rr <= mid) return query2(k << 1, l, mid, ll, rr);
if (ll > mid) return query2(k << 1 | 1, mid + 1, r, ll, rr);
int ans1 = query2(k << 1, l, mid, ll, rr), ans2 = query2(k << 1 | 1, mid + 1, r, ll, rr);
int ans3 = min(mid - ll + 1, rl[k << 1]) + min(lr[k << 1 | 1], rr - mid);
return max(ans1, max(ans2, ans3));
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build(1, 1, n);
while (q--)
{
scanf("%d%d%d", &x, &y, &z);
y++, z++;
if (x <= 2) update(1, 1, n, y, z, x);
else if (x == 3) printf("%d\n", query1(1, 1, n, y, z));
else printf("%d\n", query2(1, 1, n, y, z));
}
}
return 0;
}