Submission #971375

#TimeUsernameProblemLanguageResultExecution timeMemory
971375WaelReal Mountains (CCO23_day1problem2)C++17
16 / 25
5104 ms165512 KiB
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; int const mod = 1e6 + 3; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); int mul(long long x, long long y) { while(x >= mod) x -= mod; while(y >= mod) y -= mod; return (x * y) % mod; } int add(long long x, long long y) { while(x >= mod) x -= mod; while(y >= mod) y -= mod; return (x + y) % mod; } int sub(long long x, long long y) { while(x >= mod) x -= mod; while(y >= mod) y -= mod; return (x - y + mod) % mod; } int power(long long x, long long y) { if(y == 0) return 1; if(y == 1) return x; int ret = power(x, y / 2); ret = mul(ret, ret); if(y % 2) ret = mul(ret, x); return ret; } int inv(long long x) { return power(x, mod - 2); } int divide(long long x, long long y) { while(x >= mod) x -= mod; while(y >= mod) y -= mod; return mul(x, inv(y)); } int tree[4000006]; void update(int nod, int l, int r, int x, int val) { if (x < l || r < x) return; if (l == r) { tree[nod] = val; return; } int mid = (l + r) / 2; update(nod * 2, l, mid, x, val); update(nod * 2 + 1, mid + 1, r, x, val); tree[nod] = min(tree[nod * 2], tree[nod * 2 + 1]); } int get(int nod, int l, int r, int x, int y) { if (r < x || y < l) return 1e9 + 1; if (x <= l && r <= y) return tree[nod]; int mid = (l + r) / 2; return min(get(nod * 2, l, mid, x, y), get(nod * 2 + 1, mid + 1, r, x, y)); } void solve() { int n = 1e6; cin >> n; vector<int> h(n); map<int, vector<int>> indices; for(int i = 0; i < n; ++i) { h[i] = RNG() % (int)1e9 + 1; cin >> h[i]; indices[h[i]].push_back(i); update(1, 0, n - 1, i, h[i]); } set<int> have, rem, all; for(int i = 0; i < n; ++i) { rem.insert(i); all.insert(h[i]); } auto sum = [&](int l, int r) { return (1LL * (l + r) * (r - l + 1) / 2) % mod; }; long long ans = 0; for(auto i : all) { for(auto j : indices[i]) { have.insert(j); rem.erase(j); update(1, 0, n - 1, j, 1e9 + 1); } if(rem.size() == 0) break; if(have.size() == 0) continue; while(have.size() && *have.begin() < *rem.begin()) have.erase(have.begin()); while(have.size() && *prev(have.end()) > *prev(rem.end())) have.erase(prev(have.end())); if(have.size() == 0) continue; int l = *have.begin(), r = *prev(have.end()); int z1 = get(1, 0, n - 1, 0, l - 1); int z2 = get(1, 0, n - 1, r + 1, n - 1); int x = min({get(1, 0, n - 1, l, r), z1, z2}); auto it = all.upper_bound(i); int dif = *it - i; if(l == r) { ans = add(ans, mul(z1 + z2, dif)); } else { ans = add(ans, mul(z1 + z2, dif)); ans = add(ans, mul(x, dif)); ans = add(ans, sum(i + 1, i + dif)); ans = add(ans, mul(mul((have.size() - 2), sum(i + 1, i + dif)), 2)); } ans = add(ans, mul(have.size(), sum(i, i + dif - 1))); } cout << ans << endl; return; } main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t--) { solve(); } return 0; }

Compilation message (stderr)

Main.cpp:126:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  126 | main() {
      | ^~~~
#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...