社区讨论

传说中用线段树刷。。

P1204[USACO1.2] 挤牛奶 Milking Cows参与者 23已保存回复 24

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
24 条
当前快照
1 份
快照标识符
@lo0tf5os
此快照首次捕获于
2023/10/22 09:53
2 年前
此快照最后确认于
2025/11/18 20:09
4 个月前
查看原帖
奇葩的第五点运行时错误。。怎么回事orz。。
CPP
#include <iostream>
#include <stdio.h>
using namespace std;
struct node{int aa,bb,cov;};
node tree[20000001];
int a[5001],b[5001];
int i,j,k,l,m,n,p,q,x,y,z,tmp1,tmp2,maxx,minn;
void buildtree(int l,int r,int x)
{
     int mid;
     tree[x].aa=l;
     tree[x].bb=r;
     mid=(l+r)/2;
     //cout<<l<<' '<<r<<endl; system("pause");
     if (r-l>1)
     {
               buildtree(l,mid,x*2);
               buildtree(mid,r,x*2+1);
     }
}
void cover(int l,int r,int x)
{
     int i,j,mid;
     mid=(tree[x].aa+tree[x].bb)/2;
     if ((l==tree[x].aa)&&(r==tree[x].bb)){tree[x].cov=1; return;} else
     if (r<=mid){cover(l,r,x*2);} else
     if (l>=mid){cover(l,r,x*2+1);} else
     {
                                    cover(l,mid,x*2);
                                    cover(mid,r,x*2+1);
     }
}
void countt(int x)
{
     int i,j;
     if (tree[x].cov==1){tmp1=tmp1+tree[x].bb-tree[x].aa; if (tmp2>minn) minn=tmp2; tmp2=0; return;};
     //if (tree[x].aa>=300){cout<<tree[x].aa<<' '<<tree[x].bb<<' '<<tmp1<<' '<<tmp2<<' '<<maxx<<' '<<minn<<endl; system("pause");};
     if ((tree[x].cov==0)&&(tree[x].bb-tree[x].aa==1)&&(tree[x].aa>=q)&&(tree[x].bb<=p)){ if (tmp1>maxx){maxx=tmp1;}; tmp1=0; tmp2++;};
     if (tree[x].bb-tree[x].aa>1)
     {
                                 i=tmp1; j=tmp2;
                                 countt(x*2);
                                 countt(x*2+1);
     }
}
int main()
{
    scanf("%d",&n);
    buildtree(1,1000000,1);
    p=0; q=2000000000;
    for (i=1; i<=n; i++)
    {
        scanf("%d%d",&x,&y);
        cover(x,y,1);
        if (y>p) p=y;
        if (x<q) q=x;
    }
    maxx=0; minn=0;
    tmp1=0; tmp2=0;
    countt(1);
    if (tmp1>maxx) maxx=tmp1;
    if (tmp2>minn) minn=tmp2;
    printf("%d %d",maxx,minn);
    system("pause");
    return 0;
}

回复

24 条回复,欢迎继续交流。

正在加载回复...