Submission #437913

# Submission time Handle Problem Language Result Execution time Memory
437913 2021-06-27T09:26:54 Z shahriarkhan Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
49 ms 6600 KB
#include<bits/stdc++.h>
using namespace std ;

const long long mod = 1e9 + 7 , INF = 1e18 ;

int main()
{
    int n ;
    scanf("%d",&n) ;
    long long h[n+3] , w[n+3] , ans = 0 , pref[n+3] = {0} ;
    int left[n+1] = {0} , right[n+1] = {0} , vis[n+1] = {0} ;
    for(int i = 1 ; i <= n ; ++i) scanf("%lld",&h[i]) ;
    for(int i = 1 ; i <= n ; ++i) scanf("%lld",&w[i]) ;
    h[0] = 0 , h[n+1] = 0 ;
    for(int i = 1 ; i <= n ; ++i)
    {
        pref[i] = (pref[i-1] + w[i])%mod ;
    }
    stack<int> L , R ;
    L.push(0) ;
    for(int i = 1 ; i <= n ; ++i)
    {
        while(h[L.top()]>h[i])
        {
            L.pop() ;
        }
        left[i] = L.top() ;
        L.push(i) ;
    }
    R.push(n+1) ;
    for(int i = n ; i >= 1 ; --i)
    {
        while(h[R.top()]>h[i])
        {
            R.pop() ;
        }
        right[i] = R.top() ;
        R.push(i) ;
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        if(vis[i]) continue ;
        while(h[right[i]]==h[i])
        {
            vis[right[i]] = 1 ;
            right[i] = right[right[i]] ;
        }
        long long suf = (pref[right[i]-1] - pref[left[i]] + mod)%mod ;
        long long hj = max(h[left[i]],h[right[i]]) ;
        long long extra1 = ((h[i]*(h[i]+1))/2)%mod ;
        extra1 = (((extra1 - ((hj*(hj+1))/2)%mod)%mod) + mod)%mod ;
        long long extra2 = ((suf*(suf+1))/2)%mod ;
        ans = (ans + (extra1*extra2)%mod)%mod ;
    }
    printf("%lld\n",ans) ;
    return 0 ;
}

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d",&n) ;
      |     ~~~~~^~~~~~~~~
fancyfence.cpp:12:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     for(int i = 1 ; i <= n ; ++i) scanf("%lld",&h[i]) ;
      |                                   ~~~~~^~~~~~~~~~~~~~
fancyfence.cpp:13:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     for(int i = 1 ; i <= n ; ++i) scanf("%lld",&w[i]) ;
      |                                   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 14 ms 2764 KB Output is correct
4 Correct 29 ms 5580 KB Output is correct
5 Correct 30 ms 5192 KB Output is correct
6 Correct 27 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 4 ms 820 KB Output is correct
3 Correct 17 ms 3404 KB Output is correct
4 Correct 47 ms 6472 KB Output is correct
5 Correct 33 ms 6596 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 4 ms 844 KB Output is correct
4 Correct 17 ms 3404 KB Output is correct
5 Correct 32 ms 6472 KB Output is correct
6 Correct 40 ms 6600 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 4 ms 808 KB Output is correct
9 Correct 22 ms 3304 KB Output is correct
10 Correct 32 ms 5960 KB Output is correct
11 Correct 33 ms 6080 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 15 ms 2688 KB Output is correct
12 Correct 28 ms 5580 KB Output is correct
13 Correct 30 ms 5336 KB Output is correct
14 Correct 29 ms 4940 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 16 ms 3444 KB Output is correct
18 Correct 31 ms 6480 KB Output is correct
19 Correct 33 ms 6592 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 4 ms 844 KB Output is correct
22 Correct 16 ms 3316 KB Output is correct
23 Correct 32 ms 5952 KB Output is correct
24 Correct 33 ms 6080 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 4 ms 716 KB Output is correct
31 Correct 5 ms 716 KB Output is correct
32 Correct 17 ms 2892 KB Output is correct
33 Correct 18 ms 3012 KB Output is correct
34 Correct 43 ms 5540 KB Output is correct
35 Correct 35 ms 5452 KB Output is correct
36 Correct 49 ms 5696 KB Output is correct
37 Correct 35 ms 5712 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 33 ms 5736 KB Output is correct
40 Correct 35 ms 5712 KB Output is correct
41 Correct 33 ms 5708 KB Output is correct
42 Correct 38 ms 5832 KB Output is correct