//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 1e9+7;
const int I = (mod+1)/2;
int mul(int a, int b){
return (a*b)%mod;
}
int C2(int x){
return mul(x, mul(x-1, I));
}
void add(int &a, int b){
a += b;
if (a >= mod) a-=mod;
}
struct DSU{
vector<int>rep, mn, mx, sz;
DSU(int n){
rep.resize(n+1);
iota(rep.begin(), rep.end(), 0);
mx = rep; mn = rep;
sz.assign(n+1, 1);
}
T get(int x){
x = f(x);
return {mn[x], mx[x]};
}
int f(int a){
return a == rep[a] ? a : rep[a] = f(rep[a]);
}
bool u(int a, int b){
a = f(a);b = f(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
mn[a] = min(mn[a], mn[b]);
mx[a] = max(mx[a], mx[b]);
rep[b] = a;
return true;
}
};
void solve(){
int n; cin >> n;
vector<int>h(n+1), w(n+1), pref(n+1);
vector<T>s;
for (int i = 1; i<=n; i++) {
cin >> h[i];
s.emplace_back(h[i], i);
}
DSU dsu(n+2);
for (int i = 1; i<=n; i++) {
cin >> w[i];
pref[i] = pref[i-1] + w[i];
}
sort(s.rbegin(), s.rend());
int ans = 0;
vector<bool>vis(n+1);
for (auto [h, i]: s){
int tmp = C2(w[i]+1);
if (i + 1 <= n && vis[i+1]) dsu.u(i, i+1);
if (i - 1 >= 1 && vis[i-1]) dsu.u(i, i+1);
vis[i] = 1;
auto [L, R] = dsu.get(i);
add(tmp, mul(w[i], pref[R]-pref[L-1]-w[i]));
add(ans, mul(C2(h+1), tmp));
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |