#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
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 time |
Memory |
Grader output |
1 |
Correct |
88 ms |
125212 KB |
Output is correct |
2 |
Correct |
76 ms |
125168 KB |
Output is correct |
3 |
Correct |
75 ms |
125260 KB |
Output is correct |
4 |
Correct |
77 ms |
125288 KB |
Output is correct |
5 |
Correct |
80 ms |
125180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
125308 KB |
Output is correct |
2 |
Correct |
76 ms |
125212 KB |
Output is correct |
3 |
Correct |
90 ms |
125336 KB |
Output is correct |
4 |
Correct |
79 ms |
125216 KB |
Output is correct |
5 |
Correct |
79 ms |
125264 KB |
Output is correct |
6 |
Correct |
79 ms |
125348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
125268 KB |
Output is correct |
2 |
Correct |
77 ms |
125340 KB |
Output is correct |
3 |
Correct |
82 ms |
125424 KB |
Output is correct |
4 |
Correct |
79 ms |
125356 KB |
Output is correct |
5 |
Correct |
83 ms |
125316 KB |
Output is correct |
6 |
Correct |
107 ms |
125296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
125684 KB |
Output is correct |
2 |
Correct |
99 ms |
125648 KB |
Output is correct |
3 |
Correct |
99 ms |
125632 KB |
Output is correct |
4 |
Correct |
86 ms |
125604 KB |
Output is correct |
5 |
Correct |
83 ms |
125536 KB |
Output is correct |
6 |
Correct |
130 ms |
125620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
129160 KB |
Output is correct |
2 |
Correct |
187 ms |
131116 KB |
Output is correct |
3 |
Correct |
175 ms |
131148 KB |
Output is correct |
4 |
Correct |
157 ms |
130860 KB |
Output is correct |
5 |
Correct |
160 ms |
131488 KB |
Output is correct |
6 |
Correct |
163 ms |
131364 KB |
Output is correct |
7 |
Correct |
211 ms |
131616 KB |
Output is correct |