Submission #958911

#TimeUsernameProblemLanguageResultExecution timeMemory
958911TAhmed33Fancy Fence (CEOI20_fancyfence)C++98
100 / 100
32 ms17748 KiB
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int TWOINV = 5e8 + 4; const int MAXN = 1e5 + 25; int add (int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } int sub (int a, int b) { a -= b; if (a < 0) a += MOD; return a; } int mul (int a, int b) { return (a * 1ll * b) % MOD; } int h[MAXN], w[MAXN], n; int nxt[MAXN], prv[MAXN], sze[MAXN]; vector <int> adj[MAXN]; bool comp (int x, int y) { return h[x] == h[y] ? x < y : h[x] < h[y]; } int ans; void dfs (int pos) { sze[pos] = w[pos]; int x = mul(h[pos], mul(h[pos] + 1, TWOINV)); for (auto j : adj[pos]) { dfs(j); ans = add(ans, mul(sze[j], mul(sze[pos], x))); sze[pos] = add(sze[pos], sze[j]); } } int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n; memset(nxt, -1, sizeof(nxt)); memset(prv, -1, sizeof(prv)); for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) { int x = w[i], y = h[i]; x = mul(x, mul(x + 1, TWOINV)); y = mul(y, mul(y + 1, TWOINV)); ans = add(ans, mul(x, y)); } stack <int> cur; for (int i = 1; i <= n; i++) { while (!cur.empty() && comp(i, cur.top())) { nxt[cur.top()] = i; cur.pop(); } cur.push(i); } while (!cur.empty()) cur.pop(); for (int i = n; i >= 1; i--) { while (!cur.empty() && comp(i, cur.top())) { prv[cur.top()] = i; cur.pop(); } cur.push(i); } while (!cur.empty()) cur.pop(); int root = -1; for (int i = 1; i <= n; i++) { if (nxt[i] == -1 && prv[i] == -1) { root = i; continue; } if (nxt[i] == -1) { adj[prv[i]].push_back(i); } else if (prv[i] == -1) { adj[nxt[i]].push_back(i); } else if (!comp(nxt[i], prv[i])) { adj[nxt[i]].push_back(i); } else { adj[prv[i]].push_back(i); } } dfs(root); cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...