//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
typedef long long ll;
typedef long double ld;
const ll SZ = 2e5 + 100;
#define int ll
ll n;
vector<pair<ll, ll>> vec;
ll svd[SZ][2];
pair<int, int> seg[SZ * 4];
int seg2[SZ * 4];
void upd2(int v, int l, int r, int pos, int val = 0) {
if (l == r - 1) {
seg2[v] = val;
} else {
int mid = (l + r) / 2;
if (pos < mid) {
upd2(v * 2 + 1, l, mid, pos, val);
} else {
upd2(v * 2 + 2, mid, r, pos, val);
}
seg2[v] = seg2[v * 2 + 1] + seg2[v * 2 + 2];
}
}
int ask2(int v, int l, int r, int askl, int askr) {
if (l >= askr || r <= askl) return 0;
if (askl <= l && r <= askr) return seg2[v];
int mid = (l + r) / 2;
int q = ask2(v * 2 + 1, l, mid, askl, askr), q2 = ask2(v * 2 + 2, mid, r, askl, askr);
return q + q2;
}
void upd(int v, int l, int r, int pos, int val, int val2) {
if (l == r - 1) {
seg[v].first += val;
seg[v].second += val2;
} else {
int mid = (l + r) / 2;
if (pos < mid) {
upd(v * 2 + 1, l, mid, pos, val, val2);
} else {
upd(v * 2 + 2, mid, r, pos, val, val2);
}
seg[v] = { seg[v * 2 + 1].first + seg[v * 2 + 2].first, seg[v * 2 + 1].second + seg[v * 2 + 2].second };
}
}
pair<int, int> ask(int v, int l, int r, int askl, int askr) {
if (l >= askr || r <= askl) return { 0, 0 };
if (askl <= l && r <= askr) return seg[v];
int mid = (l + r) / 2;
pair<int, int> q = ask(v * 2 + 1, l, mid, askl, askr), q2 = ask(v * 2 + 2, mid, r, askl, askr);
return { q.first + q2.first, q.second + q2.second };
}
signed main() {
fastInp;
cin >> n;
vec.resize(n * 2);
for (int i = 0; i < n * 2; i++) cin >> vec[i].first >> vec[i].second;
ll ans = 0;
set<ll> hv, er;
for (int i = 0; i < n * 2; i++) {
if (vec[i].first <= 1) {
ans += abs(vec[i].first - 1);
vec[i].first = 1;
}
if (vec[i].first >= n) {
ans += abs(vec[i].first - n);
vec[i].first = n;
}
if (vec[i].second <= 1) {
ans += abs(vec[i].second - 1);
vec[i].second = 1;
}
if (vec[i].second >= 2) {
ans += abs(vec[i].second - 2);
vec[i].second = 2;
}
svd[vec[i].first][vec[i].second - 1]++;
}
pair<ll, ll> pt = { 1, 1 };
for (int i = 1; i <= n; i++) {
cout << "";
while (pt.first <= i && svd[pt.first][0] == 0 && svd[i][0] > 0) {
svd[pt.first][0]++;
svd[i][0]--;
ans += abs(i - pt.first);
pt.first++;
}
while (pt.second <= i && svd[pt.second][1] == 0 && svd[i][1] > 0) {
svd[pt.second][1]++;
svd[i][1]--;
ans += abs(i - pt.second);
pt.second++;
}
while (pt.first <= i && svd[pt.first][0] == 0 && svd[i][1] > 1) {
svd[pt.first][0]++;
svd[i][1]--;
ans += abs(i - pt.first) + 1;
pt.first++;
}
while (pt.second <= i && svd[pt.second][1] == 0 && svd[i][0] > 1) {
svd[pt.second][1]++;
svd[i][0]--;
ans += abs(i - pt.second) + 1;
pt.second++;
}
if (svd[i][0] > 1) {
svd[i + 1][0] += svd[i][0] - 1;
ans += svd[i][0] - 1;
svd[i][0] = 1;
}
if (svd[i][1] > 1) {
svd[i + 1][1] += svd[i][1] - 1;
ans += svd[i][1] - 1;
svd[i][1] = 1;
}
while (pt.first <= i && svd[pt.first][0] > 0) pt.first++;
while (pt.second <= i && svd[pt.second][1] > 0) pt.second++;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
320 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
320 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
380 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
1 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
336 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
2 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
320 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
380 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
1 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
336 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
2 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
40 ms |
5116 KB |
Output is correct |
66 |
Correct |
67 ms |
5108 KB |
Output is correct |
67 |
Correct |
61 ms |
5112 KB |
Output is correct |
68 |
Correct |
60 ms |
5128 KB |
Output is correct |
69 |
Correct |
54 ms |
5120 KB |
Output is correct |
70 |
Correct |
58 ms |
5120 KB |
Output is correct |
71 |
Correct |
57 ms |
5108 KB |
Output is correct |
72 |
Correct |
53 ms |
5120 KB |
Output is correct |
73 |
Correct |
55 ms |
5112 KB |
Output is correct |
74 |
Correct |
52 ms |
5060 KB |
Output is correct |
75 |
Correct |
56 ms |
5112 KB |
Output is correct |
76 |
Correct |
53 ms |
5112 KB |
Output is correct |
77 |
Correct |
59 ms |
5132 KB |
Output is correct |
78 |
Correct |
54 ms |
5132 KB |
Output is correct |
79 |
Correct |
53 ms |
5116 KB |
Output is correct |
80 |
Correct |
54 ms |
5060 KB |
Output is correct |
81 |
Correct |
53 ms |
5120 KB |
Output is correct |
82 |
Correct |
53 ms |
5060 KB |
Output is correct |
83 |
Correct |
54 ms |
5128 KB |
Output is correct |
84 |
Correct |
55 ms |
5036 KB |
Output is correct |
85 |
Correct |
54 ms |
5128 KB |
Output is correct |
86 |
Correct |
53 ms |
5124 KB |
Output is correct |
87 |
Correct |
56 ms |
5112 KB |
Output is correct |
88 |
Correct |
53 ms |
5128 KB |
Output is correct |
89 |
Correct |
54 ms |
5132 KB |
Output is correct |
90 |
Correct |
55 ms |
5108 KB |
Output is correct |
91 |
Correct |
56 ms |
5116 KB |
Output is correct |
92 |
Correct |
53 ms |
5116 KB |
Output is correct |
93 |
Correct |
54 ms |
5132 KB |
Output is correct |
94 |
Correct |
53 ms |
5128 KB |
Output is correct |
95 |
Correct |
51 ms |
5120 KB |
Output is correct |
96 |
Correct |
53 ms |
5116 KB |
Output is correct |
97 |
Correct |
59 ms |
5128 KB |
Output is correct |
98 |
Correct |
55 ms |
5188 KB |
Output is correct |
99 |
Correct |
54 ms |
5112 KB |
Output is correct |
100 |
Correct |
54 ms |
5108 KB |
Output is correct |
101 |
Correct |
54 ms |
5112 KB |
Output is correct |
102 |
Correct |
53 ms |
5112 KB |
Output is correct |
103 |
Correct |
55 ms |
5184 KB |
Output is correct |