线段树专题—ZOJ1610 Count the Colors
题意:给一个n,代表n次操作,接下来每次操作表示把[l。r]区间的线段涂成k的颜色当中,l,r,k的范围都是0到8000
分析:事实上就是拿线段树维护一段区间的颜色,整体用到的是线段树的区间更新把,可是会给人一种区间合并的错觉
注意:这题比較坑的是千万不能拿n建树,不然就会segmentation fault,必须拿8000建树。也就是树是固定的
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
int col[maxn<<2];
int num[maxn];
int ncol;
void down(int l,int r,int rt){
if(col[rt]!=-1){
col[rt<<1]=col[rt<<1|1]=col[rt];
col[rt]=-1;
}
}
void update(int L,int R,int k,int l,int r,int rt){
if(L<=l&&r<=R){
col[rt]=k;
return;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,k,l,mid,rt<<1);
if(R>mid) update(L,R,k,mid+1,r,rt<<1|1);
}
void query(int l,int r,int rt){
if(l==r){
if(col[rt]>=0&&col[rt]!=ncol) num[col[rt]]++; //统计连续颜色段的个数
ncol=col[rt];
return;
}
down(l,r,rt);
int mid=(l+r)>>1;
query(l,mid,rt<<1);
query(mid+1,r,rt<<1|1);
}
int main()
{
int n;
while(scanf("%d",&n)!=-1){
ncol=-1;
memset(num,0,sizeof(num));
memset(col,-1,sizeof(col)); //建树
for(int i=1;i<=n;i++){
int c,l,r;
scanf("%d%d%d",&l,&r,&c);
if(l<r) update(l+1,r,c,1,8000,1);
}
query(1,8000,1);
for(int i=0;i<=8000;i++){
if(num[i]) printf("%d %d\n",i,num[i]);
}
printf("\n");
}
}
线段树专题—ZOJ1610 Count the Colors的更多相关文章
- 线段树专题—ZOJ1610 Count the Colors(涂区间,直接tag标记)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- zkw线段树专题
题目来自大神博客的线段树专题 http://www.notonlysuccess.com/index.php/segment-tree-complete/ hdu1166 敌兵布阵题意:O(-1)思路 ...
- vj线段树专题
vj线段树专题题解 单点更新模板 void build(int x,int l,int r){//sum[x]控制l-r区域 if(l==r){Sum[x]=num[l];return ;} int ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- Kuangbin 带你飞-线段树专题 题解
HDU 1166 敌兵布阵 单调更新区间查询和 #include <map> #include <set> #include <list> #include < ...
- 2018 UESTC 线段树专题
A - 一棵简单的线段树 A[1...n]初始全为0. 1. 给两个数p 和 x(1≤p≤n),单点更新 A[p] <- x 2. 给两个数L和R (1≤L<R≤n), L到R区间里这几 ...
- [ZOJ1610]Count the Colors
Description 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目. Input 每组测试数据第一行只有一个整数n, 1 < ...
- 线段树专题 POJ3468 A Simple Problem with Integers
题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long ...
随机推荐
- Selenium中自动输入10位随机数字的方法
有时候项目中需要输入快递号,对于已输入过的快递单号则不能再次输入,这种问题怎么解决呢,可以看下这个方法 public static final String ALLCHAR = "01234 ...
- Detect Vertical&Horizontal Segments By OpenCV
Detect Vertical&Horizontal Segments By OpenCV,and Save the data to csv. Steps: Using adaptiveThr ...
- 普通平衡树(指针splay)
最早的板子,学自Ez大佬: #include<cstdio> #include<cstdlib> using namespace std; class Splay{ publi ...
- Codeforeces 954C Matrix Walk
题目大意 考虑一个 $x\times y$ 的矩阵 $A_{x\times y}$ ,$A_{i,j} = (i-1)x+y$ . 从矩阵中的某个位置出发,每次可向上下左右移动一步,每到一个位置,记录 ...
- Jmeter 设置HTTP RPS性能测试模型
其实也挺简单的,主要是刚接触jmeter,记录一下. 1. 首先需要安装jmeter...真是废话... 2. 需要安装JMeterPlugins-ExtrasLibs-1.3.0.zip: JMet ...
- Codeforces Round #315 (Div. 2) B 水题强行set
B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Visual Studio调试技巧 -- Attach to Process
本文系作者原创,但可随意转载.另:图中使用的IDE为Visual Studio 2013 RC 英文版. 一般写完代码时,我们通常会启动调试运行一下看看是否正确,启动运行的方式无非是F5-- Star ...
- Mongodb学习(1)--- mongoose: Schema, Model, Entity
Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由 Schema 发布生成的模型,具有抽象属性和行为的数据库操作 Entity : 由 Model 创建的 ...
- 使用state改变的jsx监听不到数据变化的问题
当使用state来改变一个组件内部的虚拟dom的时候,该虚拟dom是无法监听到state数据的变化的,他只会绑定state改变dom当时的数据.
- 【linux命令】lscpu,/etc/cpuinfo详解
lscpu 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 i2000:~ # lscpu Architecture: ...