Luo Gu P2471 - [SCOI2007] rainfall

The machine AC submitted RE ......

Portal: QAQQAQ

 

Meaning of the questions: they look

 

Ideas: This question is said to do quickly with RMQ, but this question is a segment tree

Segment tree maintenance period interval in the left-most, right-most years, whether there is a gap and a maximum range

This question is a major difficulty discussing classification, divided into the following situations:

1.AB not determined, the output maybe

2.A determine B uncertainty:

  If A is the A to B, the maximum output maybe

  Otherwise false output

3.A uncertain B determines:

  If B is lower_bound (A) B to the maximum, the output maybe

  Otherwise false output

4.A OK B OK

  If B is A + 1 B to the maximum, the maximum of A to A is B, with no gaps between the true output

  If the above conditions are satisfied, but there is a gap, the output maybe

  Otherwise false output

 

In the code implementation, I did not at first consider whether equal before the "strictly less than" the condition that the maximum value is equal to B on the line, but there is no judgment

Then added some special judge to prevent the cross-border segment tree own metaphysics not stop. . . (Recently code implementation capacity should be improved ah ...)

 

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);
                //中间降雨量必须严格小于! 
                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 ... 
            } // Laid determination prevented segment tree bounds 
            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) the puts ( " Maybe ");
                else puts("true");
            }
            else puts("false");
        }
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Forever-666/p/11210715.html