hdu-6406 + half segment tree

  The problem I think that Italy is very obvious to you, is to let you determine how to change after this connect the front and back, it is easy to feel a familiar friend out of this classic problem, changing the maximum value and if a number line in painted image, then, is the shape of a ladder, so this point there is very intuitive to raise or lower the impact.

  1. For this point is reduced: if this point is not stepped on the corner, so this point does not make any sense, the answer is the same, if the maximum front on the corner, you have to determine whether this decline is now much point (set t), had a subsequent impact on how much, and now you want to know on the first point after the value is greater than t in which you can contact.

  2. For this point was raised: If this point is not stepped on the corner, or the like after the first query is greater than its position on the line, if this point is not on the corner, but there is the possibility of a breakthrough ladder, this time prefixes the maximum value is changed, the query and the first location is greater than the new maximum empathy.

  Now we have to do with a two: i is calculated from the selected start number can be chosen, how to query the first position is greater than t in the 1p + 1 ~ n.

  The first question: Well, you can from the back stack can maintain a monotonous, when the pressure inside the can must be descending. (Specifically look at the code to understand a little better)

  The second question: to maintain maximum range with tree line, and then half on the line.

 

  Summary: This question ideas come quickly scribbled the first two rounds, the fundamental situation is very naive to think that I too stupid to promiscuity. Later pretreatment suffix actually wrote a segment tree + discrete, clearly so complex methods to solve this problem is more than one, then why I chose such a complicated thing dogs do, next time do not ponder? As well as the code appeared bug, do not know the status issue, a small detail wrong, check for a long time. The last bug is a big sucker me not calculated on nothing suffix of this situation, so silly, the idea is not rigorous.

  Expect change programs: normal estimated more difficult problem ponder, do a hot head wrote (cf feel to play more, Hu Cai up meal, but problems do not self-righteous ah), the method is too complicated time do not think he is on , and more brains to think about the other will be dead? There are next troubleshooting, comprehensive analysis of all that could happen! ! !

  

#include<iostream>
#include<cstring>
#include <string>
#include<algorithm>
#include<map>
#include<stack>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long ll;
const int maxn=2e5+20;
int tree[maxn*4];
void insert(int x,int p,int l,int r,int rt)
{
    if(l==x&&r==x)
    {
        tree[rt]=p;
        return;
    }
    int mid=(l+r)/2;
    if(x<=mid)
        insert(x,p, l, mid, lson);
    else
        insert(x,p, mid+1, r, rson);
    tree[rt]=max(tree[lson],tree[rson]);
}
int qu(int L,int R,int l,int r,int rt)
{
   if(L<=l&&r<=R)
   {
       return tree [rt]; 
   } 
   Else 
   { 
       int mid = (l + r) / 2 ;
       int years = 0 ;
       if (L <= mid) 
           years = max (years that (L, R, l, mid, lson));
       if (R> mid) 
           years = max (years that (L, R, mid + 1 , r, rson));
       return years; 
   } 
} 
Int year [maxn] ans_pre [maxn], a [maxn] h [maxn], n, m; 
map < int , int > M;
int main () 
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        
        for(int i=1;i<=n;i++){
            scanf("%d",&h[i]);
        }
        
        
        
        for(int i=0;i<=4*maxn;i++)
            tree[i]=0;
        
        for(int i=1;i<=n;i++)
            insert(i, h[i], 1, n, 1);
        stack<int> S;
        for(int i=n;i>0;i--)
        {
            while(1){
            if(S.empty()||S.top()>h[i]){
                S.push(h[i]);
                ans[i]=S.size();
                break;
            }
            else
                S.pop();
            }
        }
        
        a[1]=h[1];
        ans_pre[1]=1;
        for(int i=2,j=h[1];i<=n;i++)
        {
            if(h[i]>j)
            {
                ans_pre[i]=ans_pre[i-1]+1;
                j=h[i];
                a[i]=h[i];
            }
            else
            {
                a[i]=a[i-1];
                ans_pre[i]=ans_pre[i-1];
            }
        }
        
        for(int i=0;i<m;i++)
        {
            int p,q;
            scanf("%d%d",&p,&q);
            
            int ans1=ans_pre[p];
            if(a[p]<q)
            {
                if(p!=1&&a[p]==a[p-1])
                    ans1++;
                
                int l=p+1,r=n;
                if(l<=r&&qu(p+1, n, 1, n, 1)>q){
                    while(r>l+1)
                    {
                        int mid=(l+r)/2;
                        if(qu(l, mid, 1, n, 1)>q)
                            r=mid;
                        else
                            l=mid;
                    }
                    if(h[l]<=q)
                        l=r;
                    ans1+=ans[l];
                }
            }
            else if(p==1)
            {
                int l=p+1,r=n;
                if(l<=r&&qu(p+1, n, 1, n, 1)>q){
                    while(r>l+1)
                    {
                        int mid=(l+r)/2;
                        if(qu(l, mid, 1, n, 1)>q)
                            r=mid;
                        else
                            l=mid;
                    }
                    if(h[l]<=q)
                        l=r;
                    ans1+=ans[l];
                }
                
            }
            else if(a[p]!=a[p-1])
            {
                if(q<=a[p-1]){
                    ans1--;
                    q=a[p-1];
                }
                
                int l=p+1,r=n;
                if(l<=r&&qu(p+1, n, 1, n, 1)>q){
                    while(r>l+1)
                    {
                        int mid=(l+r)/2;
                        if(qu(l, mid, 1, n, 1)>q)
                            r=mid;
                        else
                            l=mid;
                    }
                    if(h[l]<=q)
                        l=r;
                    ans1+=ans[l];
                }
            }
            else
                ans1=ans_pre[n];
            printf("%d\n",ans1);
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/King-of-Dark/p/11616403.html