Submission #1342607

#TimeUsernameProblemLanguageResultExecution timeMemory
1342607dex111222333444555Fancy Fence (CEOI20_fancyfence)C++20
100 / 100
21 ms2108 KiB
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define lli pair<long long, int>
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))

using namespace std;

bool M1;

const int MAXN = 1e5 + 5, LOG = 62, MAXV = 1e6 + 6, infINT = 1e9 + 12312, mod = 1e9 + 7;
const long long inf = 1e18 + 132;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}

int mul(const int &a, const int &b){
    assert(a < mod && b < mod);
    return 1LL * a * 1LL * b % mod;
}

void add(int &a, const int &b){
    assert(a < mod && b < mod);
    a += b;
    if (a >= mod) a -= mod;
}

void sub(int &a, const int &b){
    assert(a < mod && b < mod);
    a -= b;
    if (a < 0) a += mod;
}

int numRect, height[MAXN], width[MAXN], f[MAXN], pre[MAXN];

int bin_pow(int a, int k){
    int res = 1;
    while(k){
        if (k & 1) res = mul(res, a);
        a = mul(a, a); k >>= 1;
    }
    return res;
}

void input(){
    cin >> numRect;
    for(int i = 1; i <= numRect; i++) cin >> height[i];
    for(int i = 1; i <= numRect; i++) cin >> width[i];
}

int get(const int &L, const int &R){
    int sum = 0;
    add(sum, pre[R]);
    sub(sum, pre[L - 1]);
    return sum;
}

void solve(){
    for(int i = 1; i <= numRect; i++){
        pre[i] = pre[i - 1];
        add(pre[i], width[i]);
    }

    int inv = bin_pow(2, mod - 2), res = 0, sum = 0;

    stack<int > st;
    for(int i = 1; i <= numRect; i++){
        vector<int > can;
        while(siz(st) && height[st.top()] > height[i]){
            sub(sum, mul(mul(mul(height[st.top()], height[st.top()] + 1), inv), width[st.top()]));
            add(sum, mul(mul(mul(height[i], height[i] + 1), inv), width[st.top()]));
            can.push_back(st.top());
            st.pop();
        }
        st.push(i);
        add(res, mul(sum, width[i]));
        add(res, mul(mul(mul(width[i], width[i] + 1), inv), mul(mul(height[i], height[i] + 1), inv)));
        add(sum, mul(mul(mul(height[i], height[i] + 1), inv), width[i]));
        for(int id: can) add(width[i], width[id]);
    }

    cout << res << '\n';
    cerr << res << '\n';
}

bool M2;
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    input();
    solve();
    cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
    cerr << (&M2 - &M1) / 1048576 << " mb\n";
}

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...