#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();
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();
assert(sz(pin)==1);
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Runtime error |
2 ms |
716 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
26 ms |
3996 KB |
Output is correct |
4 |
Correct |
41 ms |
5436 KB |
Output is correct |
5 |
Correct |
61 ms |
7488 KB |
Output is correct |
6 |
Correct |
26 ms |
4044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
14 ms |
1100 KB |
Output is correct |
4 |
Correct |
19 ms |
1868 KB |
Output is correct |
5 |
Correct |
22 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
9 ms |
1080 KB |
Output is correct |
5 |
Correct |
19 ms |
1868 KB |
Output is correct |
6 |
Correct |
21 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
10 ms |
1652 KB |
Output is correct |
9 |
Correct |
28 ms |
3532 KB |
Output is correct |
10 |
Correct |
147 ms |
13248 KB |
Output is correct |
11 |
Correct |
142 ms |
13256 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Runtime error |
2 ms |
716 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Runtime error |
2 ms |
716 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |