1 条题解

  • 0
    @ 2023-2-6 13:18:05

    线段树处理区间覆盖问题,注意要离散化。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    
    int len=0;
    struct node{int l,r,lc,rc,c;bool update;};
    struct paint{int a,b;char c;};
    struct ls{int o,n;};
    node t[20020];
    paint p[10010];
    ls pp[20020];
    int ll=0;
    int n;
    
    void qsort(int l,int r)
    {
    	int i,j;
    	int m;
    	ls t;
    	i=l;j=r;
    	m=pp[(i+j)>>1].o;
    	while (i<=j)
    	{
    		while (pp[i].o<m) i++;
    		while (pp[j].o>m) j--;
    		if (i<=j)
    		{
    			t=pp[i];pp[i]=pp[j];pp[j]=t;
    			i++;j--;
    		}
    	}
    	if (l<j) qsort(l,j);
    	if (i<r) qsort(i,r);
    }
    
    int wz(int x)
    {
    	int l=1,r=ll,m;
    	while (1)
    	{
    		m=(l+r)>>1;
    		if (pp[m].o==x) break;
    		if (pp[m].o>x) r=m-1;
    		else l=m+1;
    	}
    	return pp[m].n;
    	
    }
    
    int build(int l,int r)
    {
    	len++;
    	int x=len;
    	t[x].l=l;t[x].r=r;t[x].c=0;t[x].update=false;
    	if (l+1!=r)
    	{
    		int mid;
    		mid=(l+r)>>1;
    		t[x].lc=build(l,mid);
    		t[x].rc=build(mid,r);
    	}
    	return x;
    }
    
    void update(int x)
    {
        t[x].update=false;
        t[t[x].lc].update=true;
        t[t[x].lc].c=t[x].c;
        t[t[x].rc].update=true;
        t[t[x].rc].c=t[x].c;
    }
    
    void insert(int x,int l,int r,int c)
    {
    	if (t[x].l==l && t[x].r==r)
    	{
    		t[x].c=c;
    		t[x].update=true;
    		return;
    	}
    	int mid,lc,rc;
    	mid=(t[x].l+t[x].r)>>1;
    	lc=t[x].lc;rc=t[x].rc;
    	if (t[x].update) update(x);
    	if (r<=mid) insert(lc,l,r,c);
    	else if (mid<=l) insert(rc,l,r,c);
    	else
    	{
    		insert(lc,l,mid,c);
    		insert(rc,mid,r,c);}
    	if (t[lc].c==t[rc].c) t[x].c=t[lc].c;
    	else t[x].c=-1;
    }
    
    int getcount(int x,int l,int r)
    {
    	if (t[x].l==l && t[x].r==r) 
    	{
    		return t[x].c;
    	}
    	int mid,lc,rc;
    	mid=(t[x].l+t[x].r)>>1;
    	lc=t[x].lc;rc=t[x].rc;
    	if (t[x].update) update(x);
    	if (r<=mid) return getcount(lc,l,r);
    	if (mid<=l) return getcount(rc,l,r);
    }
    
    int main()
    {
    	scanf("%d",&n);
    	pp[1].o=0;pp[2].o=1000000000;
    	ll=2;
    	for (int i=1;i<=n;i++)
    	{
    		scanf("%d %d %c",&p[i].a,&p[i].b,&p[i].c);
    		pp[++ll].o=p[i].a;pp[++ll].o=p[i].b;
    		getchar();
    	}
    	qsort(1,ll);
    	pp[1].n=1;
    	for (int i=2;i<=ll;i++)
    	{
    		if (pp[i].o==pp[i-1].o) pp[i].n=pp[i-1].n;
    		else pp[i].n=i;
    	}
    	build(1,ll);
    	for (int i=1;i<=n;i++)
    	{
    		if (p[i].a==p[i].b) continue;
    		//printf("%d %d\n",wz(p[i].a),wz(p[i].b));
    		insert(1,wz(p[i].a),wz(p[i].b),(p[i].c=='b'));
    		//printf("ok\n");
    	}
    	//printf("XX\n");
    	int now=0,ans=0,ansl,ansr,l=0,r=1;
    	for (int i=1;i<ll;i++)
    	{
    		if (getcount(1,i,i+1)==0) {r=pp[i+1].o;now=r-l;}
    		else {l=pp[i+1].o;now=0;}
    		if (now>ans) {ans=now;ansl=l;ansr=r;};
    	}
    	printf("%d %d",ansl,ansr);
    }
    
    • 1

    信息

    ID
    21
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者