Topic links
Test instructions
A few segments of each color;
Analog Arrays:
#include <stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespacestd;#defineN 8006intCnt[n],a[n];intMain () {intN, p, Q, C; while(SCANF ("%d", &n)! =EOF) { intMax =0; Memset (A,0,sizeof(a)); memset (CNT,0,sizeof(CNT)); for(intI=0; i<n; i++) {scanf ("%d%d%d", &p, &q, &c); for(intI=s; j<q; J + +) A[j]= c+1; Max=Max (max, q); } for(intI=0; i<max; i++) { while(i!=0&& a[i]!=0&& a[i]==a[i-1]) I++; if(A[i]! =0) cnt[A[i]-1]++; } for(intI=0; i<n; i++) { if(cnt[i]!=0) printf ("%d%d\n", I, cnt[i]); } printf ("\ n"); } return 0;}View Code
#include <stdio.h>#include<string.h>#defineN 8005structsegtree{intL,r,c; intmid () {return(l+r) >>1; }}a[n*4];intCnt[n], vis[n];voidBuildsegtree (intRintLintR) {A[r]. L=L; A[R]. R=R; A[R].C= -1; if(L +1==R)//the two endpoints of a line segment differ by 1, return ; Buildsegtree (R*2, L, A[r].mid ()); Buildsegtree (R*2+1, A[r].mid (), R);//The topic is the endpoint value, for the line segment can not be used a[r].mid () +1;}voidUpdate (intRintLintRintc) { if(a[r].c = = c)return;//if the color of this line segment is the same as the one to be painted, return; if(A[r]. L = = L && a[r]. R = =R) {a[r].c= C;//change to color C in the specified range; return ; } if(A[r].c! =-1) {a[2*R].C = a[2*r+1].C = a[r].c;//update left and right subtree;A[R].C =-1; } if(r<=A[r].mid ()) Update (2*R, L, R, c); Else if(L>=a[r].mid ())//The equal number cannot be saved, because this is a line segment is not a point;Update (2*r+1, L, R, C); Else{Update (2*R, L, A[r].mid (), c); Update (2*r+1, A[r].mid (), R, c); }}voidQuery (intRintLintR) { if(A[r].c! =-1)//reach the leaf node; { if(Vis[l]! =a[r].c) cnt[a[r].c]++; VIS[R]=a[r].c; return ; } if(R-l = =1) return ; Query (2*R, L, A[r].mid ()); Query (2*r+1, A[r].mid (), r);}intMain () {intN, p, Q, C; while(SCANF ("%d", &n)! =EOF) {memset (CNT,0,sizeof(CNT)); memset (Vis,-1,sizeof(VIS)); Buildsegtree (1,0,8000); while(n--) {scanf (" %d%d%d", &p, &q, &c); if(p = = q)Continue; Update (1, p, Q, C); } Query (1,0,8000); for(intI=0; i<n; i++) { if(Cnt[i]! =0) printf ("%d%d\n", I, cnt[i]); } printf ("\ n"); } return 0;}View Code
Count the Colors---zoj1610 segment tree