链接:
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.
InputThe 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 cx1 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.
OutputEach 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 Input50 4 40 3 13 4 20 2 20 2 340 1 13 4 11 3 21 3 160 1 01 2 12 3 11 2 02 3 01 2 1 Sample Output1 12 13 11 1
0 2
1 1
一个涂色问题,我看到了好几种不同的解法, 感觉太神奇,太奇妙了,先附上自己的代码,再学习一下别人的,写了好几题了,不能追速度,我需要好好琢磨琢磨
代码:
#include#include #include #include using namespace std;#define Lson r<<1#define Rson r<<1|1const int N = 8500;struct Node{ int l, r, c;} s[N<<2];struct node{ int L, R; int color; int Mid() { return (L+R)>>1; }} a[N<<2];int Color[N];void CoverColor(int L, int R, int e){ for(int i=L; i<=R; i++) Color[i]=e;}void UpDate(int r){ if(a[r].L != a[r].R) if(a[Lson].color==1 && a[Rson].color==1) a[r].color = 1;}void BuildTree(int r, int L, int R){ a[r].L = L, a[r].R = R; a[r].color = 0; // 0代表没有颜色覆盖, 1代表未覆盖, 2代表子树有被别的颜色覆盖 if(L==R) return ; BuildTree(Lson, L, a[r].Mid()); BuildTree(Rson, a[r].Mid()+1, R);}void Insert(int r, int L, int R, int e){ if(a[r].color == 1) return ; if(a[r].L==L && a[r].R==R && !a[r].color) { a[r].color=1; CoverColor(L, R, e); return ; } a[r].color = 2; if(R<=a[r].Mid()) Insert(Lson, L, R, e); else if(L>a[r].Mid()) Insert(Rson, L, R, e); else { Insert(Lson, L, a[r].Mid(), e); Insert(Rson, a[r].Mid()+1, R, e); } UpDate(r);}int main(){ int n; while(scanf("%d", &n)!=EOF) { int i, ans[N]={ 0}; memset(s, 0, sizeof(s)); for(i=1; i<=n; i++) scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].c); BuildTree(1, 0, N-1); memset(Color, -1, sizeof(Color)); for(i=n; i>0; i--) Insert(1, s[i].l+1, s[i].r, s[i].c); for(i=0; i
队友暴力过的代码, 没用线段树
#include#include #include #include using namespace std;#define N 8006int cnt[N],a[N];int main(){ int n, p, q, c; while(scanf("%d", &n) != EOF) { int Max = 0; memset(a, 0, sizeof(a)); memset(cnt, 0, sizeof(cnt)); for(int i=0; i