#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) x.begin(), x.end()
template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}
#define int ll
const int MAXN = 2e5 + 10;
int n;
int c[MAXN][2];
ll ans;
void make(int & a, int l, int r) {
if (a < l) ans += l - a, a = l;
if (a > r) ans += a - r, a = r;
}
void read() {
cin >> n;
for (int i = 0; i < 2 * n; i++) {
int x, y;
cin >> x >> y;
make(x, 1, n);
make(y, 1, 2);
x--;
y--;
c[x][y]++;
}
}
void run() {
for (int i = 0; i < n; i++) {
int t0 = c[i][0];
int t1 = c[i][1];
t0--;
t1--;
if (t0 * t1 < 0) {
int t = min(abs(t0), abs(t1));
if (t0 < 0) t0 += t, t1 -= t;
else t0 -= t, t1 += t;
ans += t;
}
ans += abs(t0) + abs(t1);
c[i + 1][0] += t0;
c[i + 1][1] += t1;
}
}
void write() {
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
248 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
380 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
248 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
248 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
380 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
248 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
376 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
6 ms |
376 KB |
Output is correct |
39 |
Correct |
5 ms |
376 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
6 ms |
376 KB |
Output is correct |
42 |
Correct |
5 ms |
376 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
380 KB |
Output is correct |
45 |
Correct |
6 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
6 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
380 KB |
Output is correct |
51 |
Correct |
6 ms |
376 KB |
Output is correct |
52 |
Correct |
6 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
376 KB |
Output is correct |
54 |
Correct |
5 ms |
376 KB |
Output is correct |
55 |
Correct |
6 ms |
408 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
5 ms |
376 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
5 ms |
376 KB |
Output is correct |
64 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
248 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
380 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
248 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
376 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
6 ms |
376 KB |
Output is correct |
39 |
Correct |
5 ms |
376 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
6 ms |
376 KB |
Output is correct |
42 |
Correct |
5 ms |
376 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
380 KB |
Output is correct |
45 |
Correct |
6 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
6 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
380 KB |
Output is correct |
51 |
Correct |
6 ms |
376 KB |
Output is correct |
52 |
Correct |
6 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
376 KB |
Output is correct |
54 |
Correct |
5 ms |
376 KB |
Output is correct |
55 |
Correct |
6 ms |
408 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
5 ms |
376 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
5 ms |
376 KB |
Output is correct |
64 |
Correct |
6 ms |
376 KB |
Output is correct |
65 |
Correct |
48 ms |
3448 KB |
Output is correct |
66 |
Correct |
74 ms |
6520 KB |
Output is correct |
67 |
Correct |
66 ms |
5112 KB |
Output is correct |
68 |
Correct |
64 ms |
5112 KB |
Output is correct |
69 |
Correct |
75 ms |
5112 KB |
Output is correct |
70 |
Correct |
64 ms |
5112 KB |
Output is correct |
71 |
Correct |
64 ms |
5112 KB |
Output is correct |
72 |
Correct |
65 ms |
5112 KB |
Output is correct |
73 |
Correct |
63 ms |
5112 KB |
Output is correct |
74 |
Correct |
61 ms |
5116 KB |
Output is correct |
75 |
Correct |
62 ms |
4996 KB |
Output is correct |
76 |
Correct |
61 ms |
5112 KB |
Output is correct |
77 |
Correct |
60 ms |
5112 KB |
Output is correct |
78 |
Correct |
60 ms |
5112 KB |
Output is correct |
79 |
Correct |
63 ms |
5112 KB |
Output is correct |
80 |
Correct |
63 ms |
5112 KB |
Output is correct |
81 |
Correct |
64 ms |
5112 KB |
Output is correct |
82 |
Correct |
60 ms |
5112 KB |
Output is correct |
83 |
Correct |
61 ms |
5112 KB |
Output is correct |
84 |
Correct |
59 ms |
5112 KB |
Output is correct |
85 |
Correct |
79 ms |
5112 KB |
Output is correct |
86 |
Correct |
60 ms |
5112 KB |
Output is correct |
87 |
Correct |
62 ms |
5112 KB |
Output is correct |
88 |
Correct |
61 ms |
5112 KB |
Output is correct |
89 |
Correct |
63 ms |
5112 KB |
Output is correct |
90 |
Correct |
66 ms |
5088 KB |
Output is correct |
91 |
Correct |
67 ms |
5016 KB |
Output is correct |
92 |
Correct |
67 ms |
5240 KB |
Output is correct |
93 |
Correct |
65 ms |
5240 KB |
Output is correct |
94 |
Correct |
62 ms |
4984 KB |
Output is correct |
95 |
Correct |
63 ms |
4984 KB |
Output is correct |
96 |
Correct |
65 ms |
5164 KB |
Output is correct |
97 |
Correct |
63 ms |
5228 KB |
Output is correct |
98 |
Correct |
66 ms |
5108 KB |
Output is correct |
99 |
Correct |
63 ms |
5112 KB |
Output is correct |
100 |
Correct |
62 ms |
5088 KB |
Output is correct |
101 |
Correct |
63 ms |
5112 KB |
Output is correct |
102 |
Correct |
64 ms |
5240 KB |
Output is correct |
103 |
Correct |
62 ms |
5112 KB |
Output is correct |