思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误。但是不明白自己颜色统计为什么错了。
求大佬指点迷津。思路很简单,就是一个裸的线段树。只要推出样例就做出来了。
以下是两种颜色统计:
这是我的错误的:
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. C#如何读写和创建INI文件(经典)转

    C#如何读写和创建INI文件 分类: c#程序设计2011-11-27 20:42 4935人阅读 评论(2) 收藏 举报 inic#stringbuffernullfile 在做项目过程中,有时需要 ...

  2. treap平衡树

    今天集训讲平衡树,就瞎搞了一下.直接下代码. #include<iostream> #include<cstdio> #include<cmath> #includ ...

  3. [SDOI2007]游戏

    https://zybuluo.com/ysner/note/1184420 题面 题意简单,但不太好概括. 戳我 解析 不成熟想法 据题意可知,字符串字符的顺序无影响. 并且判断两个字符串能否接龙可 ...

  4. 2015 多校赛 第二场 1006 (hdu 5305)

    Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...

  5. JavaScript 判断手机端操作系统(Andorid/IOS)

    androidURL = "http://xxx/xxx.apk"; var browser = { versions: function() { var u = navigato ...

  6. 九九乘法表---for循环的嵌套

    package com.zuoye.test;//控制台输出九九乘法表public class Jiujiu { public static void main(String[] args) { in ...

  7. VHDL之concurrent之operators

    Using operators Operators can be used to implement any combinational circuit. However, as will becom ...

  8. 安卓JNI使用OpenCV

    OpenCV也有Java数据结构的包,不过计算速度还是很慢,非不得已不使用此种方式调用OpenCV.使用NDK编写底层OpenCv的调用代码,使用JNI对代码进行封装,可以稍微提高一点效率. 参考链接 ...

  9. C#访问Win 32的一些尝试

    使用C#调用Win 32 Api大部分情况下基本只涉及到参数类型的转变,但在遇到Win 32 Api返回LPVOID *lpBuff 时会遇到一些解析遍历难题.lpBuff为二维指针,*lpBuff是 ...

  10. webSphere

    WebSphere 是 IBM 的软件平台.它包含了编写.运行和监视全天候的工业强度的随需应变 Web 应用程序和跨平台.跨产品解决方案所需要的整个中间件基础设施,如服务器.服务和工具.WebSphe ...