#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqi;
typedef set<ll> sll;
typedef set<int> si;
typedef unordered_set<int> usi;
typedef unordered_set<ll> usll;
typedef multiset<ll> msll;
typedef multiset<int> msi;
typedef unordered_multiset<int> umsi;
typedef unordered_multiset<ll> umsll;
typedef map<ll, ll> mll;
typedef map<int, int> mi;
typedef unordered_map<int, int> umi;
typedef unordered_map<ll, ll> umll;
typedef multimap<ll, ll> mmll;
typedef multimap<int, int> mmi;
typedef unordered_multimap<int, int> ummi;
typedef unordered_multimap<ll, ll> ummll;
typedef tree < int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update > ordered_set;
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sd second
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i=a; i<b; i++)
#define REP_BACK(i, a, b) for (ll i=a-1; i>=b; i--)
#define TRAV(a, v) for(auto a : v)
#define file ifstream cin ("file.in"); ofstream cout ("file.out");
const long long int MOD = 1e9+7;
const long long int INF = 1e18;
const long double EPS = 1e-9;
struct pair_hash
{
template <class T1, class T2>
size_t operator() (const pair<T1, T2> &par) const {
return hash<T1>()(par.first) ^ hash<T2>()(par.second);
}
};
ll n, x;
ll h[100000], w[100000], sum[100001];
pll sparse_table[100000][20] = {};
ll ans = 0;
void solve (ll menor, ll maior, ll done)
{
if (maior < menor)
return;
ll x = log2(maior-menor+1);
pll a;
if (sparse_table[menor][x].fs < sparse_table[maior - ((ll)1<<x) + 1][x].fs)
a = sparse_table[menor][x];
else
a = sparse_table[maior - ((ll)1<<x) + 1][x];
ll height = (a.fs*(a.fs + 1)/2 - done*(done + 1)/2 + MOD)%MOD;
ll weight = ((sum[maior+1] - sum[menor])*(sum[maior+1] - sum[menor] + 1)/2)%MOD;
ans = (ans + height*weight)%MOD;
if (a.sd != menor)
solve (menor, a.sd - 1, a.fs);
if (a.sd != maior)
solve (a.sd + 1, maior, a.fs);
}
int main()
{
speed;
cin >> n;
REP(i, 0, n)
cin >> h[i];
REP(i, 0, n)
cin >> w[i];
sum[0] = 0;
REP(i, 0, n)
sum[i+1] = (sum[i] + w[i])%MOD;
for (ll i=n-1; i>=0; i--)
{
sparse_table[i][0].fs = h[i];
sparse_table[i][0].sd = i;
ll j = 1;
while (i + ((ll)1<<j) <= n)
{
if (sparse_table[i][j-1].fs > sparse_table[i + ((ll)1<<(j-1))][j-1].fs)
sparse_table[i][j] = sparse_table[i + ((ll)1<<(j-1))][j-1];
else
sparse_table[i][j] = sparse_table[i][j-1];
j ++;
}
}
solve (0, n-1, 0);
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
28 ms |
17656 KB |
Output is correct |
4 |
Correct |
55 ms |
35004 KB |
Output is correct |
5 |
Correct |
59 ms |
35020 KB |
Output is correct |
6 |
Correct |
62 ms |
35056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
7 ms |
3788 KB |
Output is correct |
3 |
Correct |
32 ms |
18028 KB |
Output is correct |
4 |
Correct |
61 ms |
35892 KB |
Output is correct |
5 |
Correct |
63 ms |
35908 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
7 ms |
3788 KB |
Output is correct |
4 |
Correct |
30 ms |
18008 KB |
Output is correct |
5 |
Correct |
64 ms |
35908 KB |
Output is correct |
6 |
Correct |
84 ms |
36004 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
6 ms |
3832 KB |
Output is correct |
9 |
Correct |
29 ms |
18160 KB |
Output is correct |
10 |
Correct |
63 ms |
35756 KB |
Output is correct |
11 |
Correct |
54 ms |
35864 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
29 ms |
17636 KB |
Output is correct |
12 |
Correct |
55 ms |
35012 KB |
Output is correct |
13 |
Correct |
68 ms |
35048 KB |
Output is correct |
14 |
Correct |
56 ms |
35020 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
6 ms |
3788 KB |
Output is correct |
17 |
Correct |
28 ms |
18028 KB |
Output is correct |
18 |
Correct |
70 ms |
35784 KB |
Output is correct |
19 |
Correct |
74 ms |
35976 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
6 ms |
3788 KB |
Output is correct |
22 |
Correct |
29 ms |
18072 KB |
Output is correct |
23 |
Correct |
61 ms |
35776 KB |
Output is correct |
24 |
Correct |
58 ms |
35840 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
456 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
8 ms |
3784 KB |
Output is correct |
31 |
Correct |
6 ms |
3788 KB |
Output is correct |
32 |
Correct |
30 ms |
18044 KB |
Output is correct |
33 |
Correct |
33 ms |
18072 KB |
Output is correct |
34 |
Correct |
100 ms |
35584 KB |
Output is correct |
35 |
Correct |
65 ms |
35684 KB |
Output is correct |
36 |
Correct |
75 ms |
35852 KB |
Output is correct |
37 |
Correct |
75 ms |
35908 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
68 ms |
35900 KB |
Output is correct |
40 |
Correct |
58 ms |
35820 KB |
Output is correct |
41 |
Correct |
74 ms |
36592 KB |
Output is correct |
42 |
Correct |
71 ms |
39788 KB |
Output is correct |