# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
902701 | KN200711 | Fancy Fence (CEOI20_fancyfence) | C++14 | 49 ms | 5948 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# include <bits/stdc++.h>
# define ll long long
# define ld long double
# define fi first
# define se second
# define pll pair<ll, ll>
# define pdd pair<ld, ld>
# define pii pair<int, int>
using namespace std;
const ll MOD = 1e9 + 7;
int par[100001];
ll sz[100001], ans[100001];
ll C2(int a) {
ll v = a * (a + 1ll) / 2ll;
v %= MOD;
return v;
}
int find(int a) {
if(a == par[a]) return a;
par[a] = find(par[a]);
return par[a];
}
void merge(int a, int b, ll c) {
a = find(a);
b = find(b);
if(a == b) return;
ans[a] += ans[b];
ans[a] %= MOD;
ll G = C2(c) * (C2(sz[a]) + C2(sz[b]));
G %= MOD;
par[b] = a;
sz[a] += sz[b];
sz[a] %= MOD;
ans[a] -= G;
ans[a] %= MOD;
G = C2(c) * (C2(sz[a]));
G %= MOD;
ans[a] += G;
ans[a] %= MOD;
}
bool vis[100001];
int main() {
int N;
scanf("%d", &N);
vector<pair<pii, int> > arr(N);
for(int i=0;i<N;i++) {
par[i] = i;
scanf("%d", &arr[i].fi.fi);
}
for(int i=0;i<N;i++) {
scanf("%d", &arr[i].fi.se);
ans[i] = C2(arr[i].fi.fi) * C2(arr[i].fi.se);
sz[i] = arr[i].fi.se;
ans[i] %= MOD;
arr[i].se = i;
}
sort(arr.begin(), arr.end());
for(int i=N-1;i>=0;i--) {
int idx = arr[i].se;
vis[idx] = 1;
if(idx > 0 && vis[idx - 1]) merge(idx - 1, idx, arr[i].fi.fi);
if(idx + 1 < N && vis[idx + 1]) merge(idx + 1, idx, arr[i].fi.fi);
}
if(ans[find(0)] < 0) ans[find(0)] += MOD;
printf("%lld\n", ans[find(0)]);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |