POJ 3225 Help with Intervals(线段树)

POJ 3225 Help with Intervals

题目链接

集合数字有的为1,没有为0,那么几种操作对应就是置为0或置为1或者翻转,这个随便推推就可以了,然后开闭区间的处理方式就是把区间扩大成两倍,偶数存点,奇数存线段即可

代码:

#include <cstdio>
#include <cstring>

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

const int N = 65536 * 2;

struct Node {
	int l, r, flip, setv;
} node[N * 4];

int to[N];

void build(int l, int r, int x = 0) {
	node[x].l = l; node[x].r = r;
	node[x].flip = 0; node[x].setv = -1;
	if (l == r) {
		to[l] = x;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
}

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

void add(int l, int r, int v, int x = 0) {
	if (l > r) return;
	if (node[x].l >= l && node[x].r <= r) {
		if (v != -1) {
			node[x].setv = v;
			node[x].flip = 0;
		} else
			node[x].flip ^= 1;
		return;
	}
	pushdown(x);
	int mid = (node[x].l + node[x].r) / 2;
	if (l <= mid) add(l, r, v, lson(x));
	if (r > mid) add(l, r, v, rson(x));
}

void query(int x = 0) {
	if (node[x].l == node[x].r) {
		if (node[x].setv == -1) node[x].setv = 0;
		return;
	}
	pushdown(x);
	int mid = (node[x].l + node[x].r) / 2;
	query(lson(x));
	query(rson(x));
}

char c, a, b;
int l, r;

int main() {
	build(0, N - 1);
	while (~scanf("%c %c%d,%d%c\n", &c, &a, &l, &r, &b)) {
		l = l * 2 + (a == '(');
		r = r * 2 - (b == ')');
		if (c == 'U') add(l, r, 1);
		if (c == 'I' || c == 'C') {
			add(0, l - 1, 0);
			add(r + 1, N - 1, 0);
			if (c == 'C') add(l, r, -1);
		}
		if (c == 'D') add(l, r, 0);
		if (c == 'S') add(l, r, -1);
	}
	query();
	int pre = 0, flag = 0, bo = 0;
	for (int i = 0; i < N; i++) {
		int id = to[i];
		int tmp = (node[id].setv^node[id].flip);
		if (!tmp && flag) {
			if (bo) printf(" ");
			else bo = 1;
			if (pre % 2) printf("(");
			else printf("[");
			printf("%d,%d", pre / 2, i / 2);
			if (i % 2 == 0) printf(")");
			else printf("]");
			flag = 0;
		} else if (tmp && !flag) {
			pre = i;
			flag = 1;
		}
	}
	if (bo == 0) printf("empty set");
	printf("\n");
	return 0;
}
时间: 2024-10-09 00:35:31

POJ 3225 Help with Intervals(线段树)的相关文章

poj 3225 Help with Intervals(线段树)

题目链接:poj 3225 Help with Intervals 题目大意:模拟集合操作,输出最终的集合. 解题思路:线段树. U l r:[l,r]区间置为1 I l r:[0,l),(r,maxn]置为0 D l r:[l,r]区间置为0 C l r:[0,l),(r,maxn]置为0,[l,r]区间取逆 S l r:[l,r]区间取逆. 然后基本水水的线段树,注意一下区间开和闭. #include <cstdio> #include <cstring> #include &

poj 3225 Help with Intervals(线段树,区间更新)

Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted: 3140 Case Time Limit: 2000MS Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on g

(中等) POJ 3225 Help with Intervals , 线段树+集合。

Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating ti

poj 3225 Help with Intervals

http://poj.org/problem?id=3225 题意:对集合进行交.并.差.异或四种操作,输出几步操作的之后的集合. U [a,b]  :可以将[a,b]全部置为1:  I [a,b] :可以将[a,b]之外的全部置为0:   S-[a,b] :将[a,b]全部置为0:  [a,b]-s  :将[a,b]之外的全部置为0,[a,b]取反.  I [a,b]  :将[a,b]取反. 然后用线段树维护区间. 1 #include <cstdio> 2 #include <cst

POJ - 3225 Help with Intervals (开闭区间)

Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating ti

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普