#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
const int N = 1e6 + 1;
int n;
ll A, B, S = 0, L = 0;
pll a[N];
namespace subtask1 {
map<pll, bool> d;
void solve() {
int rs = 0;
FOR(i, 1, n) {
ll l = a[i].st, r = a[i].nd;
for (ll t = l; t <= r; ++t) {
ll x = (t + t / B) % A, y = t % B;
if (d.count({x, y})) continue;
d[{x, y}] = 1;
++rs;
}
}
cout << rs;
}
}
namespace subtask2 {
ll rs = 0;
void solve() {
ll l = a[1].st, r = a[1].nd;
ll C = A / gcd(A, B + 1);
ll pr = max(r - (r % B - l % B + B) % B - 1, l - 1);
rs += (r - pr) * min((r - l + 1) / B + 1, C);
rs += (min(B, r - l + 1) - r + pr) * min(r / B - l / B, C);
cout << rs;
}
}
struct Query {
ll l, r, l1, r1;
Query() {}
Query(ll l, ll r, ll l1, ll r1) : l(l), r(r), l1(l1), r1(r1) {}
};
struct Event {
bool type;
ll t, l1, r1;
Event() {}
Event(bool type, ll t, ll l1, ll r1) : type(type), t(t), l1(l1), r1(r1) {}
};
bool cmp(Event &a, Event &b) {
if (a.t != b.t) return a.t < b.t;
return a.type < b.type;
}
namespace subtask3456 {
const int T = 4e6 + 1;
vector<Query> query;
vector<Event> e;
int tr[T], f[T];
ll rs = 0;
void upd(int i, int l, int r, int ql, int qr, int v) {
if (r < ql || l > qr) return;
if (ql <= l && r <= qr) {
tr[i] += v;
f[i] = (tr[i]? r - l + 1 : l != r? f[i * 2] + f[i * 2 + 1] : 0);
return;
}
int mid = l + (r - l) / 2;
upd(i * 2, l, mid, ql, qr, v);
upd(i * 2 + 1, mid + 1, r, ql, qr, v);
f[i] = (tr[i]? r - l + 1 : l != r? f[i * 2] + f[i * 2 + 1] : 0);
}
void solve() {
ll C = A / gcd(A, B + 1);
FOR(i, 1, n) {
ll l = a[i].st, r = a[i].nd;
if ((r - l + 1) >= B * C) {
cout << B * C;
return;
}
}
FOR(i, 1, n) {
ll l = a[i].st, r = a[i].nd;
if (l % B != 0) {
ll nr = min(r, l + B - 1 - l % B);
ll l1 = l / B % C, r1 = nr / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, nr % B));
else {
query.pb(Query(l1, C - 1, l % B, nr % B));
query.pb(Query(0, r1, l % B, nr % B));
}
l = nr + 1;
}
if (l > r) continue;
if (r % B != B - 1) {
ll nr = r - (r % B) - 1;
if (l <= nr) {
ll l1 = l / B % C, r1 = nr / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, nr % B));
else {
query.pb(Query(l1, C - 1, l % B, nr % B));
query.pb(Query(0, r1, l % B, nr % B));
}
l = nr + 1;
}
}
if (l > r) continue;
ll l1 = l / B % C, r1 = r / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, r % B));
else {
query.pb(Query(l1, C - 1, l % B, r % B));
query.pb(Query(0, r1, l % B, r % B));
}
}
EACH(i, query) {
ll l = i.l, r = i.r, l1 = i.l1, r1 = i.r1;
e.pb(Event(0, l, l1, r1));
e.pb(Event(1, r, l1, r1));
}
sort(all(e), cmp);
int m = sz(e);
ll pre = e[0].t - 1;
FOR(i, 0, m - 1) {
bool type = e[i].type;
ll t = e[i].t, l1 = e[i].l1, r1 = e[i].r1;
if (type == 1 && (i == 0 || e[i - 1].type == 0 || e[i - 1].t != t)) {
rs += f[1] * (t - pre);
}
upd(1, 0, B - 1, l1, r1, type == 0? 1 : -1);
if (type == 0 && i > 0 && e[i - 1].type == 1) pre = e[i].t - 1;
if (i < m - 1 && e[i + 1].type == type && e[i + 1].t == t) continue;
if (type == 0) {
rs += f[1] * (t - pre);
}
pre = t;
}
cout << rs;
}
}
namespace subtask78 {
struct Node {
ll l, r;
int cnt;
ll mrk;
Node *left, *right;
Node() : l(0), r(0), cnt(0), mrk(0), left(0), right(0) {}
Node(ll l, ll r, int cnt, ll mrk, Node *left, Node *right) : l(l), r(r), cnt(0), mrk(0), left(left), right(right) {}
};
vector<Query> query;
vector<Event> e;
ll rs = 0;
Node *root;
map<ll, bool> c;
vll v;
void build(Node *cur) {
int l = lb(all(v), cur -> l) - v.begin(), r = ub(all(v), cur -> r) - v.begin() - 1;
if (l > r) return;
if (l == r) {
if (cur -> l == v[l]) {
Node *left = new Node(cur -> l, v[l], 0, 0, 0, 0);
Node *right = new Node(v[l] + 1, cur -> r, 0, 0, 0, 0);
cur -> left = left;
cur -> right = right;
build(cur -> left);
build(cur -> right);
return;
}
if (cur -> r == v[l]) {
Node left(cur -> l, v[l] - 1, 0, 0, 0, 0);
Node right(v[l], cur -> r, 0, 0, 0, 0);
cur -> left = &left;
cur -> right = &right;
build(cur -> left);
build(cur -> right);
return;
}
return;
}
int mid = (l + r) / 2;
Node *left = new Node(cur -> l, v[mid], 0, 0, 0, 0);
Node *right = new Node(v[mid] + 1, cur -> r, 0, 0, 0, 0);
cur -> left = left;
cur -> right = right;
build(cur -> left);
build(cur -> right);
}
void upd(Node *cur, ll ql, ll qr, int v) {
ll l = cur -> l, r = cur -> r;
if (r < ql || l > qr) return;
if (ql <= l && r <= qr) {
cur -> cnt += v;
cur -> mrk =
(cur -> cnt? r - l + 1 : (cur -> left? cur -> left -> mrk : 0) + (cur -> right? cur -> right -> mrk : 0));
return;
}
if (cur -> left) upd(cur -> left, ql, qr, v);
if (cur -> right) upd(cur -> right, ql, qr, v);
cur -> mrk = (cur -> cnt? r - l + 1 : (cur -> left? cur -> left -> mrk : 0) + (cur -> right? cur -> right -> mrk : 0));
}
void solve() {
ll C = A / gcd(A, B + 1);
FOR(i, 1, n) {
ll l = a[i].st, r = a[i].nd;
if ((r - l + 1) >= B * C) {
cout << B * C;
return;
}
}
FOR(i, 1, n) {
ll l = a[i].st, r = a[i].nd;
if (l % B != 0) {
ll nr = min(r, l + B - 1 - l % B);
ll l1 = l / B % C, r1 = nr / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, nr % B));
else {
query.pb(Query(l1, C - 1, l % B, nr % B));
query.pb(Query(0, r1, l % B, nr % B));
}
l = nr + 1;
}
if (l > r) continue;
if (r % B != B - 1) {
ll nr = r - (r % B) - 1;
if (l <= nr) {
ll l1 = l / B % C, r1 = nr / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, nr % B));
else {
query.pb(Query(l1, C - 1, l % B, nr % B));
query.pb(Query(0, r1, l % B, nr % B));
}
l = nr + 1;
}
}
if (l > r) continue;
ll l1 = l / B % C, r1 = r / B % C;
if (l1 <= r1) query.pb(Query(l1, r1, l % B, r % B));
else {
query.pb(Query(l1, C - 1, l % B, r % B));
query.pb(Query(0, r1, l % B, r % B));
}
}
EACH(i, query) {
ll l = i.l, r = i.r, l1 = i.l1, r1 = i.r1;
if (!c.count(l1)) {
v.pb(l1);
c[l1] = 1;
}
if (!c.count(r1)) {
v.pb(r1);
c[r1] = 1;
}
e.pb(Event(0, l, l1, r1));
e.pb(Event(1, r, l1, r1));
}
sort(all(e), cmp);
int m = sz(e);
ll pre = e[0].t - 1;
sort(all(v));
root = new Node(v[0], v[sz(v) - 1], 0, 0, 0, 0);
build(root);
FOR(i, 0, m - 1) {
bool type = e[i].type;
ll t = e[i].t, l1 = e[i].l1, r1 = e[i].r1;
if (type == 1 && (i == 0 || e[i - 1].type == 0 || e[i - 1].t != t)) {
rs += root -> mrk * (t - pre);
}
upd(root, l1, r1, type == 0? 1 : -1);
if (type == 0 && i > 0 && e[i - 1].type == 1) pre = e[i].t - 1;
if (i < m - 1 && e[i + 1].type == type && e[i + 1].t == t) continue;
if (type == 0) {
rs += root -> mrk * (t - pre);
}
pre = t;
}
cout << rs;
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> A >> B;
FOR(i, 1, n) {
cin >> a[i].st >> a[i].nd;
S += a[i].nd - a[i].st + 1;
L = max(L, a[i].nd - a[i].st + 1);
}
if (S <= 1e6) {
subtask1::solve();
return 0;
}
if (n == 1) {
subtask2::solve();
return 0;
}
if (B <= 1e6) {
subtask3456::solve();
return 0;
}
subtask78::solve();
// cerr << "\nTime: " << setprecision(6) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
3 3 3
4 4
7 9
17 18
*/
Compilation message
strange_device.cpp: In function 'void subtask1::solve()':
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:44:3: note: in expansion of macro 'FOR'
44 | FOR(i, 1, n) {
| ^~~
strange_device.cpp: In function 'void subtask3456::solve()':
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:102:3: note: in expansion of macro 'FOR'
102 | FOR(i, 1, n) {
| ^~~
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:109:3: note: in expansion of macro 'FOR'
109 | FOR(i, 1, n) {
| ^~~
strange_device.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
strange_device.cpp:142:3: note: in expansion of macro 'EACH'
142 | EACH(i, query) {
| ^~~~
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:150:3: note: in expansion of macro 'FOR'
150 | FOR(i, 0, m - 1) {
| ^~~
strange_device.cpp: In function 'void subtask78::solve()':
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:229:3: note: in expansion of macro 'FOR'
229 | FOR(i, 1, n) {
| ^~~
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:236:3: note: in expansion of macro 'FOR'
236 | FOR(i, 1, n) {
| ^~~
strange_device.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
strange_device.cpp:269:3: note: in expansion of macro 'EACH'
269 | EACH(i, query) {
| ^~~~
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:288:3: note: in expansion of macro 'FOR'
288 | FOR(i, 0, m - 1) {
| ^~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
strange_device.cpp:310:5: note: in expansion of macro 'FOR'
310 | FOR(i, 1, n) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
12484 KB |
Output is correct |
3 |
Correct |
83 ms |
18124 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
45 ms |
7052 KB |
Output is correct |
16 |
Correct |
33 ms |
6888 KB |
Output is correct |
17 |
Correct |
68 ms |
8092 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
176 ms |
32212 KB |
Output is correct |
3 |
Correct |
200 ms |
31992 KB |
Output is correct |
4 |
Correct |
156 ms |
30540 KB |
Output is correct |
5 |
Correct |
582 ms |
113032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
830 ms |
113048 KB |
Output is correct |
3 |
Correct |
839 ms |
113080 KB |
Output is correct |
4 |
Correct |
798 ms |
113204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
830 ms |
113048 KB |
Output is correct |
3 |
Correct |
839 ms |
113080 KB |
Output is correct |
4 |
Correct |
798 ms |
113204 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2135 ms |
372604 KB |
Output is correct |
7 |
Correct |
793 ms |
113128 KB |
Output is correct |
8 |
Correct |
1892 ms |
372660 KB |
Output is correct |
9 |
Correct |
2322 ms |
372616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
830 ms |
113048 KB |
Output is correct |
3 |
Correct |
839 ms |
113080 KB |
Output is correct |
4 |
Correct |
798 ms |
113204 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
120 ms |
24744 KB |
Output is correct |
7 |
Correct |
193 ms |
44328 KB |
Output is correct |
8 |
Correct |
191 ms |
44256 KB |
Output is correct |
9 |
Correct |
177 ms |
44244 KB |
Output is correct |
10 |
Correct |
120 ms |
23988 KB |
Output is correct |
11 |
Correct |
202 ms |
44296 KB |
Output is correct |
12 |
Correct |
178 ms |
44244 KB |
Output is correct |
13 |
Correct |
222 ms |
44256 KB |
Output is correct |
14 |
Correct |
149 ms |
27844 KB |
Output is correct |
15 |
Correct |
217 ms |
24888 KB |
Output is correct |
16 |
Correct |
189 ms |
27988 KB |
Output is correct |
17 |
Correct |
293 ms |
46548 KB |
Output is correct |
18 |
Correct |
2310 ms |
372592 KB |
Output is correct |
19 |
Correct |
2142 ms |
372716 KB |
Output is correct |
20 |
Correct |
2810 ms |
372776 KB |
Output is correct |
21 |
Correct |
255 ms |
44304 KB |
Output is correct |
22 |
Correct |
177 ms |
44188 KB |
Output is correct |
23 |
Correct |
193 ms |
49352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
111 ms |
13444 KB |
Output is correct |
3 |
Runtime error |
399 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
12484 KB |
Output is correct |
3 |
Correct |
83 ms |
18124 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
45 ms |
7052 KB |
Output is correct |
16 |
Correct |
33 ms |
6888 KB |
Output is correct |
17 |
Correct |
68 ms |
8092 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
176 ms |
32212 KB |
Output is correct |
26 |
Correct |
200 ms |
31992 KB |
Output is correct |
27 |
Correct |
156 ms |
30540 KB |
Output is correct |
28 |
Correct |
582 ms |
113032 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
830 ms |
113048 KB |
Output is correct |
31 |
Correct |
839 ms |
113080 KB |
Output is correct |
32 |
Correct |
798 ms |
113204 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
2135 ms |
372604 KB |
Output is correct |
35 |
Correct |
793 ms |
113128 KB |
Output is correct |
36 |
Correct |
1892 ms |
372660 KB |
Output is correct |
37 |
Correct |
2322 ms |
372616 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
120 ms |
24744 KB |
Output is correct |
40 |
Correct |
193 ms |
44328 KB |
Output is correct |
41 |
Correct |
191 ms |
44256 KB |
Output is correct |
42 |
Correct |
177 ms |
44244 KB |
Output is correct |
43 |
Correct |
120 ms |
23988 KB |
Output is correct |
44 |
Correct |
202 ms |
44296 KB |
Output is correct |
45 |
Correct |
178 ms |
44244 KB |
Output is correct |
46 |
Correct |
222 ms |
44256 KB |
Output is correct |
47 |
Correct |
149 ms |
27844 KB |
Output is correct |
48 |
Correct |
217 ms |
24888 KB |
Output is correct |
49 |
Correct |
189 ms |
27988 KB |
Output is correct |
50 |
Correct |
293 ms |
46548 KB |
Output is correct |
51 |
Correct |
2310 ms |
372592 KB |
Output is correct |
52 |
Correct |
2142 ms |
372716 KB |
Output is correct |
53 |
Correct |
2810 ms |
372776 KB |
Output is correct |
54 |
Correct |
255 ms |
44304 KB |
Output is correct |
55 |
Correct |
177 ms |
44188 KB |
Output is correct |
56 |
Correct |
193 ms |
49352 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
111 ms |
13444 KB |
Output is correct |
59 |
Runtime error |
399 ms |
524288 KB |
Execution killed with signal 9 |
60 |
Halted |
0 ms |
0 KB |
- |