#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
ll gcd(ll a, ll b) {
while (b != 0LL) {
a %= b;
swap(a, b);
}
return a;
}
struct E {
ll x;
int k;
ll l, r;
bool operator<(const E& o) {
return x < o.x || (x == o.x && k < o.k);
}
E() {}
E(ll x, int k, ll l, ll r) : x(x), k(k), l(l), r(r) {}
};
ll n, a, b;
ll p;
vector<E> v;
void push(ll l, ll r, ll x, ll y) {
v.emplace_back(l, 0, x, y);
v.emplace_back(r+1, 1, x, y);
}
void add(ll l, ll r, ll x, ll y) {
if (x <= y) {
push(l, r, x, y);
} else {
push(l, r, 0LL, y);
push(l, r, x, p-1LL);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> a >> b;
p = a/gcd(a, b+1);
while (n--) {
ll l, r;
cin >> l >> r;
ll x = l/b, y = r/b;
if (x == y) {
add(l%b, r%b, x%p, y%p);
} else {
if (y-x > 1) {
add(0LL, b-1LL, (x+1LL)%p, (y-1LL)%p);
}
add(l%b, b-1LL, x%p, x%p);
add(0LL, r%b, y%p, y%p);
}
}
sort(v.begin(), v.end());
multiset<pair<ll, ll>> s;
ll ans = 0;
for (int i = 0; i+1 < v.size(); ++i) {
if (v[i].k) {
s.erase(s.find({v[i].l, v[i].r}));
} else {
s.insert({v[i].l, v[i].r});
}
ll d = v[i+1].x-v[i].x;
if (d) {
vector<pair<ll, ll>> u;
for (auto ss : s) {
u.emplace_back(ss.first, -1);
u.emplace_back(ss.second+1, 1);
}
sort(u.begin(), u.end());
ll q = 0;
int bal = 0;
for (int j = 0; j+1 < u.size(); ++j) {
bal -= u[j].second;
if (bal) q += u[j+1].first-u[j].first;
}
ans += q*d;
}
}
cout << ans;
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:70:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i+1 < v.size(); ++i) {
~~~~^~~~~~~~~~
strange_device.cpp:86:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j+1 < u.size(); ++j) {
~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
18 ms |
1532 KB |
Output is correct |
3 |
Correct |
18 ms |
1532 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 |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 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 |
8 ms |
384 KB |
Output is correct |
16 |
Correct |
19 ms |
1532 KB |
Output is correct |
17 |
Correct |
104 ms |
8652 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 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
16 ms |
768 KB |
Output is correct |
3 |
Correct |
12 ms |
512 KB |
Output is correct |
4 |
Correct |
41 ms |
640 KB |
Output is correct |
5 |
Correct |
2429 ms |
507604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1135 ms |
174864 KB |
Output is correct |
3 |
Correct |
2618 ms |
507708 KB |
Output is correct |
4 |
Correct |
2535 ms |
507660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1135 ms |
174864 KB |
Output is correct |
3 |
Correct |
2618 ms |
507708 KB |
Output is correct |
4 |
Correct |
2535 ms |
507660 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
3337 ms |
442060 KB |
Output is correct |
7 |
Correct |
1269 ms |
129484 KB |
Output is correct |
8 |
Correct |
3661 ms |
442028 KB |
Output is correct |
9 |
Correct |
3611 ms |
442176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
1135 ms |
174864 KB |
Output is correct |
3 |
Correct |
2618 ms |
507708 KB |
Output is correct |
4 |
Correct |
2535 ms |
507660 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
305 ms |
28620 KB |
Output is correct |
7 |
Correct |
488 ms |
48412 KB |
Output is correct |
8 |
Correct |
477 ms |
48284 KB |
Output is correct |
9 |
Correct |
474 ms |
48156 KB |
Output is correct |
10 |
Correct |
1160 ms |
22460 KB |
Output is correct |
11 |
Correct |
3346 ms |
46620 KB |
Output is correct |
12 |
Correct |
3307 ms |
48276 KB |
Output is correct |
13 |
Correct |
2398 ms |
52336 KB |
Output is correct |
14 |
Correct |
149 ms |
8680 KB |
Output is correct |
15 |
Execution timed out |
5062 ms |
27456 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
928 ms |
8688 KB |
Output is correct |
3 |
Execution timed out |
5071 ms |
8680 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
18 ms |
1532 KB |
Output is correct |
3 |
Correct |
18 ms |
1532 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 |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 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 |
8 ms |
384 KB |
Output is correct |
16 |
Correct |
19 ms |
1532 KB |
Output is correct |
17 |
Correct |
104 ms |
8652 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 |
5 ms |
384 KB |
Output is correct |
22 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |