ZOJ1610 Count the Colors 经典线段树染色问题

题意,给你n个  x,y,c,意思就是区间[x,y]被染成C色,但是颜色会被覆盖的,染色操作完成以后 问你每种颜色有多少段 并输出颜色编号id跟段数cnt

经典问题,不过写的有点撮吧,没去看别人的,这个方法应该是最传统的最普通的,常规的开数组记录,也许大神们有更高端的方法

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long

#define LL __int64

#define eps 1e-8

#define inf 0xfffffff

//const LL INF = 1LL<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;

const int N = 8000 + 5;

typedef struct Node {
	int l,r;
	int col;
};

Node tree[N * 4];

int color[N];
int ans[N];

void init() {
	memset(color,-1,sizeof(color));
	memset(ans,0,sizeof(ans));
}

void build(int l,int r,int id) {
	tree[id].l = l;
	tree[id].r = r;
	tree[id].col = -1;
	if(l == r)return;
	int mid = (l + r)/2;
	build(l,mid,id<<1);
	build(mid+1,r,id<<1|1);
}

void cal(int id) {
	if(tree[id].col > -1) {
		tree[id<<1].col = tree[id].col;
		tree[id<<1|1].col = tree[id].col;
		tree[id].col = -1;
	}
}

void update(int l,int r,int id,int col) {
	if(tree[id].col == col)return;
	if(l <= tree[id].l && r >= tree[id].r) {
		 tree[id].col = col;return;
	}
	cal(id);
	int mid = (tree[id].l + tree[id].r)/2;
	if(r <= mid)update(l,r,id<<1,col);
	else if(l > mid)update(l,r,id<<1|1,col);
	else {
		update(l,mid,id<<1,col);
		update(mid+1,r,id<<1|1,col);
	}
}

void query(int l,int r,int id) {
	if(tree[id].col > -1) {
		for(int i=tree[id].l;i<=tree[id].r;i++)
			color[i] = tree[id].col;
		return;
	}
	if(tree[id].l == tree[id].r)return;
	int mid = (tree[id].l + tree[id].r)/2;
	if(r <= mid)query(l,r,id<<1);
	else if(l > mid)query(l,r,id<<1|1);
	else {
		query(l,mid,id<<1);
		query(mid+1,r,id<<1|1);
	}
}

int main() {
	int n;
	while(scanf("%d",&n) == 1) {
		int q = n;
		init();
		build(1,N,1);
		int x,y,c;
		while(q--) {
			scanf("%d %d %d",&x,&y,&c);
			x++;
			update(x,y,1,c);
		}
		query(1,N,1);
		for(int i=1;i<N;i++)
			if(color[i - 1] != color[i])
				if(color[i] > -1)
					ans[color[i]]++;
		for(int i=0;i<N;i++)
			if(ans[i])
				printf("%d %d\n",i,ans[i]);
		puts("");
	}
	return 0;
}

ZOJ1610 Count the Colors 经典线段树染色问题,布布扣,bubuko.com

时间: 2024-10-12 04:17:24

ZOJ1610 Count the Colors 经典线段树染色问题的相关文章

非结构体线段树版 ZJU 1610 Count the Colors (线段树区间更新)

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last. Input The first line of each data set contains exactly

ZOJ 1610 Count the Colors (线段树区间更新)

题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头输出. //ZOJ 1610 #include <cstdio> #include <cstring> #include <iostream> using namespace std ; int p[8010*4],lz[8010*4] ,hashh[8010*4],has

pku 2777(经典线段树染色问题)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41202   Accepted: 12458 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.

Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候有些不同,并不是以点为对象,而是以区间为对象,很明显是对线段树的区间进行操作,更新的时候要以区间为单位,还有就是计算每个区间出现几次的时候可以根据线段树的建树特征对树进行遍历求解. 1 #include <cstdio> 2 #include <cstring> 3 #include

ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent o

ZOJ 1610 Count the Colors(线段树lazy+暴力统计)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况就需要处理一下,代码中把所有的左端点都+1,避免了这种情况. 3. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define L(root) ((root) &l

ZOJ 1610 Count the Colors(线段树区间更新)

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last. Input The first line of each data set contains exactly

POJ2528 Mayor&#39;s posters(线段树染色问题+离散化)

题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报会覆盖前一张海报,求最后可见(包括完全和不完全可见)的海报有几张. 例如: 1 5 1 4 2 6 8 10 3 4 7 10 如上图所示,答案为4. 解题思路:其实这是一道区间染色问题,但是由于查找区间太大,显然直接建树会导致MLE,所以这里通过使用对区间的离散化来缩小查找范围.参考了一些大牛博客,简单说一