HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation

题目链接

题意:给定一个01序列,有5种操作

0 a b [a.b]区间置为0

1 a b [a,b]区间置为1

2 a b [a,b]区间0变成1,1变成0

3 a b 查询[a,b]区间1的个数

4 a b 查询[a,b]区间连续1最长的长度

思路:线段树线段合并。须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0、1。右边最长0、1,区间最长0、1,然后区间合并去搞就可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)
const int N = 100005;

int t, n, m;

struct Node {
	int l, r, sum[3][2], num;
	int setv, flip;
	Node() {
		setv = -1;
		flip = 0;
	}
	int size() {return r - l + 1;}
	void gao1(int v) {
		setv = v;
		num = v * (r - l + 1);
		for (int i = 0; i < 3; i++) {
			sum[i][v] = r - l + 1;
			sum[i][!v] = 0;
		}
		flip = 0;
	}
	void gao2() {
		flip ^= 1;
		for (int i = 0; i < 3; i++)
			swap(sum[i][0], sum[i][1]);
		num = r - l + 1 - num;
	}
} node[N * 4];

Node merge(Node lson, Node rson) {
	Node x;
	x.l = lson.l; x.r = rson.r;
	for (int i = 0; i < 2; i++) {
		x.sum[0][i] = lson.sum[0][i];
		x.sum[1][i] = rson.sum[1][i];
		x.sum[2][i] = max(lson.sum[2][i], rson.sum[2][i]);
		if (lson.sum[0][i] == lson.size())
			x.sum[0][i] += rson.sum[0][i];
		if (rson.sum[1][i] == rson.size())
			x.sum[1][i] += lson.sum[1][i];
		x.sum[2][i] = max(x.sum[2][i], lson.sum[1][i] + rson.sum[0][i]);
	}
	x.num = lson.num + rson.num;
	return x;
}

void pushup(int x) {
	node[x] = merge(node[lson(x)], node[rson(x)]);
}

void pushdown(int x) {
	if (node[x].setv != -1) {
		node[lson(x)].gao1(node[x].setv);
		node[rson(x)].gao1(node[x].setv);
		node[x].setv = -1;
	}
	if (node[x].flip) {
		node[lson(x)].gao2();
		node[rson(x)].gao2();
		node[x].flip = 0;
	}
}

void build(int l, int r, int x = 0) {
	node[x].l = l; node[x].r = r;
	node[x].setv = -1; node[x].flip = 0;
	if (l == r) {
		int tmp;
		scanf("%d", &tmp);
		for (int i = 0; i < 3; i++) {
			node[x].sum[i][tmp] = 1;
			node[x].sum[i][!tmp] = 0;
		}
		node[x].num = tmp;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
	pushup(x);
}

void add(int l, int r, int v, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		if (v == 2) node[x].gao2();
		else node[x].gao1(v);
		return;
	}
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	if (l <= mid) add(l, r, v, lson(x));
	if (r > mid) add(l, r, v, rson(x));
	pushup(x);
}

Node query(int l, int r, int x = 0) {
	if (node[x].l >= l && node[x].r <= r)
		return node[x];
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	Node ans;
	if (l <= mid && r > mid) ans = merge(query(l, r, lson(x)), query(l, r, rson(x)));
	else if (l <= mid) ans = query(l, r, lson(x));
	else if (r > mid) ans = query(l, r, rson(x));
	pushup(x);
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		build(0, n - 1);
		int op, a, b;
		while (m--) {
			scanf("%d%d%d", &op, &a, &b);
			if (op <= 2) add(a, b, op);
			else if (op == 3) printf("%d\n", query(a, b).num);
			else if (op == 4) printf("%d\n", query(a, b).sum[2][1]);
		}
	}
	return 0;
}
时间: 2024-12-09 23:17:46

HDU 3397 Sequence operation(线段树)的相关文章

HDU 3397 Sequence operation 线段树

线段树大杂烩~ 各种操作都有,细心点不难1A #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; #define lson rt<<1,l,mid #define rson rt<<1|1,mid + 1,r const int maxn = 1e5 + 10; int lmax[maxn

HDU 3397 Sequence operation (线段树,成段更新,区间合并)

http://acm.hdu.edu.cn/showproblem.php?pid=3397 Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5801    Accepted Submission(s): 1713 Problem Description lxhgww got a sequence

hdu 3397 Sequence operation (线段树 区间合并 多重标记)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问区间1的个数 4.询问区间被最长连续1的长度 思路: 这5个操作都是比较基础的线段树操作,难点在于有两种修改操作,这类题之前也写过,之前是乘法和加法,这个是区间亦或和区间更新值,但是思路是可以借鉴的,我们要推出这两个操作的关系,这样才能维护好这两个标记,我们用两个标记:same , rev ,分别表

Hdu 3397 Sequence operation(线段树多操作,Lazy思想,成段更新)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6397    Accepted Submission(s): 1899 Problem Description lxhgww got a sequence contains n characters which are all '0's or '1

hdu 3397 Sequence operation 线段树 区间更新 区间合并

题意: 5种操作,所有数字都为0或1 0 a b:将[a,b]置0 1 a b:将[a,b]置1 2 a b:[a,b]中的0和1互换 3 a b:查询[a,b]中的1的数量 4 a b:查询[a,b]中的最长连续1串的长度 这题看题目就很裸,综合了区间更新,区间合并 我一开始把更新操作全放一个变量,但是在push_down的时候很麻烦,情况很多,容易漏,后来改成下面的 更新的操作可以分为两类,一个是置值(stv),一个是互换(swp).如果stv!=-1,则更新儿子节点的stv,并将儿子的sw

【线段树】HDU 3397 Sequence operation 区间合并

操作 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,

hdu 3397 Sequence operation(线段树:区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的元素全部置为0. 操作1(1 a b):将a到b这个区间的元素全部置为1. 操作2(2 a b):将a到b这个区间所有的0置为1,所有的1置为0. 操作3(3 a b):查询a到b这个区间1的总数. 操作4(4 a b):查询a到b这个区间连续1的最长长度 本题属于简单的区间更新线段树 重点:0操作

HDU 3397 Sequence operation(线段树&#183;成段更新&#183;区间合并&#183;混合操作)

题意  给你一个只有0, 1的数组  有这些操作 0. 将[a, b]区间的所有数都改为0 1. 将[a, b]区间的所有数都改为1 2. 将[a, b]区间的所有数都取反 即与1异或 3. 输出区间[a, b]中1的个数  即所有数的和 4. 输出区间[a, b]中最大连续1的长度 对于所有的3, 4操作输出对应的答案 单个的操作都很简单  但搞在一起就有点恶心了  还好数组里的数只有0和1 线段树维护9个值 对应区间0, 1的最大长度len[i]  对应区间左端点为起点的最大0, 1长度ll

HDU 3397——Sequence operation(线段树,区间染色+区间异或+区间合并)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6270    Accepted Submission(s): 1862 Problem Description lxhgww got a sequence contains n characters which are all '0's or '1