#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define len(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
bool inter(pii a, pii b) {
return max(a.x, b.x) <= min(a.y, b.y);
}
pii unite(pii a, pii b) {
return {min(a.x, b.x), max(a.y, b.y)};
}
struct Event {
int x, type, p;
Event() {}
Event(int x, int type, int p) : x(x), type(type), p(p) {}
};
bool operator<(const Event& a, const Event& b) {
return mt(a.x, a.type) < mt(b.x, b.type);
}
vector<Event> evs;
vector<pii> good;
void add_ev(int l, int r, int p) {
evs.eb(l, 0, p);
evs.eb(r + 1, 1, p);
}
bool check(int x) {
int l = -1, r = len(good);
while (r - l > 1) {
int mid = (l + r) / 2;
if (good[mid].y >= x) r = mid; else l = mid;
}
if (r == len(good)) return 0;
return good[r].x <= x;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
int period = a / __gcd(a, b + 1);
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
int block_l = l / b, block_r = r / b;
if (block_l == block_r) {
add_ev(l % b, r % b, block_l % period);
} else {
if (block_r - block_l != 1) {
int L = (block_l + 1) % period, R = (block_r - 1) % period;
if (L <= R) {
good.eb(L, R);
} else {
good.eb(0, R);
good.eb(L, period - 1);
}
}
add_ev(l % b, b - 1, block_l % period);
add_ev(0, r % b, block_r % period);
}
}
sort(all(evs));
sort(all(good));
vector<pii> st;
for (auto val : good) {
if (!st.empty() && inter(val, st.back())) st.back() = unite(st.back(), val); else st.pb(val);
}
good = st;
map<int, int> cnt;
int have = 0, ans = 0;
for (auto val : good) ans += (val.y - val.x + 1) * b;
for (int i = 0; i + 1 < len(evs); i++) {
if (!check(evs[i].p)) {
if (evs[i].type == 0) {
cnt[evs[i].p]++;
if (cnt[evs[i].p] == 1) have++;
} else {
cnt[evs[i].p]--;
if (cnt[evs[i].p] == 0) have--;
}
}
ans += have * (evs[i + 1].x - evs[i].x);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
13 ms |
1276 KB |
Output is correct |
3 |
Correct |
12 ms |
1276 KB |
Output is correct |
4 |
Correct |
5 ms |
384 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 |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 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 |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
14 ms |
1200 KB |
Output is correct |
17 |
Correct |
78 ms |
6632 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
796 ms |
126384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1171 ms |
109884 KB |
Output is correct |
3 |
Correct |
2615 ms |
250872 KB |
Output is correct |
4 |
Correct |
2596 ms |
251456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1171 ms |
109884 KB |
Output is correct |
3 |
Correct |
2615 ms |
250872 KB |
Output is correct |
4 |
Correct |
2596 ms |
251456 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
2617 ms |
188492 KB |
Output is correct |
7 |
Correct |
1092 ms |
68644 KB |
Output is correct |
8 |
Correct |
2607 ms |
188392 KB |
Output is correct |
9 |
Correct |
2658 ms |
188468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1171 ms |
109884 KB |
Output is correct |
3 |
Correct |
2615 ms |
250872 KB |
Output is correct |
4 |
Correct |
2596 ms |
251456 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
184 ms |
16248 KB |
Output is correct |
7 |
Correct |
284 ms |
19464 KB |
Output is correct |
8 |
Correct |
278 ms |
19208 KB |
Output is correct |
9 |
Correct |
256 ms |
19208 KB |
Output is correct |
10 |
Correct |
194 ms |
13260 KB |
Output is correct |
11 |
Correct |
302 ms |
19336 KB |
Output is correct |
12 |
Correct |
265 ms |
19336 KB |
Output is correct |
13 |
Correct |
244 ms |
19208 KB |
Output is correct |
14 |
Correct |
73 ms |
6760 KB |
Output is correct |
15 |
Correct |
276 ms |
16600 KB |
Output is correct |
16 |
Correct |
106 ms |
6768 KB |
Output is correct |
17 |
Correct |
312 ms |
19336 KB |
Output is correct |
18 |
Execution timed out |
5055 ms |
188496 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
79 ms |
6628 KB |
Output is correct |
3 |
Correct |
106 ms |
6632 KB |
Output is correct |
4 |
Correct |
2338 ms |
99064 KB |
Output is correct |
5 |
Correct |
118 ms |
6628 KB |
Output is correct |
6 |
Correct |
116 ms |
6632 KB |
Output is correct |
7 |
Correct |
110 ms |
6628 KB |
Output is correct |
8 |
Correct |
131 ms |
7388 KB |
Output is correct |
9 |
Correct |
138 ms |
7460 KB |
Output is correct |
10 |
Correct |
90 ms |
6628 KB |
Output is correct |
11 |
Correct |
73 ms |
6628 KB |
Output is correct |
12 |
Correct |
75 ms |
6628 KB |
Output is correct |
13 |
Correct |
121 ms |
6628 KB |
Output is correct |
14 |
Correct |
975 ms |
49708 KB |
Output is correct |
15 |
Correct |
85 ms |
6628 KB |
Output is correct |
16 |
Correct |
799 ms |
49768 KB |
Output is correct |
17 |
Correct |
2350 ms |
99064 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
13 ms |
1276 KB |
Output is correct |
3 |
Correct |
12 ms |
1276 KB |
Output is correct |
4 |
Correct |
5 ms |
384 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 |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 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 |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
14 ms |
1200 KB |
Output is correct |
17 |
Correct |
78 ms |
6632 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |