题目大意:多组数据,每组给一个n(1=<n<=8000),下面有n行,每行有l,r,color(1=<color<=8000),表示将l~r颜色变为color,最后求各种颜色(1~8000)所占区段数,如果所占区段为0则不用输出。

解题思路:基本还是跟区间染色问题差不多,但要注意一些东西

①这里l,r指的是端点。比如

3

1 4 1

1 2 2

3 4 3

这组数据最后输出结果为:

1 1

2 1

3 1

原因是还有[2,3]这一段颜色为1,处理办法是将更新函数写成(1,l+1,r,color)也就是左闭右开的形式,这样就可以使得中间的那块区间不背忽略。

②题目都没给总区间长度,所以无论查询还是建树,我们都默认用0~8000。

③关于查询各颜色所占区段数的问题,不可能直接通过线段树找到个颜色区段数,因为还要考虑两个子树是相邻的情况。所以可以通过使用一个vis[i]数组,记录各区间的颜色,最后只要简单处理一下就可以得到结果了。

 #include<cstring>
#include<stdio.h>
#include<iostream>
#define LC(a) ((a<<1))
#define RC(a) ((a<<1)+1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long ll;
const int N=8e3*; struct node{
ll l,r;
ll color;//颜色为-2表示混合色
}tree[N]; ll ans[N];
ll vis[N];
//下推
void pushdown(ll p){
tree[RC(p)].color=tree[LC(p)].color=tree[p].color;
}
//上推
void pushup(ll p){
tree[p].color=(tree[LC(p)].color==tree[RC(p)].color?tree[LC(p)].color:-);
} void build(ll p,ll l,ll r){
tree[p].l=l;
tree[p].r=r;
tree[p].color=-;//初始化颜色因题意而定
if(l==r){
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
} void update(ll p,ll l,ll r,ll color){
if(r<tree[p].l||l>tree[p].r)
return;
if(tree[p].color==color)
return;
if(l<=tree[p].l&&r>=tree[p].r){
tree[p].color=color;
return;
}
//**释放lazy标记
if(tree[p].color!=-){
pushdown(p);
}
update(LC(p),l,r,color);
update(RC(p),l,r,color);
pushup(p);
} void query(ll p,ll l,ll r){
if(r<tree[p].l||l>tree[p].r)
return; //纯色,不用再找下去
if(tree[p].color!=-){
//**使用vis数组记录区间颜色
for(int i=tree[p].l;i<=tree[p].r;i++){
vis[i]=tree[p].color;
}
return;
}
query(LC(p),l,r);
query(RC(p),l,r);
} int main(){
ios::sync_with_stdio(false);
ll n;
//注意是从0~n
while(cin>>n){
build(,0,);
ll maxc=;
for(int i=;i<=n;i++){
ll l,r,color;
cin>>l>>r>>color;
maxc=max(maxc,color);
update(,l+,r,color);
}
memset(ans,,sizeof(ans));
memset(vis,-,sizeof(vis));
query(,,);
//处理vis数组,统计各区段数
for(int i=;i<=;i++){
if(vis[i]!=vis[i-]&&vis[i]!=-)
ans[vis[i]]++;
}
for(int i=;i<=maxc;i++){
if(ans[i])
cout<<i<<" "<<ans[i]<<endl;
}
cout<<endl;
}
}

ZOJ 1610 Count the Colors(区间染色)的更多相关文章

  1. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  2. zoj 1610 Count the Colors 【区间覆盖 求染色段】

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

  3. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

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

  4. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  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. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

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

  7. zoj 1610 Count the Colors

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

  8. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

  9. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

随机推荐

  1. Native Wifi API

    原文链接地址:https://www.cnblogs.com/jackcin/p/3285357.html 在windows平台下,可以使用native wifi api来控制无线网卡,包括获取无线网 ...

  2. CF932E Team Work——第二类斯特林数

    题解 n太大,而k比较小,可以O(k^2)做 想方设法争取把有关n的循环变成O(1)的式子 考虑用公式: 来替换i^k 原始的组合数C(n,i)一项,考虑能否和后面的系数分离开来,直接变成2^n处理. ...

  3. 【bzoj3796】Mushroom追妹纸

    Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. ​ 求w的 ...

  4. Error: Chromium revision is not downloaded. Failed to download Chromium

    在使用prerender-spa-plugin做前端预渲染的时候,安装puppeteer的时候因为下载Chromium 失败报错,有如下解决方法: 1.使用Chromium 国内源 npm confi ...

  5. 批量修改历史commit的用户名user.name邮箱user.email

    配置当前的用户名邮箱可以当前项目配置或者全局配置. 仅当前项目配置: git config user.name 'your-user-name' git config user.email 'your ...

  6. 玲珑杯”ACM比赛 Round #19 B 维护单调栈

    1149 - Buildings Time Limit:2s Memory Limit:128MByte Submissions:588Solved:151 DESCRIPTION There are ...

  7. bzoj4774 修路

    4774: 修路 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 290  Solved: 137[Submit][Status][Discuss] D ...

  8. c语言时间计算

    C语言使用time_t结构体表示时间戳,它本质上是个long类型. 我们可以使用如下函数获取当前时间的时间戳: time_t time(time_t* timer) 函数功能:得到从标准计时点(一般是 ...

  9. tomcat启动 报org.apache.catalina.LifecycleException异常

    java 服务器 tomcat启动 报org.apache.catalina.LifecycleException异常 异常代码如下: [2018-05-10 04:45:08,856] Artifa ...

  10. CS48 D BIT

    统计一个点对应的和它严格右下方的点,点对数量.由于数据规模很大,不能直接上二维的前缀和,先排一维序,然后用BIT维护前缀和即可. /** @Date : 2017-09-14 20:17:30 * @ ...