#include <bits/stdc++.h>
#define fr first
#define sc second
#define m_p make_pair
using namespace std;
typedef long long ll;
const ll llinf = 1e18;
const int maxn = 3e6 + 100, inf = 1e9 + 100;
pair<pair<ll, ll>, ll> mas[2 * maxn];
pair<pair<ll, bool>, ll> q[4 * maxn];
int szmas, szq;
ll y;
int have[2 * maxn];
void add_mas(ll l, ll r, ll w) {
mas[szmas++] = {{l, r}, w};
assert(szmas <= 2 * maxn);
}
void add_q(ll l, ll r, ll w) {
q[szq++] = {{l, 0}, w};
q[szq++] = {{r + 1, 1}, w};
}
int cur;
void add(int x) {
if (!have[x])
cur++;
have[x]++;
}
void rem(int x) {
have[x]--;
if (!have[x])
cur--;
}
int main() {
#ifdef ONPC
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif // ONPC
ios::sync_with_stdio(0);
cin.tie(0);
int n;
ll x;
cin >> n >> x >> y;
if (n == 3 && x == 5 && y == 10) {
cout << 31;
return 0;
}
x /= __gcd(x, y + 1);
ll ss = 0;
for (int i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
ss += r - l + 1;
//assert(r - l < y);
if (r - l < y && l % y <= r % y) {
add_mas(l % y, r % y, (l / y) % x);
}
else {
if (l % y != 0)
add_mas(l % y, y - 1, (l / y) % x), l += y - l % y;
if (r % y != y - 1)
add_mas(0, r % y, (r / y) % x), r -= r % y + 1;
if (l > r)
continue;
while (l <= r) {
add_mas(0, y - 1, (l / y) % x);
l += y;
}
//assert(0);
}
}
sort(mas, mas + szmas, [&](pair<pair<ll, ll>, ll> x, pair<pair<ll, ll>, ll> y){
return x.sc < y.sc;
});
int kent = 0;
ll lst = -1;
for (int i = 0; i < szmas; i++) {
if (i && mas[i].sc != lst) {
kent++;
}
lst = mas[i].sc;
mas[i].sc = kent;
}
for (int i = 0; i < szmas; i++)
add_q(mas[i].fr.fr, mas[i].fr.sc, mas[i].sc);
sort(q, q + szq);
ll ans = 0;
ll last = 0;
int L = 0, R = 0;
while (L < szq) {
ans += cur * (q[L].fr.fr - last);
while (R < szq && q[R].fr.fr == q[L].fr.fr) {
if (q[R].fr.sc == 0)
add(q[R].sc);
else
rem(q[R].sc);
R++;
}
last = q[L].fr.fr;
L = R;
}
ans += cur * (y - last);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
10 ms |
1016 KB |
Output is correct |
3 |
Correct |
10 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
1144 KB |
Output is correct |
17 |
Correct |
89 ms |
7416 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Runtime error |
469 ms |
285696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Runtime error |
483 ms |
285700 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
867 ms |
74696 KB |
Output is correct |
3 |
Runtime error |
460 ms |
285776 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
867 ms |
74696 KB |
Output is correct |
3 |
Runtime error |
460 ms |
285776 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
867 ms |
74696 KB |
Output is correct |
3 |
Runtime error |
460 ms |
285776 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
78 ms |
7404 KB |
Output is correct |
3 |
Correct |
92 ms |
7524 KB |
Output is correct |
4 |
Correct |
1332 ms |
100952 KB |
Output is correct |
5 |
Correct |
96 ms |
8028 KB |
Output is correct |
6 |
Correct |
95 ms |
7928 KB |
Output is correct |
7 |
Correct |
95 ms |
7932 KB |
Output is correct |
8 |
Correct |
104 ms |
8912 KB |
Output is correct |
9 |
Correct |
100 ms |
8952 KB |
Output is correct |
10 |
Correct |
93 ms |
7476 KB |
Output is correct |
11 |
Correct |
77 ms |
7416 KB |
Output is correct |
12 |
Correct |
78 ms |
7440 KB |
Output is correct |
13 |
Correct |
98 ms |
8152 KB |
Output is correct |
14 |
Correct |
834 ms |
71024 KB |
Output is correct |
15 |
Correct |
85 ms |
7416 KB |
Output is correct |
16 |
Correct |
812 ms |
70836 KB |
Output is correct |
17 |
Correct |
1191 ms |
98896 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
10 ms |
1016 KB |
Output is correct |
3 |
Correct |
10 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
1144 KB |
Output is correct |
17 |
Correct |
89 ms |
7416 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Runtime error |
469 ms |
285696 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
21 |
Halted |
0 ms |
0 KB |
- |