F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
题意:给一段0-8000的线段染色 问最后 颜色x 有几段
题解:标准线段树 但是没有push_up 最后查询是单点按顺序查询每一个点
考虑过使用区间来维护不同的线段有多少种各色的线段 思路是 两个子区间合并:左子区最右边和右子区最左边如果相同,那么就不变,不同就+1 但是不好维护 所以直接单点查还更方便
注意 染色是染区间不是染点 例如 0 3是染 0 1 2 3这4个数中间的空 如果不考虑清楚这一点就会导致 染了 2-3 颜色1 0 2颜色2 3-4颜色3 0 到4颜色分别为 2 2 3 3只有两种颜色 而如果是染区间的话
2 3区间中染了 1 后面的操作都没有覆盖掉 所以要把边映射成点 n个点有n-1个边 那么很自然 把第一条边成1 8000也就是8000了 update就要 符合映射 让 l+1 即可
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=+;
int num[maxn],ans[maxn];
struct Node{
int l,r,lazy,v;
void update(int x){
v=x;
lazy=x;
}
}tree[maxn*];
void push_up(int x){ }
void push_down(int x){
int lazyval=tree[x].lazy;
if(lazyval!=-){
tree[x<<].update(lazyval);
tree[x<<|].update(lazyval);
tree[x].lazy=-;
}
}
void update(int x,int l,int r,int c){
int L=tree[x].l,R=tree[x].r;
if(l<=L&&R<=r){
tree[x].update(c);
}
else{
int mid=L+R>>;
push_down(x);
if(mid>=l)update(x<<,l,r,c);
if(mid<r)update(x<<|,l,r,c);
// push_up(x);
}
}
int lastcolor=-;
int query(int x,int l,int r){
//int L=tree[x].l,R=tree[x].r;
if(l==r){
if(tree[x].v!=-&&tree[x].v!=lastcolor)ans[tree[x].v]++;
lastcolor=tree[x].v;
//if(lastcolor!=-1)cout<<l<<" " <<x<<" "<<tree[x].v<<endl;
}
else {
push_down(x);
int mid=l+r>>;
query(x<<,l,mid);
query(x<<|,mid+,r);
}
}
void build(int x,int l,int r){
tree[x].l=l,tree[x].r=r;
tree[x].lazy=-;
if(l==r){
tree[x].v=-;
//if(l==8000)cout<<" !zzz! "<<x<<endl;
return ;
}
else {
int mid=l+r>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
//push_up(x);
} }
int main(){
int n;
while(scanf("%d",&n)==)
{
lastcolor=-;
build(,,);
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++){
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
update(,x+,y,c);
}
// cout<<tree[8286].v<<"!! "<<endl;
query(,,);
// cout<<tree[8286].v<<" 22222"<<endl;
for(int i=;i<=;i++){
if(ans[i]){
printf("%d %d\n",i,ans[i]);
}
}
cout<<endl;
}
}
F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)的更多相关文章
- F - Count the Colors - zoj 1610(区间覆盖)
有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块.... *************** ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- (线段树) Count the Colors --ZOJ --1610
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F http://acm.zju.edu.cn/onli ...
- kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- Count the Colors ZOJ - 1610 区间颜色覆盖
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...
- F - Count the Colors
F - Count the Colors ZOJ - 1610 思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...
- ZOJ - 1610 经典线段树染色问题
这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...
- ZOJ 3279-Ants(线段树)
传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds Memory Limit: 32768 KB echo is a curious and cleve ...
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
随机推荐
- 来自后端的突袭? --开包即食的教程带你浅尝最新开源的C# Web引擎 Blazor
在今年年初, 恰逢新春佳节临近的时候. 微软给全球的C#开发者们, 着实的送上了一分惊喜. 微软正式开源Blazor ,将.NET带回到浏览器. 这个小惊喜, 迅速的在dotnet开发者中间传开了. ...
- 广播频道-BroadcastChannel
BroadcastChannel,就字面意思来言,叫做“广播频道”,官方文档说,该API是用于同源不同页面之间完成通信的功能. 1. 概况 它与window.postMessage的区别就是,Broa ...
- c++构造函数成员初始化中赋值和初始化列表两种方式的区别
先总结下: 由于类成员初始化总在构造函数执行之前 1)从必要性: a. 成员是类或结构,且构造函数带参数:成员初始化时无法调用缺省(无参)构造函数 b. 成员是常量或引用:成员无法赋值,只能被初始化 ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- hibernate操纵数据库常用方法 及 hibernate对象的三种状态
在dao层使用hibernate语言来与数据库进行访问,hibernate作为面向对象思想开发的dao层框架其理解也需要以面向对象的思想来看待 使用.hibernate不仅支持使用者使用他提供的对象来 ...
- Python—re模块
re模块 正则表达式就是字符串的匹配规则,在多数编程语言里都有相应的支持,python里对应的模块是re 常用的表达式规则 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹 ...
- JS 执行上下文
先看个小例子 function fn(){ console.log(a);//undefined; var a = 1; } fn(); 为什么打印出来的是 undefined 呢? 执行上下文概念 ...
- vue: WebStorm设置快速编译运行
WebSorm是一款优秀的前端开发工具,而Vue项目可以使用Node进行编译运行,平常我们可以通过命令行部署项目进行调试. 本文介绍设置Webstorm进行快速部署Vue项目. 第一步 点击启动快捷按 ...
- Python之加密模块
hashlib模块 加密方式以md5方式加密举例 是标准模块,直接导入即可 还有其他的加密方式,比如:.sha1()..sha224()..sha256()等,加密后的字符串长度更长,安全性更高 一. ...
- Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换
前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...