# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
876234 | Huy | Fancy Fence (CEOI20_fancyfence) | C++17 | 56 ms | 11996 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define pii pair<ll,ll>
#define fi first
#define se second
#pragma GCC tarGet ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC tarGet("avx,avx2,fma")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const int N = 1e5;
const int maxN = 2e5 + 1;
const ll mod = 1e9 + 7;
const int block_size = 708;
const ll infty = 1e18;
const double ep = 1e-7;
const int base = 200003;
void InputFile()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
freopen("test.out","r",stdin);
}
int n;
int h[maxN];
int w[maxN];
int prew[maxN];
int id[maxN];
int L[maxN];
int R[maxN];
bool FA(int i,int j)
{
return (h[i] < h[j]) || (h[i] == h[j] && L[i] < L[j]);
}
ll power(int a,int b)
{
if(b == 0) return 1;
ll t = power(a,b/2);
if(b % 2)
{
return t * t % mod * a % mod;
}
return t * t % mod;
}
ll C2(ll w)
{
return (w % mod * ((w + 1) % mod) % mod * power(2,mod-2) % mod );
}
void Read()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> h[i];
id[i] = i;
}
for(int i = 1; i <= n; i++)
{
cin >> w[i];
prew[i] = prew[i-1] + w[i];
}
vector<int> vc;
for(int i = 1; i <= n; i++)
{
while(!vc.empty() && h[vc.back()] >= h[i])
{
vc.pop_back();
}
if(vc.empty()) L[i] = 0;
else L[i] = vc.back();
vc.push_back(i);
}
vc.clear();
for(int i = n; i >= 1; i--)
{
while(!vc.empty() && h[vc.back()] >= h[i])
{
vc.pop_back();
}
if(vc.empty()) R[i] = n + 1;
else R[i] = vc.back();
vc.push_back(i);
}
sort(id + 1,id + n + 1,FA);
ll res = 0;
for(int i = 1; i <= n; i++)
{
if(h[id[i]] == h[id[i-1]] && L[id[i]] == L[id[i-1]]) continue;
//cout << id[i] <<' ';
ll need_w = prew[R[id[i]]-1] - prew[L[id[i]]];
ll pp = max(h[L[id[i]]],h[R[id[i]]]);
ll thua = h[id[i]] - pp;
ll add = (C2(thua) + thua * pp % mod) % mod * C2(need_w) % mod ;
res = (res + add) % mod;
}
cout << res;
//cout << C2(3);
}
void Solve()
{
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |