Submission #498027

# Submission time Handle Problem Language Result Execution time Memory
498027 2021-12-24T09:47:49 Z andrei_boaca Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
75 ms 22752 KB
#include <bits/stdc++.h>

using namespace std;
/*ifstream fin("date.in");
ofstream fout("date.out");*/
typedef long long ll;
const ll mod=1e9+7;
ll s[500005],st[500005],dr[500005],rmq[500005][22],n,loga[500005],ans;
struct date
{
    ll h,w;
} v[500005];
ll getmin(ll l, ll r)
{
    ll lg=r-l+1;
    ll lgr=loga[lg];
    return min(rmq[l][lgr],rmq[r-(1<<lgr)+1][lgr]);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>v[i].h;
    for(int i=1;i<=n;i++)
    {
        loga[i]=loga[i-1];
        while((1LL<<(loga[i]+1))<=i)
            loga[i]++;
    }
    v[0].h=2e9;
    v[n+1].h=2e9;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i].w;
    }
    for(int i=1;i<=n+1;i++)
        s[i]=s[i-1]+v[i].w;
    for(int i=n+1;i>=0;i--)
    {
        rmq[i][0]=v[i].h;
        for(int j=1;j<=17;j++)
            rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
    }
    for(int i=1;i<=n;i++)
    {
        int st=0;
        int dr=i-1;
        int pozmin=i,pozmax=i;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            ll val=getmin(mij,i-1);
            if(val>v[i].h)
            {
                pozmin=min(pozmin,mij);
                dr=mij-1;
            }
            else
                st=mij+1;
        }
        st=i+1;
        dr=n+1;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            ll val=getmin(i+1,mij);
            if(val>=v[i].h)
            {
                pozmax=max(pozmax,mij);
                st=mij+1;
            }
            else
                dr=mij-1;
        }
        ll chooseH=v[i].h*(v[i].h+1)/2LL;
        chooseH%=mod;
        // both outside

        ll nrL=s[i-1]-s[max(0,pozmin-1)];
        nrL%=mod;
        ll nrR=s[pozmax]-s[i];
        nrR%=mod;
        ll val=(nrL*nrR)%mod;
        val=(val*chooseH)%mod;
        ans=(ans+val)%mod;

        // left outside

        nrL=s[i-1]-s[max(0,pozmin-1)];
        nrL%=mod;
        nrR=v[i].w%mod;
        val=(nrL*nrR)%mod;
        val=(val*chooseH)%mod;
        ans=(ans+val)%mod;

        // right outside

        nrL=v[i].w%mod;
        nrR=s[pozmax]-s[i];
        nrR%=mod;
        val=(nrL*nrR)%mod;
        val=(val*chooseH)%mod;
        ans=(ans+val)%mod;

        // none outside

        ll chooseW=v[i].w*(v[i].w+1)/2LL;
        chooseW%=mod;
        val=(chooseH*chooseW)%mod;
        ans=(ans+val)%mod;
    }
    cout<<ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 25 ms 11116 KB Output is correct
4 Correct 52 ms 21856 KB Output is correct
5 Correct 53 ms 21828 KB Output is correct
6 Correct 52 ms 21872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 6 ms 2512 KB Output is correct
3 Correct 25 ms 11452 KB Output is correct
4 Correct 55 ms 22588 KB Output is correct
5 Correct 59 ms 22640 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 5 ms 2512 KB Output is correct
4 Correct 27 ms 11496 KB Output is correct
5 Correct 56 ms 22624 KB Output is correct
6 Correct 58 ms 22708 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 5 ms 2512 KB Output is correct
9 Correct 28 ms 11484 KB Output is correct
10 Correct 62 ms 22468 KB Output is correct
11 Correct 54 ms 22604 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 0 ms 324 KB Output is correct
10 Correct 1 ms 456 KB Output is correct
11 Correct 23 ms 11080 KB Output is correct
12 Correct 53 ms 21856 KB Output is correct
13 Correct 52 ms 21752 KB Output is correct
14 Correct 54 ms 21856 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 5 ms 2556 KB Output is correct
17 Correct 25 ms 11464 KB Output is correct
18 Correct 65 ms 22636 KB Output is correct
19 Correct 71 ms 22752 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 5 ms 2508 KB Output is correct
22 Correct 30 ms 11544 KB Output is correct
23 Correct 58 ms 22460 KB Output is correct
24 Correct 75 ms 22624 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 2 ms 472 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 6 ms 2500 KB Output is correct
31 Correct 9 ms 2504 KB Output is correct
32 Correct 25 ms 11388 KB Output is correct
33 Correct 31 ms 11460 KB Output is correct
34 Correct 59 ms 22336 KB Output is correct
35 Correct 65 ms 22336 KB Output is correct
36 Correct 70 ms 22592 KB Output is correct
37 Correct 61 ms 22580 KB Output is correct
38 Correct 0 ms 336 KB Output is correct
39 Correct 62 ms 22588 KB Output is correct
40 Correct 58 ms 22600 KB Output is correct
41 Correct 73 ms 22604 KB Output is correct
42 Correct 55 ms 22644 KB Output is correct