Submission #668062

#TimeUsernameProblemLanguageResultExecution timeMemory
668062600MihneaAdriatic (CEOI13_adriatic)C++17
100 / 100
211 ms131616 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 250000 + 7; const int A = 2500 + 7; const int INF = (int) 1e9 + 7; int n; int posr[N]; int posc[N]; int s[A][A]; int rmin[N]; int cmax[N]; int rmax[N]; int cmin[N]; ll s1[A][A]; ll s2[A][A]; int getsum(int r1, int c1, int r2, int c2) { return s[r2][c2] - s[r1 - 1][c2] - s[r2][c1 - 1] + s[r1 - 1][c1 - 1]; } pair<int, int> next1(pair<int, int> current) { int r = current.first, c = current.second; return {min(r, rmin[c - 1]), max(c, cmax[r + 1])}; } pair<int, int> next2(pair<int, int> current) { int r = current.first, c = current.second; return {max(r, rmax[c + 1]), min(c, cmin[r - 1])}; } int main() { #ifdef ONPC freopen ("input.txt", "r", stdin); #endif // ONPC #ifndef ONPC ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif // ONPC for (int i = 0; i < N; i++) { rmin[i] = INF; cmin[i] = INF; } cin >> n; for (int i = 1; i <= n; i++) { cin >> posr[i] >> posc[i]; s[posr[i]][posc[i]]++; rmin[posc[i]] = min(rmin[posc[i]], posr[i]); cmax[posr[i]] = max(cmax[posr[i]], posc[i]); rmax[posc[i]] = max(rmax[posc[i]], posr[i]); cmin[posr[i]] = min(cmin[posr[i]], posc[i]); } for (int i = 1; i < A; i++) { rmin[i] = min(rmin[i - 1], rmin[i]); cmin[i] = min(cmin[i - 1], cmin[i]); } for (int i = A - 2; i >= 0; i--) { cmax[i] = max(cmax[i + 1], cmax[i]); rmax[i] = max(rmax[i + 1], rmax[i]); } for (int i = 1; i < A; i++) { int c = 0; for (int j = 1; j < A; j++) { c += s[i][j]; s[i][j] = c + s[i - 1][j]; } } for (int r = 0; r < A; r++) { for (int c = A - 1; c >= 0; c--) { pair<int, int> it = next1({r, c}); s1[r][c] = getsum(1, c, r, A - 1) + s1[it.first][it.second]; } } for (int r = A - 1; r >= 0; r--) { for (int c = 0; c < A; c++) { pair<int, int> it = next2({r, c}); s2[r][c] = getsum(r, 1, A - 1, c) + s2[it.first][it.second]; } } for (int init = 1; init <= n; init++) { cout << n - 3 + s1[posr[init]][posc[init]] + s2[posr[init]][posc[init]] << "\n"; } return 0; } /** **/

Compilation message (stderr)

adriatic.cpp: In function 'int main()':
adriatic.cpp:22:50: warning: iteration 2506 invokes undefined behavior [-Waggressive-loop-optimizations]
   22 |   return s[r2][c2] - s[r1 - 1][c2] - s[r2][c1 - 1] + s[r1 - 1][c1 - 1];
      |                                      ~~~~~~~~~~~~^
adriatic.cpp:85:27: note: within this loop
   85 |     for (int c = A - 1; c >= 0; c--)
      |                         ~~^~~~
#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...