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 ...
随机推荐
- Python学习环境搭建——VMware,Ubuntu,Anaconda,Pycharm
1.安装VMware虚拟机,版本是14.1.2(含注册码) 链接:https://pan.baidu.com/s/1ffNLONDjEMYDAenE36gRpA 密码:yazx 注:我的电脑分辨率较高 ...
- 如何写一个Xss Bot
如何写一个Xss Bot 现在的ctf比赛里 xss的出题方式比较特殊,一般使用xss bot,所以借鉴大佬经验尝试弄一个xss题目. xss bot 就是代替管理员去完成点击页面的任务,bot需要能 ...
- 第一个Django demo
平台:Pycharm Django 使用 Pycharm 进行开发,需要提前在 Pycharm 中(File > Settings > Project: Python > Proje ...
- 下一代的DevOps服务:AIOps
AIOps是一个总称,用于指代使用复杂的基础设施管理软件和云解决方案监控工具来实现自动化数据分析和日常的DevOps操作. 那些10年前甚至是5年前构建的系统监控工具的主要缺陷是它们不是为了满足大数据 ...
- Geatpy遗传算法在曲线寻优上的初步探究
园子里关于遗传算法的教案不少,但基于geatpy框架的并未多见,故分享此文以作参考,还望广大园友多多指教! Geatpy出自三所名校联合团队之手,是遗传算法领域的权威框架(python),其效率之高. ...
- impala 使用记录
在命令行里面直接输入类似下面的语句,就可以执行impala sql语句. impala-shell -q "select * from xxxc limit 10;" 当用pyth ...
- python爬虫项目(新手教程)之知乎(requests方式)
-前言 之前一直用scrapy与urllib姿势爬取数据,最近使用requests感觉还不错,这次希望通过对知乎数据的爬取为 各位爬虫爱好者和初学者更好的了解爬虫制作的准备过程以及requests请求 ...
- fetch err : "Body not allowed for GET or HEAD requests"
在使用 fetch 的时候 报了 "Body not allowed for GET or HEAD requests" 这个错. 代码如下: 一番google , 找到答案了. ...
- 第十一次PSP
- Thunder-Beta发布-事后诸葛亮会议-2017秋-软件工程第十一次作业
小组名称:Thunder项目名称:爱阅APP小组成员:王航 李传康 翟宇豪 邹双黛 苗威 宋雨 胡佑蓉 杨梓瑞一.设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有 ...