Submission #1219052

#TimeUsernameProblemLanguageResultExecution timeMemory
1219052phuonglinhn09Pilot (NOI19_pilot)C++20
3 / 100
47 ms30024 KiB
/** * author: phuonglinhn09 * created: 17.06.2025 */ #include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; const int nmax = 1e6; int n, q, a[nmax + 5], ans[nmax + 5], y[nmax + 5]; pair<int, int> h[nmax + 5]; vector<int> query[nmax + 5]; namespace DSU{ int par[nmax + 5], sz[nmax + 5]; ll get_val(int cnt) { ll ans = cnt; return ans * (ans - 1) / 2 + ans; } void preprocess() { for (int i = 1; i <= n; i++) { par[i] = i; sz[i] = 1; } } int find_set(int u) { return u == par[u] ? u : par[u] = find_set(par[u]); } ll Union (int u, int v) { u = find_set(u); v = find_set(v); if (u == v) return 0; if (sz[v] > sz[u]) swap(u, v); ll cur_ans = get_val(sz[u] + sz[v]) - get_val(sz[u]) - get_val(sz[v]); par[v] = u, sz[u] += sz[v]; return cur_ans; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen (".INP", "r")) { freopen (".INP", "r", stdin); freopen (".OUT", "w", stdout); } cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> h[i].fi; h[i].se = i; } for (int i = 1; i <= q; i++) { cin >> y[i]; query[y[i]].emplace_back(i); } sort(h + 1, h + 1 + n); sort(y + 1, y + 1 + q); DSU::preprocess(); ll cur_ans = 0; int j = 1; for (int i = 1; i <= q; i++) { while (j <= n && h[j].fi <= y[i]) { int pos = h[j].se; a[pos] = h[j].fi; cur_ans++; if (pos > 1 && a[pos - 1]) cur_ans += DSU::Union(pos, pos - 1); if (pos < n && a[pos + 1]) cur_ans += DSU::Union(pos, pos + 1); j++; } ans[i] = cur_ans; } for (int i = 1; i <= q; i++) { cout << ans[i] << "\n"; } return 0; }

Compilation message (stderr)

pilot.cpp: In function 'int main()':
pilot.cpp:56:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen (".INP", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
pilot.cpp:57:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         freopen (".OUT", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...