This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
ll calc(multiset<pair<ll, ll>> seg) {
vector<pair<ll, ll>> events;
for (auto [l, r] : seg) {
events.emplace_back(l, 1);
events.emplace_back(r + 1, -1);
}
sort(events.begin(), events.end());
ll res = 0;
int cnt = 0;
for (int i = 0; i + 1 < events.size(); i++) {
cnt += events[i].second;
if (cnt > 0)
res += events[i + 1].first - events[i].first;
}
return res;
}
ll gcd(ll a, ll b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
struct Event {
ll x;
int type;
ll pl, pr;
Event() {}
Event(ll x, int type, ll pl, ll pr) : x(x), type(type), pl(pl), pr(pr) {}
};
bool operator<(Event a, Event b) {
return a.x < b.x || (a.x == b.x && -a.type < -b.type);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll a, b;
cin >> n >> a >> b;
ll p = a / gcd(a, b + 1);
vector<Event> events;
auto add = [&] (ll l, ll r, ll pl, ll pr) {
events.emplace_back(l, 1, pl, pr);
events.emplace_back(r + 1, -1, pl, pr);
};
auto add_safe = [&] (ll l, ll r, ll pl, ll pr) {
if (pl > pr) {
add(l, r, 0, pr);
add(l, r, pl, p - 1);
} else
add(l, r, pl, pr);
};
while (n--) {
ll l, r;
cin >> l >> r;
ll L = l / b, R = r / b;
if (L == R)
add_safe(l % b, r % b, L % p, R % p);
else {
if (L + 1 < R)
add_safe(0, b - 1, (L + 1) % p, (R - 1) % p);
add_safe(l % b, b - 1, L % p, L % p);
add_safe(0, r % b, R % p, R % p);
}
}
sort(events.begin(), events.end());
multiset<pair<ll, ll>> seg;
ll ans = 0;
for (int i = 0; i + 1 < events.size(); ++i) {
auto e = events[i];
if (e.type == 1)
seg.insert({e.pl, e.pr});
else
seg.erase(seg.find({e.pl, e.pr}));
ll diff = events[i + 1].x - e.x;
if (diff != 0)
ans += calc(seg) * diff;
}
cout << ans;
return 0;
}
Compilation message (stderr)
strange_device.cpp: In function 'll calc(std::multiset<std::pair<long long int, long long int> >)':
strange_device.cpp:9:15: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto [l, r] : seg) {
^
strange_device.cpp:16:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < events.size(); i++) {
~~~~~~^~~~~~~~~~~~~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:81:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < events.size(); ++i) {
~~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |