Topic links
POJ2777 Topic
There is a length of N (N≤\le 105 10^5) axis, there is a T (t≤\le30) color, there are M operations (M≤\le 105 10^5), the operation has the following two kinds:
c A B C: Dye A and the middle C color
P a B: Query A and B in the middle there are several colors
The initial color of the axis is 1
Note: A may be greater than B Analysis
This is a typical interval coverage problem that can be solved with a line segment tree. We use the Segment tree array col[] to record the color information of the interval, and if a range contains multiple colors, then the col[] value is-1. "C" operation is the normal line segment tree interval update, "P" operation needs to use a global variable CNT to the feedback interval there are several different colors, and need a book[i] to record whether the first color has occurred, in the query interval to find the color of the cnt++, The last CNT is the number of different colors in the interval.
The left end of the interval given by this question can be greater than the right end of the AH ... This I did not notice at the beginning, so write about the interval of the topic also beware of the left endpoint is greater than the right end of the situation. Code
#include <iostream> #include <cstdio> #include <cstring> #define LS (rt<<1) #define RS (rt<<
; 1|1) using namespace std;
const int maxn=100010;
int n,t,m,cnt,col[maxn<<2],lazy[maxn<<2],book[35]; Col[] Record color information, lazy[] is lazy tag, book[] flag color is counted in the interval void pushup (int rt)//If there are multiple colors in the node corresponding interval, the color value is-1 {if (col[ls]==-1| |
Col[rs]==-1) Col[rt]=-1;
else if (Col[ls]==col[rs]) col[rt]=col[ls];
else Col[rt]=-1;
} void Build (int l,int r,int RT) {if (l==r) {col[rt]=1;//initial color is 1 return;
} int mid= (L+R) >>1;
Build (L,MID,LS);
Build (MID+1,R,RS);
Pushup (RT);
} void Pushdown (int rt)//delay update, push tag {if (Lazy[rt]) {COL[LS]=COL[RS]=LAZY[RT];
LAZY[LS]=LAZY[RS]=LAZY[RT];
lazy[rt]=0;
}} void Update (int l,int r,int c,int l,int r,int RT) {if (l<=l&&r<=r) {col[rt]=c;
Lazy[rt]=c;
Return
} int mid= (L+R) >>1; Pushdown (RT);
if (l<=mid) Update (L,R,C,L,MID,LS);
if (r>mid) Update (L,R,C,MID+1,R,RS);
Pushup (RT);
} void Query (int l,int r,int l,int r,int RT) {if (col[rt]>0) {if (!book[col[rt]])//Current color has not been counted
{cnt++;
Book[col[rt]]=1;
} return;
} pushdown (RT);
int mid= (L+R) >>1;
if (l<=mid) Query (L,R,L,MID,LS);
if (r>mid) Query (L,R,MID+1,R,RS);
} int main () {int i,l,r,c;
Char op;
while (scanf ("%d%d%d", &n,&t,&m)!=eof) {Build (1,n,1);
memset (lazy,0,sizeof (lazy));
for (i=1;i<=m;i++) {GetChar ();
scanf ("%c", &op);
if (op== ' C ') {scanf ("%d%d%d", &l,&r,&c);
if (l<r) Update (l,r,c,1,n,1);
else Update (r,l,c,1,n,1); } if (op== ' P ') {scanf ("%d%d", &l,&r);
cnt=0;//the number of different colors in the statistical interval memset (book,0,sizeof (book));
if (l<r) Query (l,r,1,n,1);
else Query (r,l,1,n,1);
printf ("%d\n", CNT);
}}} return 0;
}
Look at the other person's solution to know that the problem can also be done with a bitwise operation, but I do not understand, so then fill this practice.