Luogu P4755 Beautiful Pair (maximum partition)

Title
pretreatment \ (L [p] \) represents a value greater than the first of its left position equal to its number plus one, \ (R & lt [P] \) represents a value which is greater than or equal to the right of the first position of the number of its minus one.
Consider for \ (P \) , the maximum value of \ (P \) of the interval \ ([L [P], R & lt [P]] \) , i.e. the left end point \ ([L [p], p] \) , the right end in \ ([P, R & lt [P]] \) .
We take a period in which the length of the small.
For example, \ ([L [p], p] \) This is a rather short, so \ (\ FORALL i \ in [L [the p-], the p-] \) , the answer will be added \ ([p, R [p ]] \) of greater than or equal \ (\ lfloor \ frac {a [p]} {a [i]} \ rfloor \) of the counted number.
Receiving at its repellent into \ ([1, R [p ]] \) is greater than the \ (\ lfloor \ frac {a [p]} {a [i]} \ rfloor \) number of values minus \ ([1, p-1 ] \) of greater than or equal \ (\ lfloor \ frac {a [p]} {a [i]} \ rfloor \)The number of numbers.
If \ ([p, R [p ]] \) is relatively short to do the same operation.
Can be shown that the complexity of not more than \ (nlog \ the n-\) .
We chose to use Fenwick tree maintenance after discrete.

#include<bits/stdc++.h>
#define P pair<int,int>
#define mp make_pair
#define fir first
#define sec second
#define pb push_back
#define LL long long
using namespace std;
const int N=100007;
int a[N],t[N],L[N],R[N],n,m;
LL s[N];
P stk[N];
vector<int>vec[N];
int abs(int a){return a<0? -a:a;}
int read(){int x;scanf("%d",&x);return x;}
int lowbit(int x){return x&-x;}
int Find(int x){return x>=t[n]? n:upper_bound(t+1,t+m+1,x)-(t+1);}
LL query(int x)
{
    LL sum=0;
    while(x) sum+=s[x],x-=lowbit(x);
    return sum;
}
void update(int x,int v){while(x<=n)s[x]+=v,x+=lowbit(x);}
int main()
{
    n=read();int i,j,top;LL ans=0;
    for(i=1;i<=n;++i) a[i]=t[i]=read();
    for(i=1,top=0;i<=n;++i)
    {
    while(top&&stk[top].fir<a[i]) --top;
    L[i]=(top? stk[top].sec+1:1),stk[++top]=mp(a[i],i);
    }
    for(i=n,top=0;i;--i)
    {
    while(top&&stk[top].fir<=a[i]) --top;
    R[i]=(top? stk[top].sec-1:n),stk[++top]=mp(a[i],i);
    }
    for(i=1;i<=n;++i)
    if(i-L[i]<=R[i]-i) for(vec[i-1].pb(-1),vec[R[i]].pb(1),j=L[i];j<i;++j) vec[i-1].pb(-a[i]/a[j]),vec[R[i]].pb(a[i]/a[j]);
        else for(vec[L[i]-1].pb(-1),vec[i].pb(1),j=i+1;j<=R[i];++j) vec[L[i]-1].pb(-a[i]/a[j]),vec[i].pb(a[i]/a[j]);
    sort(t+1,t+n+1),m=unique(t+1,t+n+1)-(t+1);
    for(i=1;i<=n;++i) a[i]=lower_bound(t+1,t+m+1,a[i])-t;
    for(i=1;i<=n;i++) for(update(a[i],1),top=vec[i].size(),j=0;j<top;++j) vec[i][j]<0? ans-=query(Find(abs(vec[i][j]))):ans+=query(Find(abs(vec[i][j])));
    return !printf("%lld",ans);
}

Guess you like

Origin www.cnblogs.com/cjoierShiina-Mashiro/p/11512299.html