#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int nx=1e6+5;
ll n, a, b, l[nx], r[nx], sm, p, mx, res, tmp;
set<pair<ll, ll>> s;
void add_range(ll x, ll y)
{
x=x%p;
y=y%p;
if (y<x) y+=p;
s.insert({x, min(p-1, y)});
if (y>=p) s.insert({0, y-p});
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>a>>b;
for (int i=1; i<=n; i++) cin>>l[i]>>r[i], mx=max(mx, r[i]-l[i]+1), sm+=(r[i]-l[i]+1);
tmp=a/__gcd(a, b+1);
if (tmp >= LLONG_MAX/b) return cout<<sm, 0;
p=tmp*b;
if (mx>=p) return cout<<p, 0;
for (int i=1; i<=n; i++) add_range(l[i], r[i]);
//for (auto [x, y]:s) cout<<"val "<<x<<' '<<y<<'\n';
for (auto itr=s.begin(); itr!=s.end()&&next(itr)!=s.end();)
{
auto itr2=next(itr);
pair<ll, ll> nw;
if (itr2->first>itr->second+1) itr=itr2;
else nw={itr->first, max(itr->second, itr2->second)}, s.erase(itr), s.erase(itr2), s.insert(nw), itr=s.find(nw);
}
for (auto [x, y]:s) res+=y-x+1;
cout<<res;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:24:20: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
24 | for (int i=1; i<=n; i++) cin>>l[i]>>r[i], mx=max(mx, r[i]-l[i]+1), sm+=(r[i]-l[i]+1);
| ~^~~
strange_device.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
29 | for (int i=1; i<=n; i++) add_range(l[i], r[i]);
| ~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
5 ms |
3164 KB |
Output is correct |
3 |
Correct |
5 ms |
3164 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2512 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
3164 KB |
Output is correct |
17 |
Correct |
50 ms |
12884 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
171 ms |
16000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
497 ms |
78676 KB |
Output is correct |
3 |
Correct |
515 ms |
78716 KB |
Output is correct |
4 |
Correct |
538 ms |
78832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
497 ms |
78676 KB |
Output is correct |
3 |
Correct |
515 ms |
78716 KB |
Output is correct |
4 |
Correct |
538 ms |
78832 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
488 ms |
78716 KB |
Output is correct |
7 |
Correct |
550 ms |
78704 KB |
Output is correct |
8 |
Correct |
502 ms |
78672 KB |
Output is correct |
9 |
Correct |
579 ms |
78556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
497 ms |
78676 KB |
Output is correct |
3 |
Correct |
515 ms |
78716 KB |
Output is correct |
4 |
Correct |
538 ms |
78832 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
47 ms |
12808 KB |
Output is correct |
7 |
Correct |
45 ms |
12880 KB |
Output is correct |
8 |
Correct |
46 ms |
12880 KB |
Output is correct |
9 |
Correct |
48 ms |
12884 KB |
Output is correct |
10 |
Correct |
45 ms |
13040 KB |
Output is correct |
11 |
Correct |
46 ms |
12960 KB |
Output is correct |
12 |
Correct |
61 ms |
12932 KB |
Output is correct |
13 |
Correct |
49 ms |
12900 KB |
Output is correct |
14 |
Correct |
45 ms |
12892 KB |
Output is correct |
15 |
Correct |
54 ms |
12928 KB |
Output is correct |
16 |
Correct |
51 ms |
12880 KB |
Output is correct |
17 |
Correct |
55 ms |
12880 KB |
Output is correct |
18 |
Correct |
486 ms |
78672 KB |
Output is correct |
19 |
Correct |
536 ms |
78708 KB |
Output is correct |
20 |
Correct |
589 ms |
78928 KB |
Output is correct |
21 |
Correct |
50 ms |
12880 KB |
Output is correct |
22 |
Correct |
48 ms |
13040 KB |
Output is correct |
23 |
Correct |
85 ms |
10868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
53 ms |
12924 KB |
Output is correct |
3 |
Correct |
52 ms |
12884 KB |
Output is correct |
4 |
Correct |
565 ms |
78716 KB |
Output is correct |
5 |
Correct |
53 ms |
12880 KB |
Output is correct |
6 |
Correct |
54 ms |
13032 KB |
Output is correct |
7 |
Correct |
52 ms |
12880 KB |
Output is correct |
8 |
Correct |
52 ms |
13032 KB |
Output is correct |
9 |
Correct |
49 ms |
12892 KB |
Output is correct |
10 |
Correct |
47 ms |
12880 KB |
Output is correct |
11 |
Correct |
48 ms |
12928 KB |
Output is correct |
12 |
Correct |
47 ms |
12888 KB |
Output is correct |
13 |
Correct |
59 ms |
12944 KB |
Output is correct |
14 |
Correct |
601 ms |
78928 KB |
Output is correct |
15 |
Correct |
47 ms |
13036 KB |
Output is correct |
16 |
Correct |
483 ms |
78676 KB |
Output is correct |
17 |
Correct |
486 ms |
78716 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
5 ms |
3164 KB |
Output is correct |
3 |
Correct |
5 ms |
3164 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2512 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
3164 KB |
Output is correct |
17 |
Correct |
50 ms |
12884 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
171 ms |
16000 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
497 ms |
78676 KB |
Output is correct |
31 |
Correct |
515 ms |
78716 KB |
Output is correct |
32 |
Correct |
538 ms |
78832 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
488 ms |
78716 KB |
Output is correct |
35 |
Correct |
550 ms |
78704 KB |
Output is correct |
36 |
Correct |
502 ms |
78672 KB |
Output is correct |
37 |
Correct |
579 ms |
78556 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
47 ms |
12808 KB |
Output is correct |
40 |
Correct |
45 ms |
12880 KB |
Output is correct |
41 |
Correct |
46 ms |
12880 KB |
Output is correct |
42 |
Correct |
48 ms |
12884 KB |
Output is correct |
43 |
Correct |
45 ms |
13040 KB |
Output is correct |
44 |
Correct |
46 ms |
12960 KB |
Output is correct |
45 |
Correct |
61 ms |
12932 KB |
Output is correct |
46 |
Correct |
49 ms |
12900 KB |
Output is correct |
47 |
Correct |
45 ms |
12892 KB |
Output is correct |
48 |
Correct |
54 ms |
12928 KB |
Output is correct |
49 |
Correct |
51 ms |
12880 KB |
Output is correct |
50 |
Correct |
55 ms |
12880 KB |
Output is correct |
51 |
Correct |
486 ms |
78672 KB |
Output is correct |
52 |
Correct |
536 ms |
78708 KB |
Output is correct |
53 |
Correct |
589 ms |
78928 KB |
Output is correct |
54 |
Correct |
50 ms |
12880 KB |
Output is correct |
55 |
Correct |
48 ms |
13040 KB |
Output is correct |
56 |
Correct |
85 ms |
10868 KB |
Output is correct |
57 |
Correct |
1 ms |
2392 KB |
Output is correct |
58 |
Correct |
53 ms |
12924 KB |
Output is correct |
59 |
Correct |
52 ms |
12884 KB |
Output is correct |
60 |
Correct |
565 ms |
78716 KB |
Output is correct |
61 |
Correct |
53 ms |
12880 KB |
Output is correct |
62 |
Correct |
54 ms |
13032 KB |
Output is correct |
63 |
Correct |
52 ms |
12880 KB |
Output is correct |
64 |
Correct |
52 ms |
13032 KB |
Output is correct |
65 |
Correct |
49 ms |
12892 KB |
Output is correct |
66 |
Correct |
47 ms |
12880 KB |
Output is correct |
67 |
Correct |
48 ms |
12928 KB |
Output is correct |
68 |
Correct |
47 ms |
12888 KB |
Output is correct |
69 |
Correct |
59 ms |
12944 KB |
Output is correct |
70 |
Correct |
601 ms |
78928 KB |
Output is correct |
71 |
Correct |
47 ms |
13036 KB |
Output is correct |
72 |
Correct |
483 ms |
78676 KB |
Output is correct |
73 |
Correct |
486 ms |
78716 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
1 ms |
2392 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2496 KB |
Output is correct |
78 |
Correct |
1 ms |
2396 KB |
Output is correct |
79 |
Correct |
5 ms |
3420 KB |
Output is correct |
80 |
Correct |
610 ms |
115796 KB |
Output is correct |
81 |
Correct |
618 ms |
115692 KB |
Output is correct |
82 |
Correct |
725 ms |
115836 KB |
Output is correct |
83 |
Correct |
710 ms |
115920 KB |
Output is correct |
84 |
Correct |
714 ms |
115820 KB |
Output is correct |
85 |
Correct |
589 ms |
115912 KB |
Output is correct |
86 |
Correct |
80 ms |
23156 KB |
Output is correct |
87 |
Correct |
1 ms |
2716 KB |
Output is correct |