Submission #1116743

# Submission time Handle Problem Language Result Execution time Memory
1116743 2024-11-22T10:04:17 Z JanPham Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
72 ms 14408 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define pb push_back
#define fi first
#define se second
#define TranLeThanhTam "TranLeThanhTam"
#define all(v) v.begin(),v.end()
#define reset(a,i) memset(a,i,sizeof(a))
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; --i)
#define compact(v) sort(all(v)), v.erase(unique(all(v)), end(v))
#define sz(v) (int) v.size()
#define MASK(i) (1LL << (i)
#define BIT(i,x) ((x & MASK(i)) >> (i))
#define int ll
 
typedef long long ll;
typedef long double lb;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
 
const int Thanh_Tam = 1e5 + 17;
const int inf = 1e9 + 17;
const long long infll = 1e18 + 17;
const int MOD = 1e9 + 7;
const int Log = 18;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int max_block_size = 270;
const ll P = 311;
const int maxA = 1e2 + 17;
const int addi = 1e9;
const char dir[4] = {'W', 'S', 'N', 'E'};
const int Mod[3] = {998244353, MOD, 759186269};
 
ll mul(ll a, ll b, ll mod);
ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1) res = mul(res,a,mod);
        b >>= 1;
        a = mul(a,a,mod);
    }
    return res;
}
ll add(ll a, ll b, ll mod) { return (a + b) % mod;                 }
ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod;          }
ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
ll div(ll a, ll b, ll mod) { return mul(a, binpow(b, mod - 2, mod), mod); }
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r) { return uniform_int_distribution<ll>(l,r)(rng); }
 
int n;
int h[Thanh_Tam], w[Thanh_Tam], l[Thanh_Tam], r[Thanh_Tam];
ll pref_w[Thanh_Tam];
ll ans = 0;
stack<int> st;
vector<pii> order;
map<pair<int,pii>, bool> mp;
 
ll sum(int x)
{
    return ((1LL * x * (x + 1)) >> 1) % MOD;
}
 
ll calc(int x, int y)
{
    return (((1LL * sum(x)) % MOD) * ((1LL * sum(y)) % MOD)) % MOD;
}
 
ll merge(int x1, int y1, int x2, int y2)
{
    if (x1 != x2) return 0;
    return ((sum(x1) % MOD) * ((1LL * y1 * y2) % MOD)) % MOD; 
}
 
void ThanhTam()
{
    cin >> n;
    FOR(i,1,n) cin >> h[i];
    FOR(i,1,n) cin >> w[i];
 
    //Prepare
    FOR(i,1,n) pref_w[i] = add(pref_w[i - 1], w[i], MOD);
    
    //Find left
    while (!st.empty()) st.pop();
    FOR(i,1,n)
    {
        while (!st.empty()) 
            if (h[st.top()] >= h[i]) st.pop();
        else break;
 
        if (st.empty()) l[i] = 0;
        else l[i] = st.top();
 
        st.push(i);
    }
 
    //Find right
    while (!st.empty()) st.pop();
    FORD(i,n,1)
    {
        while (!st.empty()) 
            if (h[st.top()] >= h[i]) st.pop();
        else break;
 
        if (st.empty()) r[i] = n + 1;
        else r[i] = st.top();
 
        st.push(i);
    }
 
 
    //Calculate answer
    FOR(i,1,n)    
        if (!mp.count({h[i], {l[i], r[i]}})) 
        {
            ans = add(ans, calc(sub(pref_w[r[i] - 1], pref_w[l[i]], MOD), h[i] - min(h[i], max(h[l[i]], h[r[i]]))), MOD);
            mp[{h[i], {l[i], r[i]}}] = true;
            ans = add(ans, merge(sub(pref_w[r[i] - 1], pref_w[l[i]], MOD), h[i] - min(h[i], max(h[l[i]], h[r[i]])), sub(pref_w[r[i] - 1], pref_w[l[i]], MOD), min(h[i], max(h[l[i]], h[r[i]]))), MOD);
        }
    cout << ans;
    return;
}
 
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    if (fopen(TranLeThanhTam".inp","r"))
    {
        freopen(TranLeThanhTam".inp","r",stdin);
        freopen(TranLeThanhTam".out","w",stdout);
    }
 
    int t = 1;
    //cin >> t;
    while (t--)
        ThanhTam();    
 
    return 0;
}
 
/*
2 * 4 => 30
2 * 2 => 9
2 * 3 => 18
2 * 1 => 3
 
Cách suy ra 2 * 4 từ 2 * 3 và 2 * 1
2 * 1 + 2 * 3 + 2 * 3 * 1 = 3 + 18 + 9 = 30 (Điều kiện: 2 cạnh đầu phải bằng nhau)


  0
0 0
000
*/

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(TranLeThanhTam".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(TranLeThanhTam".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2520 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 12 ms 4432 KB Output is correct
4 Correct 18 ms 5456 KB Output is correct
5 Correct 23 ms 6192 KB Output is correct
6 Correct 19 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 3 ms 2640 KB Output is correct
3 Correct 8 ms 3408 KB Output is correct
4 Correct 20 ms 4176 KB Output is correct
5 Correct 16 ms 4176 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 3 ms 2640 KB Output is correct
4 Correct 9 ms 3408 KB Output is correct
5 Correct 19 ms 4188 KB Output is correct
6 Correct 17 ms 4376 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 4 ms 3568 KB Output is correct
9 Correct 15 ms 5968 KB Output is correct
10 Correct 41 ms 13948 KB Output is correct
11 Correct 39 ms 13896 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2552 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 2 ms 2384 KB Output is correct
9 Correct 2 ms 2384 KB Output is correct
10 Correct 1 ms 2528 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 2 ms 2432 KB Output is correct
13 Correct 1 ms 2640 KB Output is correct
14 Correct 1 ms 2640 KB Output is correct
15 Correct 1 ms 2640 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2640 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 11 ms 4944 KB Output is correct
12 Correct 25 ms 6736 KB Output is correct
13 Correct 32 ms 7200 KB Output is correct
14 Correct 26 ms 6164 KB Output is correct
15 Correct 1 ms 2552 KB Output is correct
16 Correct 3 ms 2908 KB Output is correct
17 Correct 9 ms 4276 KB Output is correct
18 Correct 18 ms 6224 KB Output is correct
19 Correct 18 ms 6372 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 4 ms 3664 KB Output is correct
22 Correct 16 ms 5968 KB Output is correct
23 Correct 42 ms 13912 KB Output is correct
24 Correct 41 ms 13904 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 2 ms 2384 KB Output is correct
27 Correct 1 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
29 Correct 2 ms 2640 KB Output is correct
30 Correct 7 ms 3676 KB Output is correct
31 Correct 6 ms 3664 KB Output is correct
32 Correct 23 ms 7928 KB Output is correct
33 Correct 28 ms 8444 KB Output is correct
34 Correct 72 ms 13812 KB Output is correct
35 Correct 59 ms 13292 KB Output is correct
36 Correct 60 ms 13896 KB Output is correct
37 Correct 58 ms 13920 KB Output is correct
38 Correct 1 ms 2384 KB Output is correct
39 Correct 60 ms 13628 KB Output is correct
40 Correct 54 ms 13900 KB Output is correct
41 Correct 39 ms 14152 KB Output is correct
42 Correct 43 ms 14408 KB Output is correct