Submission #1320424

#TimeUsernameProblemLanguageResultExecution timeMemory
1320424MunkhErdeneObstacles for a Llama (IOI25_obstacles)C++17
10 / 100
214 ms52028 KiB
#include <bits/stdc++.h> #include "obstacles.h" using namespace std; #define ll long long #define pb push_back #define ff first #define ss second #define _ << " " << #define yes cout<<"YES\n" #define no cout<<"NO\n" #define ull unsigned long long #define lll __int128 #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define BlueCrowner ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORD(i, a, b) for (ll i = (a); i >= (b); i--) const ll mod = 1e9 + 7; const ll mod1 = 998244353; const ll naim = 1e9; const ll max_bit = 60; const ull tom = ULLONG_MAX; const ll MAXN = 2e6 + 5; const ll LOG = 20; const ll NAIM = 1e18; const ll N = 2e6 + 5; // ---------- GCD ---------- ll gcd(ll a, ll b) { while (b) { a %= b; swap(a, b); } return a; } // ---------- LCM ---------- ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // ---------- Modular Exponentiation ---------- ll modpow(ll a, ll b, ll m = mod) { ll c = 1; a %= m; while (b > 0) { if (b & 1) c = c * a % m; a = a * a % m; b >>= 1; } return c; } // ---------- Modular Inverse (Fermat’s Little Theorem) ---------- ll modinv(ll a, ll m = mod) { return modpow(a, m - 2, m); } // ---------- Factorials and Inverse Factorials ---------- ll fact[N], invfact[N]; void pre_fact(ll n = N-1, ll m = mod) { fact[0] = 1; for (ll i = 1; i <= n; i++) fact[i] = fact[i-1] * i % m; invfact[n] = modinv(fact[n], m); for (ll i = n; i > 0; i--) invfact[i-1] = invfact[i] * i % m; } // ---------- nCr ---------- ll nCr(ll n, ll r, ll m = mod) { if (r < 0 || r > n) return 0; return fact[n] * invfact[r] % m * invfact[n-r] % m; } // ---------- Sieve of Eratosthenes ---------- vector<ll> primes; bool is_prime[N]; void sieve(ll n = N-1) { fill(is_prime, is_prime + n + 1, true); is_prime[0] = is_prime[1] = false; for (ll i = 2; i * i <= n; i++) { if (is_prime[i]) { for (ll j = i * i; j <= n; j += i) is_prime[j] = false; } } for (ll i = 2; i <= n; i++) if (is_prime[i]) primes.pb(i); } vector<int> t, h; struct UnionFind { vector<int> par, sz; UnionFind(int n) { par.resize(n); sz.resize(n, 1); iota(all(par), 0); } int find(int a) { if(par[a] == a) return a; return par[a] = find(par[a]); } int unite(int a, int b) { a = find(a); b = find(b); if(a == b) return 0; if(sz[a] < sz[b]) swap(a, b); par[b] = a; sz[a] += sz[b]; return 1; } }; UnionFind dsu(MAXN); void initialize(vector<int> T, vector<int> H) { t = T; h = H; ll n = t.size(), m = h.size(); // replaced priority_queues with multisets multiset<pair<ll,ll>> inactive_columns; // acts as min-heap (begin() -> smallest) multiset<pair<ll,ll>> comps; // acts as max-heap (prev(end()) -> largest) vector<int> mn(m, 0); vector<int> inactive(m, 1); vector<int> id(m, -1); FOR(i, 0, m) { mn[i] = h[i]; if(h[i] >= t[0]) inactive_columns.insert({h[i], i}); else { comps.insert({mn[i], i}); inactive[i] = 0; } if(i) { if(t[0] > h[i] && t[0] > h[i - 1]) { ll a = dsu.find(i), b = dsu.find(i - 1); dsu.unite(i, i - 1); ll c = dsu.find(i); ll d = (c == a ? b : a); mn[c] = min(mn[c], mn[d]); comps.insert({mn[c], c}); } } } FOR(i, 0, m) { if(t[0] > h[i]) id[i] = dsu.find(i); } vector<int> removed_comps(m, 0); FOR(i, 1, n) { // comps: emulate priority_queue<pair<ll,ll>> (max-heap) while(!comps.empty()) { auto it = prev(comps.end()); auto px = *it; ll k = px.first; ll x = px.second; if(k >= t[i]) { comps.erase(it); removed_comps[x] = 1; } else break; } // inactive_columns: emulate min-heap (smallest first) while(!inactive_columns.empty()) { auto it = inactive_columns.begin(); auto px = *it; ll k = px.first; ll x = px.second; if(id[x] != -1) { inactive_columns.erase(it); continue; } if(t[i] > k) { inactive_columns.erase(it); if(x < m - 1 && id[x + 1] != -1 && !removed_comps[id[x + 1]]) { ll a = dsu.find(id[x + 1]), b = dsu.find(x); comps.erase({mn[a], a}); comps.erase({mn[b], b}); dsu.unite(a, b); ll c = dsu.find(a); ll d = (c == a ? b : a); mn[c] = min(mn[c], mn[d]); comps.insert({mn[c], c}); id[x] = c; ll cur = x; while(cur > 0 && id[cur - 1] == -1 && h[cur - 1] < t[i]) { ll a2 = cur - 1, b2 = dsu.find(cur); comps.erase({mn[b2], b2}); comps.erase({mn[a2], a2}); dsu.unite(a2, b2); ll c2 = dsu.find(a2); ll d2 = (c2 == a2 ? b2 : a2); mn[c2] = min(mn[c2], mn[d2]); comps.insert({mn[c2], c2}); id[cur] = c2; cur--; } } if(x > 0 && id[x - 1] != -1 && !removed_comps[id[x - 1]]) { ll a = dsu.find(id[x - 1]), b = dsu.find(x); comps.erase({mn[a], a}); comps.erase({mn[b], b}); dsu.unite(a, b); ll c = dsu.find(a); ll d = (c == a ? b : a); mn[c] = min(mn[c], mn[d]); comps.insert({mn[c], c}); id[x] = c; ll cur = x; while(cur < m - 1 && id[cur + 1] == -1 && h[cur + 1] < t[i]) { ll a2 = cur + 1, b2 = dsu.find(cur); comps.erase({mn[b2], b2}); comps.erase({mn[a2], a2}); dsu.unite(a2, b2); ll c2 = dsu.find(a2); ll d2 = (c2 == a2 ? b2 : a2); mn[c2] = min(mn[c2], mn[d2]); comps.insert({mn[c2], c2}); id[cur] = c2; cur++; } } } else break; } } } bool can_reach(int l, int r, int s, int d) { if(dsu.find(s) == dsu.find(d)) return 1; return 0; }

