Time Limit:1000MS

Memory Limit:65536K

## Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.

## Input

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either ‘even’ or ‘odd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where ‘even’ means an even number of ones and ‘odd’ means an odd number).

## Output

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

## Solution

P.S.这题其实是个骗分的好题（ACMer们别打我。。。）。不离散化最少70分。如果内存放宽一点就有80分。即使奇偶搞反了都有50分……总之怎么着都能得分的。CEOI的标程好像不是用并查集做的，但是要慢一些。就不管它了。

URAL的数据貌似有问题，就再不管了。  #include <cstdio>
#include <cassert>
#include <cstring>
#define NDEBUG
//#define FILE_IO
using namespace std;
#ifdef FILE_IO
FILE *fin = fopen("INPUT.TXT","r");
FILE *fout= fopen("OUTPUT.TXT","w");
#else
FILE *fin = stdin;
FILE *fout= stdout;
#endif
const unsigned short min_a=0, max_b=8000;
unsigned short N;
struct STNode{
unsigned short a,b,lch,rch;
short color;    //-1为无色，-2为杂色
STNode(unsigned short __a, unsigned short __b){
a=__a;
b=__b;
color=-1;
lch=rch=0;
}
STNode(void){;}
};
STNode STree[(max_b-min_a)*2];
unsigned short i;
void ST_Build(unsigned short a, unsigned short b){
unsigned now=i++;
STree[now]=STNode(a,b);
if(b-a>1){
STree[now].lch=i;
ST_Build(a,(a+b)>>1);
STree[now].rch=i;
ST_Build((a+b)>>1,b);
}
}
void ST_Insert(unsigned short i,unsigned short a, unsigned short b, short color){
assert(a<b);
if(STree[i].color==color)   return;
if(a<=STree[i].a&&STree[i].b<=b)    STree[i].color=color;
else{
if(STree[i].color!=-2&&STree[i].color!=-1){
STree[STree[i].lch].color=STree[i].color;
STree[STree[i].rch].color=STree[i].color;
}
STree[i].color=-2;
unsigned short m=((STree[i].a+STree[i].b)>>1);
if(a<m) ST_Insert(STree[i].lch,a,b,color);
if(b>m) ST_Insert(STree[i].rch,a,b,color);
}
}
unsigned colors;
void SgCount(unsigned short i, short &a_color, short &b_color){
//在在color中增加以i节点为根的ST中各色的线段数目
//并返回最左、最右端的颜色分别为a_color和b_color
if(STree[i].color!=-2){
a_color=b_color=STree[i].color;
if(STree[i].color!=-1)  colors[STree[i].color]++;
}
else{
short m1_color,m2_color;
SgCount(STree[i].lch,a_color,m1_color);
SgCount(STree[i].rch,m2_color,b_color);
if(m1_color==m2_color&&m1_color!=-1)
colors[m1_color]--;
}
}
int main()
{
while(fscanf(fin,"%dn",&N)==1){
i=1;
ST_Build(min_a,max_b);
for(int i=0,a,b,color;i<N;i++){
fscanf(fin,"%d %d %dn",&a,&b,&color);
ST_Insert(1,a,b,color);
}
memset(colors,'',sizeof(colors));
short a_color,b_color;
SgCount(1,a_color,b_color);
for(int i=0;i<=8000;i++)
if(colors[i]!=0)    fprintf(fout,"%d %dn",i,colors[i]);
fprintf(fout,"n");
}
return 0;
}