Count the Colors ZOJ - 1610

传送门

线段树区间染色求染色的片段数

#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define P pair<int,int>
const ll INF=1e18;
const int N=8000+10;
int ans[N];
struct SegmentTree{
int l,r;
int dat;
}t[N*4];
void build(int p,int l,int r)
{
t[p].l = l;
t[p].r = r;
t[p].dat = -1;
if(t[p].l == t[p].r){t[p].dat = -1;return ;}
int mid = (l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
}
void spread(int p)
{
if(t[p].dat>=0)
{
t[p*2].dat = t[p].dat;
t[p*2+1].dat = t[p].dat;
t[p].dat = -1;
}
}
void change(int p,int l,int r,int v)
{
if(l <= t[p].l && t[p].r <= r)
{
t[p].dat = v;
return ;
}
int mid = (t[p].l+t[p].r)/2;
spread(p);
if(l<=mid) change(p*2,l,r,v);
if(r>mid) change(p*2+1,l,r,v);
}
int ask(int p,int l,int r)
{
if(t[p].l == t[p].r)
{
return t[p].dat;
}
int mid = (t[p].l+t[p].r)/2;
spread(p);
if(l<=mid) return ask(p*2,l,r);
if(r>mid) return ask(p*2+1,l,r);
}
int n;
int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
build(1,1,8000);
for(int i=0;i<=8000;i++)
{
ans[i] = 0;
}
for(int i=1;i<=n;i++)
{
int l,r,d;
cin >> l >> r >> d;
change(1,l+1,r,d);
}
int last = -1,now;
for(int i=1;i<=8000;i++)
{
now = ask(1,i,i);
if(now!=last && now!=-1)
ans[now]++;
last = now;
}
for(int i=0;i<=8000;i++)
{
if(ans[i])
{
cout << i <<" "<< ans[i] <<"\n";
}
}
cout << "\n";
}
return 0;
}

最新文章

  1. Microsoft Visual C++ Compiler for Python
  2. [UML]UML系列——类图Class
  3. java Servlet Filter 拦截Ajax请求
  4. Binary Tree Postorder Traversal--leetcode难题讲解系列
  5. 很好用的在线markdown编辑器
  6. Python 描述符(descriptor) 杂记
  7. 树的基本操作java版
  8. 常用加密算法的Java实现总结
  9. JAVA对象之生
  10. linux-3.0下input_dev模型按键驱动
  11. 客户端Webview重定向
  12. win8系统不小心禁用了管理员权限怎么解决
  13. 关于centos 7 systemctl自定义服务笔记
  14. SpringBoot整合多数据源实现
  15. python开发环境配置和python源码打包生成exe可执行文件
  16. Jmeter接口测试实例
  17. python pycharm pyqt 安装
  18. 【树莓派】使用xdrp远程登录树莓派的图形界面
  19. BAT-批量改文件后缀名
  20. lua中实现倒计时

热门文章

  1. 基于Flask框架搭建视频网站的学习日志(三)之原始web表单
  2. Shell常用语句及结构
  3. spring cloud oauth2搭建认证中心与资源中心
  4. Excel学习——VBA学习(一)
  5. jQuery下载所有版本
  6. ConnectWeb
  7. python学习记录(五)
  8. CCF_201604-2_俄罗斯方块
  9. 【Detection】物体识别-制作PASCAL VOC数据集
  10. 【5min+】 巨大的争议?C# 8 中的接口