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 ...
随机推荐
- 结对测试 vs 随机测试
在接口测试过程中,最关键的是对参数的各种情况进行测试. 随机测试是指随机选择一些参数值来测. 结对测试是指parewise算法生成较高“性价比”的组合情况来测. 随机测试存在的问题 随机,这两个字本身 ...
- 会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
我们都知道,Python 的设计哲学是「优雅」.「明确」.「简单」.这也许很多人选择 Python 的原因.但是我收到有些伙伴反馈,他写的 Python 并不优雅,甚至很臃肿,那可能是你的姿势不对 ...
- QRCode 二维码
一.生成二维码 1.二维码就是绘制成黑白相间的图片,所谓的黑白相间就是代表0和1 ,二维码大约可以容纳500多个中文,所以用途之广显而易见. 所需的jar包 http://pan.baidu.com ...
- centos7.6 安装 openvpn--2.4.7
openvpn-server端 搭建 1,软件版本 Centos - 7.x easy-rsa - 3.0.3 OpenVPN - 2.4.7 2,安装 建议安装启用epel源,采用yum的方式安装o ...
- kubeadm源码修改证书时间 -1.13
编译后~ 链接:https://pan.baidu.com/s/1ofLX1Sv0ZF2yjkJdqf-6rw 提取码:cnbd 已统一与CA证书都是10年 已测试 适用于k8s 1.10 至 1.1 ...
- 机器学习之k-最近邻(kNN)算法
一.kNN(k-nearest neighbor)算法原理 事物都遵循物以类聚的思想,即有相同特性的事物在特征空间分布上会靠得更近,所以kNN的思路是:一个样本在特征空间中k个靠的最近的样本中,大多数 ...
- 【每日scrum】第一次冲刺day2
和小伙伴一起找地图 ,学习了mapinfo地图格式的基本知识,数据和图像分开存储
- sqlDataAdapter和SqlCommand的区别
因为DataSet是离线的,所以SqlDataAdapter这个对象是连接DataSet和数据库的桥梁,所有对DataSet的操作(填充,更新等)都要通过他 ado.net数据访问有两种方式: 1.离 ...
- Java锁的种类以及辨析
锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类 ...
- vs2010调试-尝试调试dll源码。
第一步: 打开“调试”——“选项和设置”——点击调试下“常规”——设置启用“启用.NET Framework源代码单步执行 ” 第二步 选择“符号”——选择Microsoft符号服务器——设置符号缓存 ...