题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
Time Limit: 2 Seconds Memory Limit: 65536 KB
Problem Description
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.
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.
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
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
Problem solving report:
Description: 有一根[0,8000]的数轴,有n次操作,每次操作给l到r的区间染成数字代号为C的颜色。最后输出在数轴上出现的颜色,且这种颜色覆盖的区间有几个。
Problem solving: 最大值8000,数值很小,可以直接暴力。注意染色的是段不是点,所以我们要把每一段转化成右端点,所以染色的是左开右闭的。
Accepted Code:
//线段树
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005;
struct Tree {
int val, Mark;
}segTree[MAXN << 2];
int ans[MAXN], col;
void PushDown_(int rt, int val) {
segTree[rt].val = val;
segTree[rt].Mark = val;
}
void PushDown(int rt) {
if (~segTree[rt].Mark) {
PushDown_(rt << 1, segTree[rt].Mark);
PushDown_(rt << 1 | 1, segTree[rt].Mark);
segTree[rt].Mark = -1;
}
}
void Update(int Ql, int Qr, int val, int l, int r, int rt) {
if (Ql <= l && Qr >= r) {
segTree[rt].val = val;
segTree[rt].Mark = val;
return ;
}
PushDown(rt);
int mid = l + (r - l >> 1);
if (Ql <= mid)
Update(Ql, Qr, val, l, mid, rt << 1);
if (Qr > mid)
Update(Ql, Qr, val, mid + 1, r, rt << 1 | 1);
}
void Query(int l, int r, int rt) {
if (!(l != r)) {
if (~segTree[rt].val && segTree[rt].val != col)
ans[segTree[rt].val]++;
col = segTree[rt].val;
return ;
}
PushDown(rt);
int mid = l + (r - l >> 1);
Query(l, mid, rt << 1);
Query(mid + 1, r, rt << 1 | 1);
}
int main() {
int n;
while (~scanf("%d", &n)) {
col = -1;
int maxv = -1;
memset(ans, 0, sizeof(ans));
memset(segTree, -1, sizeof(segTree));
for (int i = 0; i < n; i++) {
int l, r, v;
scanf("%d%d%d", &l, &r, &v);
maxv = max(maxv, v);
Update(l + 1, r, v, 1, 8000, 1);
}
Query(1, 8000, 1);
for (int i = 0; i <= maxv; i++)
if (ans[i])
printf("%d %d\n", i, ans[i]);
printf("\n");
}
return 0;
}
//暴力
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005;
int spt[MAXN], ans[MAXN];
int main() {
int n;
while (~scanf("%d", &n)) {
int maxr = -1, maxv = -1;
memset(ans, 0, sizeof(ans));
memset(spt, -1, sizeof(spt));
for (int i = 0; i < n; i++) {
int l, r, v;
scanf("%d%d%d", &l, &r, &v);
for (int j = l + 1; j <= r; j++)
spt[j] = v;
maxr = max(maxr, r);
maxv = max(maxv, v);
}
for (int i = 0; i < maxr; i++)
if (~spt[i] && spt[i] != spt[i + 1])
ans[spt[i]]++;
ans[spt[maxr]]++;
for (int i = 0; i <= maxv; i++)
if (ans[i])
printf("%d %d\n", i, ans[i]);
printf("\n");
}
return 0;
}