Submission #1003539

#TimeUsernameProblemLanguageResultExecution timeMemory
1003539vjudge1Flooding Wall (BOI24_wall)C++17
70 / 100
5024 ms68788 KiB
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(), x.end() const int N = 5e5, mod = 1e9+7; int n, a[N], b[N], pre[N], suf[N], pw[N]; vector<pair<int, int>> cp; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] > b[i]) swap(a[i], b[i]); } pw[0] = 1; for (int i = 1; i < n; i++) { pw[i] = (pw[i-1] << 1) % mod; } vector<int> u(n*2); u.push_back(0); for (int i = 0; i < n; i++) { u[i*2] = a[i]; u[i*2 + 1] = b[i]; } sort(all(u)); u.erase(unique(all(u)), u.end()); int sz = u.size(); cp.reserve(sz); for (int i = 0; i < sz; i++) { cp.push_back(make_pair(u[i], u[i])); if (i+1 < sz && u[i]+1 <= u[i+1]-1) cp.push_back(make_pair(u[i]+1, u[i+1]-1)); } sz = cp.size(); int ans = 0; for (int j = sz-2; j >= 0; j--) { for (int i = 0; i < n; i++) { pre[i] = (i ? pre[i-1] : 1); if (b[i] <= cp[j].second) { pre[i] *= 2; pre[i] %= mod; } else if (a[i] > cp[j].second) pre[i] = 0; } for (int i = n-1; i >= 0; i--) { suf[i] = (i+1 < n ? suf[i+1] : 1); if (b[i] <= cp[j].second) { suf[i] *= 2; suf[i] %= mod; } else if (a[i] > cp[j].second) suf[i] = 0; } bool ok = 0; for (int i = 1; i < n-1; i++) { int x = 0; x += pw[n-1-i] * pre[i-1]; x %= mod; x += pw[i] * suf[i+1]; x %= mod; x = (x - pre[i-1] * suf[i+1] + mod) % mod; if (x) ok = 1; if (a[i] < cp[j+1].first) { ans += (pw[n-1] - x + mod) % mod * (cp[j+1].second-cp[j+1].first+1); ans %= mod; } if (b[i] < cp[j+1].first) { ans += (pw[n-1] - x + mod) % mod * (cp[j+1].second-cp[j+1].first+1); ans %= mod; } } if (!ok) { for (int k = j-1; k >= 0; k--) { for (int i = 1; i < n-1; i++) { if (a[i] < cp[k+1].first) { ans += pw[n-1] * (cp[k+1].second-cp[k+1].first+1); ans %= mod; } if (b[i] < cp[k+1].first) { ans += pw[n-1] * (cp[k+1].second-cp[k+1].first+1); ans %= mod; } } } break; } } 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...