kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
<b< dd="">
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
<b< dd="">
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
<b< dd="">
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
因为是线段树专题,就思考线段树,由于刚用线段树,这里的建树感觉没用,但是不确定,不过Pushup果断弃用。还有就是Query的时候是需要跑所有叶节点的,所以不需要那些判断, 大体思路正确,就是没想到用last记录。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=; //last 上一段的颜色
int lazy[maxn<<],n,ans[maxn],last;//lazy就是树,不需要处理什么信息 void Pushdown(int rt) {
if(lazy[rt]!=-) {
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
lazy[rt]=-;
}
} void Updata(int rt,int L,int R,int l,int r,int val) {
if(L>=l&&R<=r) {
lazy[rt]=val;
return;
}
Pushdown(rt);
int mid=(L+R)>>;
if(l<=mid) Updata(rt<<,L,mid,l,r,val);
if(r>mid) Updata(rt<<|,mid+,R,l,r,val);
} void Query(int rt,int L,int R,int l,int r) {//不要太死板,学会变通
if(L==R) {
if(lazy[rt]!=-&&lazy[rt]!=last) {//巧妙
ans[lazy[rt]]++;
}
last=lazy[rt];
return;
}
Pushdown(rt);
int mid=(L+R)>>;//需要遍历所有叶节点
Query(rt<<,L,mid,l,r);
Query(rt<<|,mid+,R,l,r);
} int main() {
while(~scanf("%d",&n)) {
mem(ans,);
mem(lazy,-);
int x,y,val;
while(n--) {
scanf("%d%d%d",&x,&y,&val);
Updata(,,maxn,x+,y,val);//把0 变为 1
}
last=-;
Query(,,maxn,,maxn);
rep(i,,maxn) {
if(ans[i]) printf("%d %d\n",i,ans[i]);
}
printf("\n");
}
}
kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)的更多相关文章
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
- kuangbin专题七 HDU1166 敌兵布阵 (线段树或树状数组)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- F - Count the Colors(线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
随机推荐
- 2015.1.15 利用航线id取所有点的函数创建视图
1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...
- laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl
laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl laravel 4.2 用composer 安装任何包都会报这个错,通过谷歌找到 ...
- 如何设置linux在出现kernel panic后自动重启 (ZT)
Automatic reboot after Linux kernel panic http://www.syn-ack.org/centos-linux/automatic-reboot-after ...
- AJAX——XMLHttpRequest对象的使用
AJAX是web2.0即动态网页的基础,而XMLHttpRequest对象又是AJAX的核心.XMLHttpRequest对象负责将用户信息以异步通信地发送到服务器端,并接收服务器响应信息和数据 一. ...
- WebRTC的拥塞控制技术<转>
转载地址:http://www.jianshu.com/p/9061b6d0a901 1. 概述 对于共享网络资源的各类应用来说,拥塞控制技术的使用有利于提高带宽利用率,同时也使得终端用户在使用网络时 ...
- java多线程环境单例模式实现详解
Abstract 在开发中,如果某个实例的创建需要消耗很多系统资源,那么我们通常会使用惰性加载机制,也就是说只有当使用到这个实例的时候才会创建这个实例,这个好处在单例模式中得到了广泛应用.这个机制在s ...
- c# 类的初步认识
这里我们把类分为三种: String类(字符串类):Math类(数学类):DateTime类(时间日期类). 在使用类时注意 在输入的过程中代码前面会出现一些符号(紫色立方体代表方法,函数和黑色扳手 ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
For many programs, developing a mathematical model of running timereduces to the following steps:■De ...
- Bulma 中的媒体查询
在 Bulma 中将设备分为 6 类:手机.平板.触屏设备.桌面.宽屏和大屏.提供了四个阈值. // sass/utilities/variables.sass $tablet: 769px !def ...
- 根据Value对Map中的对象进行排序
背景 SortedMap的实现类TreeMap可以按自然顺序或自定义顺序遍历键(key),有时我们需要根据值(Value)进行排序,本文提供了一种简单实现思路. 实现 Comparator接口 使用V ...