#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
using ll= long long;
#define fast_io ios::sync_with_stdio(0); cin.tie(0)
#define inpint(x) int x; cin>>x
#define inpll(x) long long x; cin>>x
#define fl(i, n) for(int i=0; i<n; i++)
#define flo(i, n) for(int i=1; i<=n; i++)
#define int long long
#define pi pair<int, int>
#define mp make_pair
#define ld long double
const int MOD = 998244353;
const int MODa = 7 + (int)1e9;
const int INF = 1 + (int)1e18;
const int MODinv2 = 500000004;
int nc2(int i)
{
int ans = 0;
i %= MODa;
ans = i*(i-1);
ans %= MODa;
ans *= MODinv2;
ans %= MODa;
return ans;
}
int calc(int h, int w)
{
h %= MODa;
w %= MODa;
int ne = (h+1)*(w+1);
ne %= MODa;
int ans = nc2(ne);
int hr = nc2(h+1);
int wr = nc2(w+1);
hr *= (w+1);
hr %= MODa;
wr *= (h+1);
wr %= MODa;
ans -= hr;
ans += MODa;
ans %= MODa;
ans -= wr;
ans += MODa;
ans %= MODa;
ans *= MODinv2;
ans %= MODa;
return ans;
}
vector<int> h(0);
vector<int> w(0);
vector<int> wp(0);
bool rsort(int x, int y)
{
if(h[x]==h[y]) return x<y;
return h[x]<h[y];
}
void solve()
{
int n;
cin>>n;
h.resize(n+2);
w.resize(n+2);
wp.resize(n+2);
wp[0] = 0;
h[0] = 0;
h[n+1] = 0;
w[n+1] = 0;
int ans = 0;
for(int i=1; i<=n; i++)
{
cin>>h[i];
}
for(int i=1; i<=n; i++)
{
cin>>w[i];
wp[i] = wp[i-1] + w[i];
}
vector<int> rec(n+1, 0);
iota(rec.begin(), rec.end(), 0);
set<int> done;
sort(rec.begin()+1, rec.end(), rsort);
for(int i=1; i<=n; i++)
{
auto it = done.lower_bound(rec[i]);
int l, r;
if(it==done.end())
{
r = n;
}
else
{
r = (*it)-1;
}
if(it == done.begin())
{
l = 1;
}
else
{
it--;
l = (*it)+1;
}
int wis = wp[r] - wp[l-1];
wis %= MODa;
int hd = max(h[l-1], h[r+1]);
if(hd==h[rec[i]])
{
done.insert(rec[i]);
continue;
}
ans += calc(h[rec[i]], wis);
ans %= MODa;
ans -= calc(hd, wis);
ans += MODa;
ans %= MODa;
done.insert(rec[i]);
}
cout<<ans;
}
int32_t main()
{
fast_io;
int t=1;
//cin>>t;
while(t--)
{
solve();
}
cout<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
24 ms |
4780 KB |
Output is correct |
4 |
Correct |
52 ms |
9236 KB |
Output is correct |
5 |
Correct |
46 ms |
9220 KB |
Output is correct |
6 |
Correct |
51 ms |
9284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
1224 KB |
Output is correct |
3 |
Correct |
31 ms |
5196 KB |
Output is correct |
4 |
Correct |
70 ms |
10052 KB |
Output is correct |
5 |
Correct |
63 ms |
10108 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
1240 KB |
Output is correct |
4 |
Correct |
25 ms |
5200 KB |
Output is correct |
5 |
Correct |
52 ms |
10108 KB |
Output is correct |
6 |
Correct |
59 ms |
10088 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
1188 KB |
Output is correct |
9 |
Correct |
28 ms |
5196 KB |
Output is correct |
10 |
Correct |
67 ms |
10032 KB |
Output is correct |
11 |
Correct |
58 ms |
10008 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
412 KB |
Output is correct |
11 |
Correct |
23 ms |
4812 KB |
Output is correct |
12 |
Correct |
55 ms |
9192 KB |
Output is correct |
13 |
Correct |
47 ms |
9200 KB |
Output is correct |
14 |
Correct |
45 ms |
9296 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
31 ms |
5160 KB |
Output is correct |
18 |
Correct |
53 ms |
10000 KB |
Output is correct |
19 |
Correct |
59 ms |
10112 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
1264 KB |
Output is correct |
22 |
Correct |
27 ms |
5236 KB |
Output is correct |
23 |
Correct |
60 ms |
9960 KB |
Output is correct |
24 |
Correct |
64 ms |
9964 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
6 ms |
1176 KB |
Output is correct |
31 |
Correct |
7 ms |
1224 KB |
Output is correct |
32 |
Correct |
30 ms |
5060 KB |
Output is correct |
33 |
Correct |
43 ms |
5176 KB |
Output is correct |
34 |
Correct |
71 ms |
9788 KB |
Output is correct |
35 |
Correct |
78 ms |
9804 KB |
Output is correct |
36 |
Correct |
74 ms |
10080 KB |
Output is correct |
37 |
Correct |
81 ms |
10016 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
69 ms |
10076 KB |
Output is correct |
40 |
Correct |
70 ms |
10080 KB |
Output is correct |
41 |
Correct |
63 ms |
10080 KB |
Output is correct |
42 |
Correct |
59 ms |
10052 KB |
Output is correct |