test 190730 Genting Yi


Problem Description

Genting Yi is a very popular recently self-propelled chess game, players need to extract the card to upgrade equipment and to win the game through clever layout.

The key to winning the game is a good strategy, but luck is also essential.
Good chance the company did not lose a game fishing gold, so, if you want to smoke less than the card can be purchased at the store.
Of course, the game the company will also use people's chances, let them pay for the opportunity to draw cards. stores were guaranteed even draw a card c times will be able to achieve the desired.
in particular, each store has a person patronize krypton gold value and a European air value, if he European gas value equals c, he will choose even pumping c times to obtain a card if his European gas value of less than c, and his krypton gold value equals the card price p, he will krypton gold card to buy what he wants, otherwise he would leave the store.
now, game companies need to do is set how many times to ensure even pumping want to get a card, that even a man pumping c company can earn game time $ c * w $ profitable game company wants you to help him c is calculated as 1 to $ 1 + max (b_i) $ maximum profit. (p uncertain).
input format
the first line of the two numbers, n, w , means humans and cost-per-drawn cards to patronize the store.
Next n lines of two numbers represents the i individual krypton Gold values a_i $ and $ European gas values $ b_i $.
Output format
line, a total of $ 1 + max (b_i) $ number, represents the i-th maximum profit when c = i.
Input Sample. 1
2. 1
2 0
0 2
sample output. 1
. 3. 4 2
input sample 2
. 3. 1
. 3. 1
2 2
. 1. 3
output sample 2
. 3. 7. 7. 4
data range
$ n \ leq10 ^ 5 $, $ a_i, b_i \ leq10 ^ 5 $

The basic idea a good idea think, to enumerate c seeking answers, plus discrete, time complexity $ O (nmax (a_i)) $

The main procedure of finding the answer can be found in such an abstract data structure, to support each of 1 ~ x plus the number itself, the maximum interrogation of 1 ~ n

Then naturally thought to use the block

If the modification is a complete block, how to maintain the highest value?

We know that each position corresponding to a value of $ i * tag_i + s_i $

When you modify only $ tag_i $ will change, and monotonically increasing

Further, in each block $ I $ is monotonically increasing

Is there a little bit like the slope of the optimization?

Each element considered $ (i, s_i) $, maintaining a lower convex hull meet for $ i <j <k $, $ \ frac {s_i-s_j} {ij} <\ frac {s_j-s_k} { jk} $

Each time modifying, deleting $ s_i-s_j \ leq (ji) * tag_ {block_i} $ of

The remaining force to reconstruction, the time complexity $ O (n \ sqrt {n}) $

In the block are cleverly combined with the idea of ​​the optimization slope

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,n_n,w,mxb,mxa;
int block[N],lft[N],rgt[N];
long long tag[N],s[N],maxx[N];
struct person {int a,b;} p[N];
bool cmp(person u,person v) {return u.b<v.b;}
int q[505][505],h[505],t[505];
void update(int r)
{
    int R=block[r];
    for(int i=1;i<R;++i)
    {
        ++tag[i];
        while(h[i]<t[i] && 
            s[q[i][h[i]]]-s[q[i][h[i]+1]]<=
                1ll*(q[i][h[i]+1]-q[i][h[i]])*tag[i]) 
            ++h[i];
        
        maxx[i]=1ll*q[i][h[i]]*tag[i]+s[q[i][h[i]]];
    }
    for(int i=lft[R];i<=r;++i) s[i]+=i;
    for(int i=lft[R];i<=rgt[R];++i) s[i]+=tag[R]*i;
    h[R]=1,t[R]=tag[R]=0;
    for(int i=lft[R];i<=rgt[R];++i)
    {
        while(t[R]>h[R] && 
            1ll*(s[q[R][t[R]]]-s[i])*(q[R][t[R]-1]-q[R][t[R]])>=
                1ll*(q[R][t[R]]-i)*(s[q[R][t[R]-1]]-s[q[R][t[R]]])) --t[R];
        q[R][++t[R]]=i;
    }
    while(h[R]<t[R] && s[q[R][h[R]]]<=s[q[R][h[R]+1]]) ++h[R];
    maxx[R]=s[q[R][h[R]]];
}
long long ask_max()
{
    long long ret=0;
    for(int i=1;i<=n_n;++i) ret=max(ret,maxx[i]);
    return ret;
}
int main()
{
    freopen("could.in","r",stdin); freopen("could.out","w",stdout);
    scanf("%d%d",&n,&w);
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d",&p[i].a,&p[i].b);
        mxa=max(p[i].a,mxa);
    }
    sort(p+1,p+n+1,cmp);
    mxb=p[n].b+1;
    m=sqrt(mxa), n_n=(mxa-1)/m+1;
    for(int i=1;i<=mxa;++i) block[i]=(i-1)/m+1;
    for(int i=1;i<=n_n;++i)
    {
        lft[i]=(i-1)*m+1, rgt[i]=min(i*m,mxa);
        h[i]=1, t[i]=1, q[i][1]=rgt[i];
    }
    int pl=1,pr=1;
    for(int i=1;i<=mxb;++i)
    {
        long long ans=0;
        while(p[pr].b<i && pr<=n) ++pr;
        ans=1ll*i*w*(n-pr+1);
        for(int j=pl;j<pr;++j) update(p[j].a);
        pl=pr;
        printf("%lld ",ans+ask_max());
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/w19567/p/11272705.html