思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误。但是不明白自己颜色统计为什么错了。
求大佬指点迷津。思路很简单,就是一个裸的线段树。只要推出样例就做出来了。
以下是两种颜色统计:
这是我的错误的:
for(int i=;i<=;i++){
if(vis[i]!=vis[i-]&&vis[i-]!=-) ans[vis[i-]]++;
if(i==&&vis[i]!=-) ans[vis[i-]]++;
}

后来大佬解决了疑惑,改成酱就对了:

for(int i=;i<=;i++)
if(vis[i]!=vis[i-]&&vis[i-]!=-) ans[vis[i-]]++;

是因为本来i就超过了最大范围,所以最后一个一定会被统计上,再加上特判就会造成重复计数。

这是正确的:

int i=;
while(i<MAXN){
int flagor=vis[i],j=i+;
if(flagor==-){ ++i;continue; }
while(vis[j]!=-&&vis[j]==flagor&&j<MAXN) ++j;
++ans[flagor];i=j;
}

cpp:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 8010
using namespace std;
int n,m;
struct nond{
int l,r,flag;
}tree[MAXN*];
int vis[MAXN*],ans[MAXN*];
void build(int now,int l,int r){
tree[now].l=l;tree[now].r=r;tree[now].flag=-;
if(tree[now].l==tree[now].r) return ;
int mid=(tree[now].l+tree[now].r)/;
build(now*,l,mid);
build(now*+,mid+,r);
}
void down(int now){
tree[now*].flag=tree[now].flag;
tree[now*+].flag=tree[now].flag;
tree[now].flag=-; return ;
}
void change(int now,int l,int r,int k){
if(tree[now].l==l&&tree[now].r==r){
tree[now].flag=k;
return ;
}
if(tree[now].flag==k) return ;
if(tree[now].flag!=-) down(now);
int mid=(tree[now].l+tree[now].r)/;
if(r<=mid) change(now*,l,r,k);
else if(l>mid) change(now*+,l,r,k);
else{ change(now*,l,mid,k);change(now*+,mid+,r,k); }
}
void query(int now){
if(tree[now].flag!=-){
for(int i=tree[now].l;i<=tree[now].r;i++)
vis[i]=tree[now].flag;
return ;
}
if(tree[now].l==tree[now].r) return ;
query(now*); query(now*+);
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(ans,,sizeof(ans));
memset(vis,-,sizeof(vis));
build(,,);
for(int i=;i<=n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
change(,x+,y,z);
}
query();
for(int i=;i<=;i++)
if(vis[i]!=vis[i-]&&vis[i-]!=-) ans[vis[i-]]++;
for(int i=;i<=;i++)
if(ans[i]) printf("%d %d\n",i,ans[i]);
cout<<endl;
}
}

F - Count the Colors的更多相关文章

  1. F - Count the Colors(线段树)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  2. F - Count the Colors - zoj 1610(区间覆盖)

    有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块.... *************** ...

  3. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  4. (线段树) Count the Colors --ZOJ --1610

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F http://acm.zju.edu.cn/onli ...

  5. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  6. Count the Colors(线段树染色)

    Count the Colors Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Submit ...

  7. zoj 1610 Count the Colors

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610  Count the Colors Time Limit:2000MS   ...

  8. Count the Colors(线段树,找颜色段条数)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  9. Count the Colors

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

随机推荐

  1. Java 8 实战 P3 Effective Java 8 programming

    目录 Chapter 8. Refactoring, testing, and debugging Chapter 9. Default methods Chapter 10. Using Optio ...

  2. PCB 后台自动系统集成与邮件推送实现

    在PCB行业中,工程系统是主要数据生产者,而这些数据不仅仅给自己系统使用呀,我们需要将数据传递到各系统,才达到各系统共同协作的目的. 这里以问答方式对实现方式进行讲解.呵呵呵! 后台自动集成问题解答: ...

  3. bzoj1593 [Usaco2008 Feb]Hotel 旅馆(线段树)

    1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 758  Solved: 419[Submit ...

  4. this引用逃逸问题

    //this引用逃逸 // 1.构造器还未完成前,将自身this引用向外抛,使其他线程访问这个引用,进而访问到其未初始化的变量,造成问题 // 2.内部类访问外部类未初始化的成员变量 //3.多态继承 ...

  5. Nginx代理配置-centos6.10版

    nginx代理配置 cd /etc/nginx/init.d vi default.conf 添加: upstream server1{ server 192.168.125.128:8100 wei ...

  6. 解决QQ未启用状态,QQ留言图标未启用

    最近由于腾讯升级QQ一些东西,导致QQ图标成未启用状态:如图 解决方法,到腾讯此站点登陆一下即可, http://wp.qq.com/set.html 另外设置 没有保存按钮,如果选择完全公开,到自己 ...

  7. 日期Date和String/Long之间的转换

    下面是关于日期的常见的几种类型转换: import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...

  8. Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点

    [转载请注明]http://www.cnblogs.com/igoslly/p/8672656.html 看一下题目: Given a linked list, remove the nth node ...

  9. Eclipse安装配置——For Java

    1.下载安装JRE 2.下载Eclipse,解压到相应文件夹 3.配置Eclipse 3.1 配置字体大小  -12号 3.2配置workspace默认编码,utf-8,默认系统windows 3.3 ...

  10. linux环境jdk安装及配置

    linux环境jdk安装及配置 linux环境jdk安装及配置 1.下载jkd( http://www.oracle.com/technetwork/java/javase/downloads/ind ...