#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1<<17;
int r[N][3];
int w[N];
int h[N];
int pre[N];
int ans = 0;
int mod = 1e9 + 7;
struct node{
int l,r;
node* left;
node* right;
int ind;
bool lft,rgt;
node(int ll,int rr){
l = ll;
r = rr;
left = right = NULL;
lft = rgt = false;
ind = 0;
}
void insert(int i){
if(i<l or i>=r)
return;
if (r-l==1){
ind = i;
return;
}
if (!lft)
left = new node(l,(l+r)/2);
if (!rgt)
right = new node((l+r)/2,r);
lft = rgt = true;
left->insert(i);
right->insert(i);
if (h[left->ind]<h[right->ind])
ind = left->ind;
else
ind = right->ind;
}
int get(int ll,int rr){
// if (ll==rr)
// cout<<ll<<" "<<rr<<endl;
ll = max(ll,l);
rr = min(rr,r);
if (ll>=r or rr<=l)
return 0;
if (l==ll and r==rr)
return ind;
int lind = 0;
int rind = 0;
if (lft)
lind = left->get(ll,rr);
if (rgt)
rind = right->get(ll,rr);
if (h[lind]<h[rind])
return lind;
return rind;
}
};
int asd(int a){
int b = a+1;
if (a%2)
b /= 2;
else
a /= 2;
return (a*b)%mod;
}
int answer(int W,int H){
int ans = asd(W);
int ans2 = asd(H);
return (ans*ans2)%mod ;
}
node* root;
void solve(int l,int r,int height){
if (r<l or l>r)
return;
int ind = root->get(l,r+1);
int width = pre[r]-pre[l-1];
if (width<0)
width += mod;
ans -= answer(width,height);
if (ans<0)
ans += mod;
ans += answer(width,h[ind]);
ans %= mod;
solve(l,ind-1,h[ind]);
solve(ind+1,r,h[ind]);
}
signed main(){
h[0] = 2e9;
root = new node(1,N+1);
int n;
cin>>n;
for (int i=1;i<=n;i++){
cin>>h[i];
root->insert(i);
}
for (int i=1;i<=n;i++){
cin>>w[i];
pre[i] = pre[i-1] + w[i];
pre[i] %= mod;
}
solve(1LL,n,0LL);
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2440 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
42 ms |
13432 KB |
Output is correct |
4 |
Correct |
84 ms |
27032 KB |
Output is correct |
5 |
Correct |
85 ms |
22100 KB |
Output is correct |
6 |
Correct |
77 ms |
18348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
12 ms |
4952 KB |
Output is correct |
3 |
Correct |
51 ms |
16968 KB |
Output is correct |
4 |
Correct |
100 ms |
29600 KB |
Output is correct |
5 |
Correct |
103 ms |
29504 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
10 ms |
4956 KB |
Output is correct |
4 |
Correct |
51 ms |
17040 KB |
Output is correct |
5 |
Correct |
102 ms |
29628 KB |
Output is correct |
6 |
Correct |
112 ms |
29808 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
11 ms |
3820 KB |
Output is correct |
9 |
Correct |
54 ms |
14604 KB |
Output is correct |
10 |
Correct |
94 ms |
17100 KB |
Output is correct |
11 |
Correct |
96 ms |
17184 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
42 ms |
13472 KB |
Output is correct |
12 |
Correct |
84 ms |
27220 KB |
Output is correct |
13 |
Correct |
81 ms |
22024 KB |
Output is correct |
14 |
Correct |
80 ms |
18256 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
10 ms |
4968 KB |
Output is correct |
17 |
Correct |
56 ms |
17012 KB |
Output is correct |
18 |
Correct |
103 ms |
29688 KB |
Output is correct |
19 |
Correct |
101 ms |
29532 KB |
Output is correct |
20 |
Correct |
1 ms |
2408 KB |
Output is correct |
21 |
Correct |
10 ms |
3728 KB |
Output is correct |
22 |
Correct |
53 ms |
14680 KB |
Output is correct |
23 |
Correct |
107 ms |
16992 KB |
Output is correct |
24 |
Correct |
96 ms |
16996 KB |
Output is correct |
25 |
Correct |
1 ms |
2408 KB |
Output is correct |
26 |
Correct |
1 ms |
2404 KB |
Output is correct |
27 |
Correct |
2 ms |
2408 KB |
Output is correct |
28 |
Correct |
1 ms |
2408 KB |
Output is correct |
29 |
Correct |
2 ms |
2548 KB |
Output is correct |
30 |
Correct |
10 ms |
3688 KB |
Output is correct |
31 |
Correct |
10 ms |
3688 KB |
Output is correct |
32 |
Correct |
52 ms |
10848 KB |
Output is correct |
33 |
Correct |
47 ms |
10952 KB |
Output is correct |
34 |
Correct |
93 ms |
17212 KB |
Output is correct |
35 |
Correct |
91 ms |
17160 KB |
Output is correct |
36 |
Correct |
95 ms |
17080 KB |
Output is correct |
37 |
Correct |
100 ms |
17288 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
96 ms |
17232 KB |
Output is correct |
40 |
Correct |
96 ms |
17184 KB |
Output is correct |
41 |
Correct |
97 ms |
18468 KB |
Output is correct |
42 |
Correct |
102 ms |
23380 KB |
Output is correct |