#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)x.size()
#define int ll
int k, in_group, groups, a, b;
int get_num(int x) {
return (x / (b + 1)) % in_group;
}
int scanline(vector<pair<int, int>> &ev) {
sort(all(ev));
int cl = -1, balance = 0, res = 0;
for (auto it : ev) {
balance -= it.S;
if (balance == 0) {
res += it.F - cl;
cl = -1;
}
else {
if (cl == -1) {
cl = it.F;
}
}
}
return res;
}
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int slow_solve(int n, int a, int b, vector<pair<int, int>> q) {
set<pair<int, int>> used;
for (auto it : q) {
for (int i = it.F; i <= it.S; i++) {
int x = (i + i / b) % a, y = i % b;
used.insert({x, y});
}
}
return SZ(used);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//while (true) {
int n = 5;
a = 123;
b = rnd() % a;
if (b <= 0) {
b = 1;
}
//cout << n << ' ' << a << ' ' << b << endl;
cin >> n >> a >> b;
vector<pair<int, int>> q(n);
int lim = 10;
for (auto &it : q) {
cin >> it.F >> it.S;
//it.F = rnd() % lim;
//it.S = it.F + rnd() % lim;
}
groups = __gcd(a, b + 1);
in_group = a / groups;
//cout << groups << ' ' << in_group << '\n';
map<int, vector<pair<int, int>>> have;
vector<pair<int, int>> ev;
for (int i = 0; i < n; i++) {
int l = q[i].F, r = q[i].S;
int tl = (l % b == 0 ? l : l + (b - l % b)), tr = (r % b == b - 1 ? r : r - r % b - 1);
int x = (l - l % b) + ((l - l % b) / b), y = l % b,
x2 = (r - r % b) + ((r - r % b) / b), y2 = r % b;
//cout << i << ' ' << tl << ' ' << tr << '\n';
//tr -= b - 1;
if (tl > tr && x == x2) {
//cout << "C0 " << get_num(x) << ' ' << y << ' ' << y2 << '\n';
have[get_num(x)].pb({y, -1ll});
have[get_num(x)].pb({y2 + 1, 1ll});
continue;
}
//cout << "OK " << i << '\n';
if (tl > l) {
//cout << "C1 " << get_num(x) << ' ' << y << '\n';
have[get_num(x)].pb({y, -1ll});
have[get_num(x)].pb({b, 1ll});
}
if (tr < r) {
//cout << "C2 " << get_num(x2) << ' ' << y2 << '\n';
have[get_num(x2)].pb({0ll, -1ll});
have[get_num(x2)].pb({y2 + 1, 1ll});
}
if (tl > tr) {
continue;
}
//cout << tl << ' ' << tr << '\n';
//tr += b - 1;
int cx = (tl + tl / b);
int f = get_num(cx), s = get_num((tr + tr / b)), cnt = (tr - tl + 1) / b;
//cout << i << ' ' << (tl + tl / b) % a << ' ' << (tr + tr / b) % a << ' ' << f << ' ' << s << '\n';
/*if (f <= s) {
cnt = s - f + 1;
}
else {
cnt = in_group - (s - f - 1);
}*/
cnt = min(cnt, in_group);
//cout << "MAIN_C " << f << ' ' << cnt << ' ' << id << ' ' << num_in_group << ' ' << get_num(cx) << '\n';
if (get_num(cx) + cnt - 1 < in_group) {
ev.pb({get_num(cx), -1});
ev.pb({get_num(cx) + cnt, 1});
//cout << get_num(cx) << ' ' << get_num(cx) + cnt << '\n';
}
else {
ev.pb({get_num(cx), -1});
ev.pb({in_group, 1});
cnt -= in_group - get_num(cx);
ev.pb({0, -1});
ev.pb({cnt, 1});
}
}
sort(all(ev));
/*for (auto it : ev) {
cout << "DB " << it.F << ' ' << it.S << '\n';
}*/
int balance = 0, cl = -1, ans = 0;
for (auto it : ev) {
balance -= it.S;
if (balance == 0) {
while (!have.empty() && have.begin()->F < it.F) {
have.erase(have.begin());
}
ans += (it.F - cl) * b;
cl = -1;
}
else if (balance > 0 && cl == -1) {
while (!have.empty() && have.begin()->F < it.F) {
ans += scanline(have.begin()->S);
have.erase(have.begin());
}
cl = it.F;
}
}
//cout << ans << '\n';
for (auto it : have) {
//cout << "DB " << it.F << '\n';
ans += scanline(it.S);
}
/*if (ans != slow_solve(n, a, b, q)) {
cout << "ERROR\n";
cout << n << ' ' << a << ' ' << b << '\n';
for (auto it : q) {
cout << it.F << ' ' << it.S << '\n';
}
cout << ans << ' ' << slow_solve(n, a, b, q) << '\n';
cout << '\n';
exit(0);
}*/
//}
cout << ans << '\n';
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:103:8: warning: unused variable 'f' [-Wunused-variable]
int f = get_num(cx), s = get_num((tr + tr / b)), cnt = (tr - tl + 1) / b;
^
strange_device.cpp:103:25: warning: unused variable 's' [-Wunused-variable]
int f = get_num(cx), s = get_num((tr + tr / b)), cnt = (tr - tl + 1) / b;
^
strange_device.cpp:62:7: warning: unused variable 'lim' [-Wunused-variable]
int lim = 10;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
11 ms |
1016 KB |
Output is correct |
3 |
Correct |
12 ms |
888 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
4 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 |
13 ms |
1400 KB |
Output is correct |
17 |
Correct |
76 ms |
9200 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
300 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
565 ms |
74176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
807 ms |
48964 KB |
Output is correct |
3 |
Correct |
866 ms |
86224 KB |
Output is correct |
4 |
Correct |
839 ms |
86316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
807 ms |
48964 KB |
Output is correct |
3 |
Correct |
866 ms |
86224 KB |
Output is correct |
4 |
Correct |
839 ms |
86316 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
1819 ms |
241024 KB |
Output is correct |
7 |
Correct |
904 ms |
126328 KB |
Output is correct |
8 |
Correct |
1709 ms |
241040 KB |
Output is correct |
9 |
Correct |
1966 ms |
241148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
807 ms |
48964 KB |
Output is correct |
3 |
Correct |
866 ms |
86224 KB |
Output is correct |
4 |
Correct |
839 ms |
86316 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
118 ms |
21240 KB |
Output is correct |
7 |
Correct |
151 ms |
24416 KB |
Output is correct |
8 |
Correct |
154 ms |
24548 KB |
Output is correct |
9 |
Correct |
164 ms |
24416 KB |
Output is correct |
10 |
Correct |
113 ms |
18808 KB |
Output is correct |
11 |
Correct |
156 ms |
24468 KB |
Output is correct |
12 |
Correct |
145 ms |
24416 KB |
Output is correct |
13 |
Correct |
157 ms |
24416 KB |
Output is correct |
14 |
Correct |
86 ms |
9232 KB |
Output is correct |
15 |
Correct |
140 ms |
21492 KB |
Output is correct |
16 |
Correct |
67 ms |
9848 KB |
Output is correct |
17 |
Correct |
157 ms |
24416 KB |
Output is correct |
18 |
Correct |
1649 ms |
241336 KB |
Output is correct |
19 |
Correct |
1640 ms |
241144 KB |
Output is correct |
20 |
Correct |
1897 ms |
241248 KB |
Output is correct |
21 |
Correct |
158 ms |
24416 KB |
Output is correct |
22 |
Correct |
149 ms |
24688 KB |
Output is correct |
23 |
Correct |
234 ms |
34904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
71 ms |
5368 KB |
Output is correct |
3 |
Correct |
69 ms |
5496 KB |
Output is correct |
4 |
Correct |
1033 ms |
104636 KB |
Output is correct |
5 |
Correct |
89 ms |
6904 KB |
Output is correct |
6 |
Correct |
70 ms |
6520 KB |
Output is correct |
7 |
Correct |
76 ms |
6904 KB |
Output is correct |
8 |
Correct |
95 ms |
10488 KB |
Output is correct |
9 |
Correct |
86 ms |
10744 KB |
Output is correct |
10 |
Correct |
64 ms |
5164 KB |
Output is correct |
11 |
Correct |
73 ms |
5388 KB |
Output is correct |
12 |
Correct |
63 ms |
5624 KB |
Output is correct |
13 |
Correct |
80 ms |
8312 KB |
Output is correct |
14 |
Correct |
589 ms |
48488 KB |
Output is correct |
15 |
Correct |
76 ms |
6008 KB |
Output is correct |
16 |
Correct |
658 ms |
47988 KB |
Output is correct |
17 |
Correct |
940 ms |
98424 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
11 ms |
1016 KB |
Output is correct |
3 |
Correct |
12 ms |
888 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
4 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 |
13 ms |
1400 KB |
Output is correct |
17 |
Correct |
76 ms |
9200 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
300 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 |
6 ms |
632 KB |
Output is correct |
26 |
Correct |
6 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
504 KB |
Output is correct |
28 |
Correct |
565 ms |
74176 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
807 ms |
48964 KB |
Output is correct |
31 |
Correct |
866 ms |
86224 KB |
Output is correct |
32 |
Correct |
839 ms |
86316 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
1819 ms |
241024 KB |
Output is correct |
35 |
Correct |
904 ms |
126328 KB |
Output is correct |
36 |
Correct |
1709 ms |
241040 KB |
Output is correct |
37 |
Correct |
1966 ms |
241148 KB |
Output is correct |
38 |
Correct |
5 ms |
376 KB |
Output is correct |
39 |
Correct |
118 ms |
21240 KB |
Output is correct |
40 |
Correct |
151 ms |
24416 KB |
Output is correct |
41 |
Correct |
154 ms |
24548 KB |
Output is correct |
42 |
Correct |
164 ms |
24416 KB |
Output is correct |
43 |
Correct |
113 ms |
18808 KB |
Output is correct |
44 |
Correct |
156 ms |
24468 KB |
Output is correct |
45 |
Correct |
145 ms |
24416 KB |
Output is correct |
46 |
Correct |
157 ms |
24416 KB |
Output is correct |
47 |
Correct |
86 ms |
9232 KB |
Output is correct |
48 |
Correct |
140 ms |
21492 KB |
Output is correct |
49 |
Correct |
67 ms |
9848 KB |
Output is correct |
50 |
Correct |
157 ms |
24416 KB |
Output is correct |
51 |
Correct |
1649 ms |
241336 KB |
Output is correct |
52 |
Correct |
1640 ms |
241144 KB |
Output is correct |
53 |
Correct |
1897 ms |
241248 KB |
Output is correct |
54 |
Correct |
158 ms |
24416 KB |
Output is correct |
55 |
Correct |
149 ms |
24688 KB |
Output is correct |
56 |
Correct |
234 ms |
34904 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
71 ms |
5368 KB |
Output is correct |
59 |
Correct |
69 ms |
5496 KB |
Output is correct |
60 |
Correct |
1033 ms |
104636 KB |
Output is correct |
61 |
Correct |
89 ms |
6904 KB |
Output is correct |
62 |
Correct |
70 ms |
6520 KB |
Output is correct |
63 |
Correct |
76 ms |
6904 KB |
Output is correct |
64 |
Correct |
95 ms |
10488 KB |
Output is correct |
65 |
Correct |
86 ms |
10744 KB |
Output is correct |
66 |
Correct |
64 ms |
5164 KB |
Output is correct |
67 |
Correct |
73 ms |
5388 KB |
Output is correct |
68 |
Correct |
63 ms |
5624 KB |
Output is correct |
69 |
Correct |
80 ms |
8312 KB |
Output is correct |
70 |
Correct |
589 ms |
48488 KB |
Output is correct |
71 |
Correct |
76 ms |
6008 KB |
Output is correct |
72 |
Correct |
658 ms |
47988 KB |
Output is correct |
73 |
Correct |
940 ms |
98424 KB |
Output is correct |
74 |
Correct |
5 ms |
376 KB |
Output is correct |
75 |
Correct |
5 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
376 KB |
Output is correct |
77 |
Correct |
5 ms |
376 KB |
Output is correct |
78 |
Correct |
6 ms |
376 KB |
Output is correct |
79 |
Correct |
14 ms |
2168 KB |
Output is correct |
80 |
Correct |
700 ms |
88864 KB |
Output is correct |
81 |
Correct |
1014 ms |
141816 KB |
Output is correct |
82 |
Correct |
1947 ms |
240980 KB |
Output is correct |
83 |
Correct |
1842 ms |
241152 KB |
Output is correct |
84 |
Correct |
1961 ms |
241264 KB |
Output is correct |
85 |
Correct |
1804 ms |
241068 KB |
Output is correct |
86 |
Correct |
242 ms |
35040 KB |
Output is correct |
87 |
Correct |
5 ms |
376 KB |
Output is correct |