bzoj 4383 [POI2015] Desert

LINK: POI2015 Desert

Meaning of the title is no longer said to be larger than the rest of the numbers and determine whether the solvability and output solutions.

Consider the difference constraint model. For even Because each side of the interval is too large we can not go even violent side so consider optimizing the construction segment tree diagram.

Complexity: Since \ (\ sum k \ leq 3 * 10 ^ 5 \) so Klogn up even though each side of a point is the need to create in the acceptable range edges.

Analyzing no solution to consider how a> b can be converted into a> = b + 1 to change that situation b <= a-1 a program to do output b even if such an edge to be awkward.

Original form to a> = b + 1 b represents a connected to the side at least to a> = b + 1 is not always large run spfa FIG.

But given the special nature of this picture. This is a directed acyclic graph if there is no solution ring so inevitable.

Finally, we consider how to construct a solution and then look forward topsort dp similar form solution can be obtained.

Finally, of course, also need to determine whether the proper range.

const int MAXN=100010,maxn=2000010;
int n,m,v,flag,cnt,len,root;
int a[MAXN],f[maxn],q[maxn];
int lin[maxn],ver[maxn],nex[maxn],e[maxn],ru[maxn];
struct wy{int l,r;}t[MAXN<<2];
inline void add(int x,int y,int z)
{
    ver[++len]=y;
    nex[len]=lin[x];
    lin[x]=len;
    e[len]=z;
    ++ru[y];
}
inline void build(int &p,int l,int r)
{
    p=++cnt;
    if(l==r){add(p,l,0);return;}
    int mid=(l+r)>>1;
    build(l(p),l,mid);
    build(r(p),mid+1,r);
    add(p,l(p),0);add(p,r(p),0);
}
inline void change(int p,int l,int r,int L,int R,int x)
{
    if(L>R)return;
    if(L<=l&&R>=r){add(x,p,1);return;}
    int mid=(l+r)>>1;
    if(L<=mid)change(l(p),l,mid,L,R,x);
    if(R>mid)change(r(p),mid+1,r,L,R,x);
}
inline void topsort()
{
    int l=0,r=0;
    for(int i=1;i<=cnt;++i)
    {
        f[i]=INF;
        if(!ru[i])q[++r]=i;
    }
    while(++l<=r)
    {
        int x=q[l];
        if(f[x]<1){flag=1;return;}
        if(x<=n&&a[x])
        {
            if(f[x]<a[x]){flag=1;return;}
            else f[x]=a[x];
        }
        for(int i=lin[x];i;i=nex[i])
        {
            int tn=ver[i];
            f[tn]=min(f[tn],f[x]-e[i]);
            --ru[tn];if(!ru[tn])q[++r]=tn;
        }
    }
    for(int i=1;i<=cnt;++i)if(ru[i])flag=1;
}
int main()
{
    freopen("1.in","r",stdin);
    get(n);get(v);get(m);
    for(int i=1;i<=v;++i)
    {
        int x;
        get(x);get(a[x]);
        if(a[x]>INF)flag=1;
    }
    if(flag){puts("NIE");return 0;}
    cnt=n;build(root,1,n);
    rep(1,m,i)
    {
        int x,y,k,p;
        get(x);get(y);get(k);
        ++cnt;
        rep(1,k,j)
        {
            get(p);
            add(p,cnt,0);
            if(j==1){if(x<p)change(root,1,n,x,p-1,cnt);}
            else if(x+1!=p)change(root,1,n,x+1,p-1,cnt);
            x=p;
        }
        if(p<y)change(root,1,n,p+1,y,cnt);
    }
    topsort();
    if(flag){puts("NIE");return 0;}
    puts("TAK");
    rep(1,n,i)printf("%d ",f[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/chdy/p/12466623.html