Compilation message (stderr)

In file included from /usr/include/x86_64-linux-gnu/c++/13/bits/c++allocator.h:33,
                 from /usr/include/c++/13/bits/allocator.h:46,
                 from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from obstacles.cpp:1:
In member function 'void std::__new_allocator<_Tp>::deallocate(_Tp*, size_type) [with _Tp = int]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::deallocate(allocator_type&, pointer, size_type) [with _Tp = int]' at /usr/include/c++/13/bits/alloc_traits.h:517:23,
    inlined from 'void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(pointer, std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:390:19,
    inlined from 'std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:369:15,
    inlined from 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:738:7,
    inlined from 'void initialize(std::vector<int>, std::vector<int>)' at obstacles.cpp:220:1:
/usr/include/c++/13/bits/new_allocator.h:172:33: warning: 'void operator delete(void*, std::size_t)' called on pointer '<unknown>' with nonzero offset [1, 9223372036854775804] [-Wfree-nonheap-object]
  172 |         _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
      |                                 ^
In member function '_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = int]',
    inlined from 'static _Tp* std::allocator_traits<std::allocator<_CharT> >::allocate(allocator_type&, size_type) [with _Tp = int]' at /usr/include/c++/13/bits/alloc_traits.h:482:28,
    inlined from 'std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:381:33,
    inlined from 'std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:378:7,
    inlined from 'void std::_Vector_base<_Tp, _Alloc>::_M_create_storage(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:398:44,
    inlined from 'std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:335:26,
    inlined from 'std::vector<_Tp, _Alloc>::vector(size_type, const value_type&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/include/c++/13/bits/stl_vector.h:571:47,
    inlined from 'void initialize(std::vector<int>, std::vector<int>)' at obstacles.cpp:140:35:
/usr/include/c++/13/bits/new_allocator.h:151:55: note: returned from 'void* operator new(std::size_t)'
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
      |                                                       ^
#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...