#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
#define all(x) (x).begin(), (x).end()
#define le(vec) vec[vec.size()-1]
#define sz(x) ((int)((x).size()))
#define pb push_back
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
const int N=1e5+10;
int n;
ll h[N], w[N];
ll calc(ll hh, ll L, ll wd)
{
return ((2*hh-L+1)*L/2%MOD)*((wd+1)*wd/2%MOD)%MOD;
}
int main()
{
setIO();
cin >> n;
FOR(i, 0, n) cin >> h[i];
FOR(i, 0, n) cin >> w[i];
ll lst=h[0], tot=0;
vi nh, nw;
FOR(i, 0, n)
{
if(h[i]==lst){
tot+=w[i];
tot%=MOD;
}
else{
nh.pb(lst);
nw.pb(tot);
lst=h[i];
tot=w[i];
}
}
nh.pb(lst);
nw.pb(tot);
n=sz(nh);
FOR(i, 0, n)
{
h[i]=nh[i];
w[i]=nw[i];
}
set<int> pin, nin;
FOR(i, 0, n)
{
pin.insert(i);
nin.insert(-i);
}
queue<int> q;
vector<pii> hgts;
FOR(i, 0, n)
{
hgts.pb({h[i],i});
}
sort(all(hgts), greater<pii>());
FOR(i, 0, n)
{
q.push(hgts[i].s);
}
ll ans=0;
while(!q.empty()){
int i=q.front(); q.pop();
//cout << i << endl;
if(!pin.count(i)) continue;
int l=-*nin.upper_bound(-i), r=*pin.upper_bound(i);
bool canL=nin.upper_bound(-i)!=nin.end(), canR=pin.upper_bound(i)!=pin.end();
if(!canL && !canR) continue;
else if(canR){
if(h[r]>h[i]) continue;
ll dif=h[i]-h[r];
pin.erase(r);
nin.erase(-r);
ans+=calc(h[i], dif, w[i]);
ans%=MOD;
w[i]+=w[r]; w[i]%=MOD;
h[i]=h[r];
}
else if(canL){
if(h[l]>h[i]) continue;
ll dif=h[i]-h[l];
pin.erase(l);
nin.erase(-l);
ans+=calc(h[i], dif, w[i]);
ans%=MOD;
w[i]+=w[l]; w[i]%=MOD;
h[i]=h[l];
}
else{
if(h[l]>h[i]||h[r]>h[i]) continue;
if(h[l]>h[r]){
ll dif=h[i]-h[l];
pin.erase(l);
nin.erase(-l);
ans+=calc(h[i], dif, w[i]);
ans%=MOD;
w[i]+=w[l]; w[i]%=MOD;
h[i]=h[l];
}
else if(h[r]>h[l]){
ll dif=h[i]-h[r];
pin.erase(r);
pin.erase(-r);
ans+=calc(h[i], dif, w[i]);
ans%=MOD;
w[i]+=w[r]; w[i]%=MOD;
h[i]=h[r];
}
else{
ll dif=h[i]-h[l];
pin.erase(l); pin.erase(r);
nin.erase(-l); nin.erase(-r);
ans+=calc(h[i], dif, w[i]);
w[i]+=w[r]; w[i]%=MOD;
w[i]+=w[l]; w[i]%=MOD;
h[i]=h[l];
}
}
q.push(i);
}
int in=*pin.begin();
ans+=((h[in]+1)*h[in]/2%MOD)*((w[in]+1)*w[in]/2%MOD)%MOD;
ans%=MOD;
cout << ans;
}
Compilation message
fancyfence.cpp: In function 'void setIO(std::string)':
fancyfence.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
2 ms |
448 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
27 ms |
4544 KB |
Output is correct |
4 |
Correct |
47 ms |
6600 KB |
Output is correct |
5 |
Correct |
56 ms |
8616 KB |
Output is correct |
6 |
Correct |
28 ms |
5188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
13 ms |
1988 KB |
Output is correct |
4 |
Correct |
21 ms |
3780 KB |
Output is correct |
5 |
Correct |
22 ms |
3880 KB |
Output is correct |
6 |
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 |
3 ms |
588 KB |
Output is correct |
4 |
Correct |
11 ms |
1992 KB |
Output is correct |
5 |
Correct |
20 ms |
3780 KB |
Output is correct |
6 |
Correct |
27 ms |
3892 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
15 ms |
1872 KB |
Output is correct |
9 |
Correct |
30 ms |
4548 KB |
Output is correct |
10 |
Correct |
138 ms |
15036 KB |
Output is correct |
11 |
Correct |
139 ms |
15116 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
2 ms |
456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
2 ms |
448 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |