Zju1610 Count the Colors
题面:
画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了。
你的任务就是要数出你随后能看到的不同颜色的段的数目。
Input:
每组测试数据第一行只有一个整数n, 1 <= n <= 8000,等于填色的次数
接下来的n行每行有三个非负整数,他们之间用一个空格分开。
x1 x2 c
x1和x2表示填色段最左边的点和最右边的点, c表示填进的颜色。
所有数字都是在[0..8000]这个范围里的整数。
输入有多组测试数据,最后以文件结束符为结束。
Output:
输出的每一行都是最后能看到的颜色的数字,还有这种颜色的段数。
如果这种颜色最后不能见到,就不要打印出来。
每组数据后面跟一个空行。
Solution:
说实话,这道题十分的傻逼,我一开始想的时候一直没想出来,一看题解,豁然开朗:原来还可以这么暴力啊。。。
线段树,tree数组记录当前区间是否颜色一致,若颜色一致,则等于那个颜色,否则等于-1
那么叶子节点的tree记录的显然是自己的颜色,所以最后再线段树查找单点颜色就行了
把颜色都记录下来后,O(N)扫一遍就行了
Code:
#include<bits/stdc++.h>
#define N 8001
#define ls (q<<1)
#define rs (q<<1|1)
using namespace std;
int n,cl,cc,tree[N*5],col[N],ans[N];
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void pushdown(int q){
if(tree[q]!=-1)tree[ls]=tree[rs]=tree[q];
tree[q]=-1;
}
void change(int l,int r,int q,int L,int R,int v){
if(tree[q]==v)return ;
if(l>=L&&r<=R){
tree[q]=v;
return ;
}
pushdown(q);
int mid=(l+r)>>1;
if(mid>=L)change(l,mid,ls,L,R,v);
if(mid<R)change(mid+1,r,rs,L,R,v);
}
int query(int l,int r,int q,int x){
if(l==r)return tree[q];
int mid=(l+r)>>1;
pushdown(q);
if(mid>=x)return query(l,mid,ls,x);
if(mid<x)return query(mid+1,r,rs,x);
}
int main(){
while((scanf("%d",&n))!=EOF){
memset(tree,255,sizeof(tree));
memset(col,255,sizeof(col));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++){
int l=read(),r=read(),v=read();
change(0,N,1,l,r-1,v);//因为是区间长度,所以r要减一
}
int pre=-1;
for(int i=0;i<=N;i++){
int now=query(0,N,1,i);
if(now!=-1&&now!=pre)ans[now]++;
pre=now;
}
for(int i=0;i<=N;i++)
if(ans[i])printf("%d %d\n",i,ans[i]);
puts("");
}
return 0;
}
Zju1610 Count the Colors的更多相关文章
- Zju1610 Count the Colors(lazy标记详解)
Description 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目. Input 每组测试数据第一行只有一个整数n, 1 < ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- zoj 1610 Count the Colors
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 Count the Colors Time Limit:2000MS ...
- Count the Colors(线段树,找颜色段条数)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- Count the Colors
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- (线段树) Count the Colors --ZOJ --1610
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F http://acm.zju.edu.cn/onli ...
- zoj 1610 Count the Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
随机推荐
- C++可继承的单例基类模板
目录 一.介绍 二.代码 三.关键处 五.参考资料 一.介绍 最近在写一个项目,其中用到好几个单例,类本身的设计不是很复杂,但是如果每个都写一遍单例又觉得有点冗余:所以查资料写了一个单例基类模板,只要 ...
- gulp + angular + requirejs 简单学习
一.安装gulp(已经安装了node环境) npm install -g gulp 二.在package.json文件中配置依赖插件 { "name": "xxxx&q ...
- sqli-labs学习笔记 DAY5
DAY 5 sqli-labs lesson 26a 闭合符号为单引号和括号,并且不回显错误,如果服务器是Linux,尝试%a0代替空格,这里尝试使用布尔型 数据库名长度:?id=1')&&a ...
- trustbox文件破解
常见的破解方式,是要还原内容的二进制文件,删除加密壳部分的对应二进制数值,然后把剩下的内容保存下来,就实现了破解的任务. 淘宝破解链接:https://item.taobao.com/item.ht ...
- Navicat新建查询,系统找不到指定路径 独家解决办法
Navicat新建查询系统找不到指定路径,很多人用了网上流行的那些解决办法,还是无法解决.比如: https://jingyan.baidu.com/article/86112f1387a713273 ...
- mongodb基本使用(一)
1.启动.停止和重启mongodb服务 brew services start mongodb ---启动 brew services stop mongodb --停止 brew services ...
- TeamWork#1,Week 2,Learn In Team
我觉得做为一个团队,每个人的能力固然重要,但是更重要的是几个人能同心协力. 俗话说“三个臭皮匠,赛过诸葛亮”,团队合作往往能激发出团体不可思议的潜力,集体协作干出的成果往往能超过成员个人业绩的总和.在 ...
- C/C++:static用法总结
前言:static是C/C++中一个很重要的关键字,最近阅读了很多博客和资料,遂在此对自己的学习笔记进行简单的总结并发表在这里 一.C语言中的static • 静态全局变量:在全局变量之前加上关键字s ...
- POJ 3597 Polygon Division 多边形剖分
题目链接: http://poj.org/problem?id=3597 Polygon Division Time Limit: 2000MSMemory Limit: 131072K 问题描述 G ...
- 信息安全C散列函数的应用及其安全性2016011992
1:散列函数的具体应用 使用一个散列函数可以很直观的检测出数据在传输时发生的错误. MD5 Hash算法的"数字指纹"特性,使它成为目前应用最广泛的一种文件完整性校验(Checks ...