http://acm.timus.ru/problem.aspx?space=1&num=1019
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 200100
using namespace std;
struct node
{
int r,l,co;
}tree[maxn*4];
int co[maxn],l[maxn],r[maxn];
struct node1
{
int x,y,co;
}p[maxn*4];
void build(int i,int l,int r)
{
tree[i].l=l;tree[i].r=r;
tree[i].co=1;
if(l==r-1) return ;
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid,r);
}
void update(int i,int l,int r,int co)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].co=co;
return ;
}
if(tree[i].co>=0&&tree[i].co!=co)
{
tree[i<<1].co=tree[i<<1|1].co=tree[i].co;
tree[i].co=-1;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
{
update(i<<1,l,r,co);
}
else if(l>=mid)
{
update(i<<1|1,l,r,co);
}
else
{
update(i<<1,l,mid,co);
update(i<<1|1,mid,r,co);
}
}
void search1(int i,int l,int r)
{
if(tree[i].co>=0)
{
for(int j=tree[i].l; j<tree[i].r; j++)
{
co[j]=tree[i].co;
}
return ;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
{
search1(i<<1,l,r);
}
else if(l>=mid)
{
search1(i<<1|1,l,r);
}
else
{
search1(i<<1,l,mid);
search1(i<<1|1,mid,r);
}
}
int bs(int key,int li,int ri)
{
int low=li,high=ri-1;
while(low<=high)
{
int mid=(low+high)>>1;
if(l[mid]==key) return mid;
else if(l[mid]>key)
high=mid-1;
else
low=mid+1;
}
}
int main()
{
int n;
char ch;
scanf("%d",&n);
int cnt=0;
for(int i=0; i<=maxn; i++)
{
co[i]=1;
}
p[0].x=0;p[0].y=1000000000;
l[cnt++]=p[0].x; l[cnt++]=p[0].y;
for(int i=0; i<n; i++)
{
scanf("%d%d %c",&p[i].x,&p[i].y,&ch);
l[cnt++]=p[i].x; l[cnt++]=p[i].y;
if(ch=='w')
{
p[i].co=1;
}
getchar();
}
sort(l,l+cnt);
int t=1;
for(int i=1; i<cnt; i++)
{
if(l[i]!=l[i-1]) l[t++]=l[i];
}
build(1,0,t);
for(int i=0; i<n; i++)
{
int li=bs(p[i].x,0,t);
int ri=bs(p[i].y,0,t);
//printf("%d %d\n",li,ri);
update(1,li,ri,p[i].co);
}
search1(1,0,t);
int s=0,e=0,si,ei;
co[t]=0;
l[t]=1000000000;
for(int i=0; i<t; i++)
{
si=l[i];
while(co[i]==1)
i++;
ei=l[i];
if(ei-si>e-s)
{
e=ei;
s=si;
}
}
printf("%d %d\n",s,e);
return 0;
}