#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define ll long long
#define ld long double
#define int long long
#define pii pair<int, int>
int ob(vector<pii> a) {
vector<pii> v;
for (auto s : a) {
v.eb(s.x, 0);
v.eb(s.y + 1, 1);
}
sort(all(v));
int res = 0, cnt = 0;
f(i, sz(v) - 1) {
if (v[i].y == 0) cnt++; else cnt--;
if (cnt) res += v[i + 1].x - v[i].x;
}
return res;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
int p = a / __gcd(a, b + 1);
if (n == 1) {
int l, r;
cin >> l >> r;
vector<pair<pii, pii>> v;
int bl = l / b, br = r / b;
if (bl == br) {
cout << r - l + 1;
return 0;
}
if (bl + 1 < br) {
v.pb({{0, 0}, {(bl + 1) % p, (br - 1) % p}});
v.pb({{b, 1}, {(bl + 1) % p, (br - 1) % p}});
}
v.pb({{l % b, 0}, {bl % p, bl % p}});
v.pb({{b, 1}, {bl % p, bl % p}});
v.pb({{0, 0}, {br % p, br % p}});
v.pb({{r % b + 1, 1}, {br % p, br % p}});
multiset<pii> ss;
int ans = 0;
f(i, sz(v) - 1) {
if (v[i].x.y == 0) {
ss.insert(v[i].y);
} else {
ss.erase(ss.find(v[i].y));
}
vector<pii> tt;
for (auto s : ss) tt.pb(s);
ans += ob(tt) * (v[i + 1].x.x - v[i].x.x);
}
cout << ans;
return 0;
}
if (b <= 3) {
vector<int> l(n), r(n);
f(i, n) cin >> l[i] >> r[i];
int ans = 0;
f(z, b) {
vector<pii> v;
f(i, n) {
if (r[i] < z) continue;
int ql = max(0LL, (l[i] - z + b - 1) / b), qr = (r[i] - z) / b;
if (ql <= qr) {
ql %= p;
qr %= p;
if (ql <= qr) {
v.eb(ql, qr);
} else {
v.eb(0, qr);
v.eb(ql, p - 1);
}
}
}
ans += ob(v);
}
cout << ans;
} else {
set<pii> s;
while (n--) {
int l, r;
cin >> l >> r;
for (int i = l; i <= r; i++) s.insert({i / b % p, i % b});
}
cout << sz(s);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
55 ms |
12536 KB |
Output is correct |
3 |
Correct |
87 ms |
18040 KB |
Output is correct |
4 |
Correct |
7 ms |
896 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
11 ms |
1280 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
45 ms |
7032 KB |
Output is correct |
16 |
Correct |
33 ms |
6904 KB |
Output is correct |
17 |
Correct |
70 ms |
6904 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
143 ms |
32376 KB |
Output is correct |
3 |
Correct |
139 ms |
32120 KB |
Output is correct |
4 |
Correct |
124 ms |
30656 KB |
Output is correct |
5 |
Execution timed out |
5070 ms |
62716 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
693 ms |
80400 KB |
Output is correct |
3 |
Correct |
757 ms |
80580 KB |
Output is correct |
4 |
Correct |
702 ms |
80708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
693 ms |
80400 KB |
Output is correct |
3 |
Correct |
757 ms |
80580 KB |
Output is correct |
4 |
Correct |
702 ms |
80708 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
981 ms |
111956 KB |
Output is correct |
7 |
Correct |
744 ms |
54572 KB |
Output is correct |
8 |
Correct |
1250 ms |
112104 KB |
Output is correct |
9 |
Correct |
1568 ms |
112244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
693 ms |
80400 KB |
Output is correct |
3 |
Correct |
757 ms |
80580 KB |
Output is correct |
4 |
Correct |
702 ms |
80708 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
311 ms |
63092 KB |
Output is correct |
7 |
Runtime error |
2205 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Runtime error |
1931 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
55 ms |
12536 KB |
Output is correct |
3 |
Correct |
87 ms |
18040 KB |
Output is correct |
4 |
Correct |
7 ms |
896 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
11 ms |
1280 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
45 ms |
7032 KB |
Output is correct |
16 |
Correct |
33 ms |
6904 KB |
Output is correct |
17 |
Correct |
70 ms |
6904 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |