Luogu P2471 - [SCOI2007] rainfall

Keywords: PHP less

Local AC submits RE...

Portal: QAQQAQ

 

Look at it by yourself

 

Thought: it is said that RMQ can be used for this question very quickly, but the line tree of this question is OK

The line tree maintains the leftmost and rightmost years of a section, whether there is a gap and the maximum value of the section.

The classification and discussion of this question is a big difficulty, mainly divided into the following situations:

1.AB is uncertain, output may

2.A determines B is uncertain:

If A is A to B maximum, output may

Otherwise output false

3.A uncertainty B determination:

If B is lower than bound (A) to B max, then output may

Otherwise output false

4.A determination B determination

If B is A+1 to B maximum, A is A to B maximum, and there is no gap between them, output true

If the above conditions are met, but there is a gap, the output may be

Otherwise output false

 

In terms of code implementation, I didn't consider the condition of "strictly less than". I thought that the maximum value was equal to B, but I didn't judge whether it was equal before.

Then I added some special judgments to prevent my metaphysical line tree from crossing the boundary and not stopping... (the code implementation ability needs to be improved recently...)

 

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
  
int n;
struct node
{
    int ly,ry,max_r,Gap;
}tree[N*4];
int rain[N],year[N];
  
void push_up(node& fa,node ls,node rs)
{
    fa.ly=ls.ly;
    fa.ry=rs.ry;
    fa.max_r=max(ls.max_r,rs.max_r);
    fa.Gap=0;
    if(ls.Gap||rs.Gap) fa.Gap=1;
    if(ls.ry+1<rs.ly) fa.Gap=1;
}
  
void build(int x,int l,int r)
{
    if(l==r)
    {
        tree[x].ly=year[l];
        tree[x].ry=year[l];
        tree[x].max_r=rain[l];
        tree[x].Gap=0;
        return;
    }
    int mid=(l+r)>>1;
    build(x+x,l,mid);
    build(x+x+1,mid+1,r);
    push_up(tree[x],tree[x+x],tree[x+x+1]);
}
  
node query(int x,int l,int r,int L,int R)
{
    node ret;
    if(L<=l&&R>=r) return tree[x];
    int mid=(l+r)>>1;
    if(mid<L) return query(x+x+1,mid+1,r,L,R);
    if(mid>=R) return query(x+x,l,mid,L,R);
    node ls=query(x+x,l,mid,L,R);
    node rs=query(x+x+1,mid+1,r,L,R);
    push_up(ret,ls,rs);
    return ret;
}
  
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&year[i],&rain[i]);
    build(1,1,n);
    int q; scanf("%d",&q);
    while(q--)
    {
        int x,y,xx,yy,bl=0;
        scanf("%d%d",&xx,&yy);
        if(yy<xx)
        {
            puts("false");
            continue;
        }
        if(xx==yy) 
        {
            puts("maybe");
            continue;
        }
        x=lower_bound(year+1,year+n+1,xx)-year;
        y=lower_bound(year+1,year+n+1,yy)-year;
        if(xx!=year[x]&&yy!=year[y]) puts("maybe");
        else if(xx!=year[x])
        {
            if(y==1||x==y) puts("maybe");
            else
            {
                node n1=query(1,1,n,x,y);
                node n2=query(1,1,n,x,y-1);
                //The intermediate rainfall must be strictly less than! 
                if(n1.max_r==rain[y]&&n2.max_r<rain[y]) puts("maybe");
                else puts("false");
            }
        }
        else if(yy!=year[y])
        {
            if(x==n||x==y-1) puts("maybe");
            else
            {
                node n1=query(1,1,n,x,y-1);
                node n2=query(1,1,n,x+1,y-1);
                if(n1.max_r==rain[x]&&n2.max_r<rain[x]) puts("maybe");
                else puts("false");
            }
        }
        else
        {
            if(x+1==y)
            {
                if(rain[x]<rain[y]) puts("false");
                else if(year[x]+1==year[y]) puts("true");
                else puts("maybe");
                continue;//Not written before continue Ah ah... 
            }//Special judgment to prevent the line tree from crossing the boundary 
            node n1=query(1,1,n,x,y);
            node n2=query(1,1,n,x+1,y);
            node n3=query(1,1,n,x+1,y-1);
            if(n2.max_r==rain[y]&&rain[y]<=rain[x]&&n3.max_r<rain[y])
            {
                if(n1.Gap) puts("maybe");
                else puts("true");
            }
            else puts("false");
        }
    }
    return 0;
}

Posted by abelajohnb on Mon, 21 Oct 2019 09:12:40 -0700