#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <algorithm>
#define ll long long
#define pii pair<ll,ll>
#define ppi pair<pii,ll>
#define fst first
#define snd second
#define pub push_back
using namespace std;
vector<pii> cr,cr2;
vector<ppi> dt;
priority_queue<pii,vector<pii>,greater<pii>> pq;
unordered_map<ll,ll> mp;
ll n,a,b,t,crto = 0,rs = 0,l,r;
ll gcd(ll x,ll y) {return y?gcd(y,x%y):x;}
int main()
{
scanf("%lld %lld %lld",&n,&a,&b);
t = a / gcd(a,b+1);
for (int i=0;i<n;i++)
{
scanf("%lld %lld",&l,&r);
if (l/b == r/b) {dt.push_back({{l%b,r%b},(l/b)%t});}
else
{
if (r%b != b-1) {dt.push_back({{0,r%b},(r/b)%t});}
if (l%b != 0) {dt.push_back({{l%b,b-1},(l/b)%t});}
if ((l+(b-1))/b <= (r-(b-1))/b)
{
ll l2 = ((l+(b-1))/b)%t;
ll r2 = ((r-(b-1))/b)%t;
if (((r-(b-1))/b) - ((l+(b-1))/b) >= t) {cr.pub({0,t-1});}
else if (l2<=r2) {cr.pub({l2,r2});}
else {cr.pub({l2,t-1});cr.pub({0,r2});}
}
}
}
sort(dt.begin(),dt.end());
sort(cr.begin(),cr.end());
l = -1,r = -2;
for (int i=0;i<cr.size();i++)
{
if (r+1<cr[i].fst)
{
if (l!=-1) {cr2.pub({l,r});crto += r - l + 1;}
l = cr[i].fst;
r = cr[i].fst;
}
r = max(r,cr[i].snd);
}
if (l!=-1) {cr2.pub({l,r});crto += r - l + 1;}
//cr.clear();
/*
cout<<"-- DT --\n";
for (int i=0;i<dt.size();i++)
{
cout<<dt[i].fst.fst<<" "<<dt[i].fst.snd<<" "<<dt[i].snd<<"\n";
}
cout<<"-- CR2 --\n";
for (int i=0;i<cr2.size();i++)
{
cout<<cr2[i].fst<<" "<<cr2[i].snd<<"\n";
}
*/
ll pv = 0;
for (int i=0;i<dt.size();i++)
{
while (pq.size())
{
if (pq.top().fst < dt[i].fst.fst)
{
rs += crto * (pq.top().fst+1-pv);
pv = pq.top().fst+1;
mp[pq.top().snd]--;
if (mp[pq.top().snd]==0) {crto--;}
pq.pop();
}
else {break;}
}
//cout<<i<<" "<<crto<<" "<<rs<<" "<<pv<<"\n";
auto it = upper_bound(cr2.begin(),cr2.end(),make_pair(dt[i].snd+1,(ll)-1));
if (it != cr2.begin())
{
it = prev(it);
if (dt[i].snd > (it->snd))
{
rs += crto * (dt[i].fst.fst-pv);
pv = dt[i].fst.fst;
mp[dt[i].snd]++;
if (mp[dt[i].snd]==1) {crto++;}
pq.push({dt[i].fst.snd,dt[i].snd});
}
}
else
{
rs += crto * (dt[i].fst.fst-pv);
pv = dt[i].fst.fst;
mp[dt[i].snd]++;
if (mp[dt[i].snd]==1) {crto++;}
pq.push({dt[i].fst.snd,dt[i].snd});
}
//cout<<i<<" "<<crto<<" "<<rs<<" "<<pv<<"\n";
}
while (pq.size())
{
rs += crto * (pq.top().fst+1-pv);
pv = pq.top().fst+1;
mp[pq.top().snd]--;
if (mp[pq.top().snd]==0) {crto--;}
pq.pop();
}
rs += crto * (b - pv);
printf("%lld\n",rs);
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:47:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<cr.size();i++)
~^~~~~~~~~~
strange_device.cpp:72:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<dt.size();i++)
~^~~~~~~~~~
strange_device.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld %lld",&n,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&l,&r);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
10 ms |
1208 KB |
Output is correct |
3 |
Correct |
10 ms |
1208 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 |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
10 ms |
1208 KB |
Output is correct |
17 |
Correct |
69 ms |
6556 KB |
Output is correct |
18 |
Correct |
2 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
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 |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
504 ms |
17248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
962 ms |
89724 KB |
Output is correct |
3 |
Correct |
605 ms |
17500 KB |
Output is correct |
4 |
Correct |
594 ms |
17232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
962 ms |
89724 KB |
Output is correct |
3 |
Correct |
605 ms |
17500 KB |
Output is correct |
4 |
Correct |
594 ms |
17232 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2281 ms |
144532 KB |
Output is correct |
7 |
Correct |
831 ms |
45744 KB |
Output is correct |
8 |
Correct |
2177 ms |
144468 KB |
Output is correct |
9 |
Correct |
2216 ms |
144600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
962 ms |
89724 KB |
Output is correct |
3 |
Correct |
605 ms |
17500 KB |
Output is correct |
4 |
Correct |
594 ms |
17232 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
136 ms |
14032 KB |
Output is correct |
7 |
Correct |
188 ms |
17028 KB |
Output is correct |
8 |
Correct |
208 ms |
17016 KB |
Output is correct |
9 |
Correct |
192 ms |
17164 KB |
Output is correct |
10 |
Correct |
140 ms |
12808 KB |
Output is correct |
11 |
Correct |
201 ms |
17000 KB |
Output is correct |
12 |
Correct |
194 ms |
17000 KB |
Output is correct |
13 |
Correct |
170 ms |
17008 KB |
Output is correct |
14 |
Correct |
69 ms |
6136 KB |
Output is correct |
15 |
Correct |
182 ms |
14284 KB |
Output is correct |
16 |
Correct |
79 ms |
6252 KB |
Output is correct |
17 |
Correct |
257 ms |
17324 KB |
Output is correct |
18 |
Correct |
2619 ms |
144552 KB |
Output is correct |
19 |
Correct |
2851 ms |
144452 KB |
Output is correct |
20 |
Correct |
3004 ms |
144568 KB |
Output is correct |
21 |
Correct |
207 ms |
16748 KB |
Output is correct |
22 |
Correct |
214 ms |
16608 KB |
Output is correct |
23 |
Correct |
226 ms |
11088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
74 ms |
5096 KB |
Output is correct |
3 |
Correct |
80 ms |
5352 KB |
Output is correct |
4 |
Correct |
1577 ms |
56328 KB |
Output is correct |
5 |
Correct |
82 ms |
4956 KB |
Output is correct |
6 |
Correct |
81 ms |
4968 KB |
Output is correct |
7 |
Correct |
80 ms |
4968 KB |
Output is correct |
8 |
Correct |
91 ms |
5860 KB |
Output is correct |
9 |
Correct |
213 ms |
5984 KB |
Output is correct |
10 |
Correct |
76 ms |
5612 KB |
Output is correct |
11 |
Correct |
69 ms |
5736 KB |
Output is correct |
12 |
Correct |
71 ms |
5224 KB |
Output is correct |
13 |
Correct |
94 ms |
5332 KB |
Output is correct |
14 |
Correct |
758 ms |
25544 KB |
Output is correct |
15 |
Correct |
76 ms |
5224 KB |
Output is correct |
16 |
Correct |
706 ms |
25848 KB |
Output is correct |
17 |
Correct |
1299 ms |
55428 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
10 ms |
1208 KB |
Output is correct |
3 |
Correct |
10 ms |
1208 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 |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
10 ms |
1208 KB |
Output is correct |
17 |
Correct |
69 ms |
6556 KB |
Output is correct |
18 |
Correct |
2 ms |
360 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
504 ms |
17248 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
962 ms |
89724 KB |
Output is correct |
31 |
Correct |
605 ms |
17500 KB |
Output is correct |
32 |
Correct |
594 ms |
17232 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
2281 ms |
144532 KB |
Output is correct |
35 |
Correct |
831 ms |
45744 KB |
Output is correct |
36 |
Correct |
2177 ms |
144468 KB |
Output is correct |
37 |
Correct |
2216 ms |
144600 KB |
Output is correct |
38 |
Correct |
2 ms |
256 KB |
Output is correct |
39 |
Correct |
136 ms |
14032 KB |
Output is correct |
40 |
Correct |
188 ms |
17028 KB |
Output is correct |
41 |
Correct |
208 ms |
17016 KB |
Output is correct |
42 |
Correct |
192 ms |
17164 KB |
Output is correct |
43 |
Correct |
140 ms |
12808 KB |
Output is correct |
44 |
Correct |
201 ms |
17000 KB |
Output is correct |
45 |
Correct |
194 ms |
17000 KB |
Output is correct |
46 |
Correct |
170 ms |
17008 KB |
Output is correct |
47 |
Correct |
69 ms |
6136 KB |
Output is correct |
48 |
Correct |
182 ms |
14284 KB |
Output is correct |
49 |
Correct |
79 ms |
6252 KB |
Output is correct |
50 |
Correct |
257 ms |
17324 KB |
Output is correct |
51 |
Correct |
2619 ms |
144552 KB |
Output is correct |
52 |
Correct |
2851 ms |
144452 KB |
Output is correct |
53 |
Correct |
3004 ms |
144568 KB |
Output is correct |
54 |
Correct |
207 ms |
16748 KB |
Output is correct |
55 |
Correct |
214 ms |
16608 KB |
Output is correct |
56 |
Correct |
226 ms |
11088 KB |
Output is correct |
57 |
Correct |
2 ms |
256 KB |
Output is correct |
58 |
Correct |
74 ms |
5096 KB |
Output is correct |
59 |
Correct |
80 ms |
5352 KB |
Output is correct |
60 |
Correct |
1577 ms |
56328 KB |
Output is correct |
61 |
Correct |
82 ms |
4956 KB |
Output is correct |
62 |
Correct |
81 ms |
4968 KB |
Output is correct |
63 |
Correct |
80 ms |
4968 KB |
Output is correct |
64 |
Correct |
91 ms |
5860 KB |
Output is correct |
65 |
Correct |
213 ms |
5984 KB |
Output is correct |
66 |
Correct |
76 ms |
5612 KB |
Output is correct |
67 |
Correct |
69 ms |
5736 KB |
Output is correct |
68 |
Correct |
71 ms |
5224 KB |
Output is correct |
69 |
Correct |
94 ms |
5332 KB |
Output is correct |
70 |
Correct |
758 ms |
25544 KB |
Output is correct |
71 |
Correct |
76 ms |
5224 KB |
Output is correct |
72 |
Correct |
706 ms |
25848 KB |
Output is correct |
73 |
Correct |
1299 ms |
55428 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
2 ms |
256 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
15 ms |
1404 KB |
Output is correct |
80 |
Correct |
847 ms |
25520 KB |
Output is correct |
81 |
Correct |
1372 ms |
55956 KB |
Output is correct |
82 |
Correct |
3480 ms |
144092 KB |
Output is correct |
83 |
Correct |
3147 ms |
144152 KB |
Output is correct |
84 |
Correct |
3712 ms |
181208 KB |
Output is correct |
85 |
Correct |
3471 ms |
181444 KB |
Output is correct |
86 |
Correct |
241 ms |
18520 KB |
Output is correct |
87 |
Correct |
3 ms |
256 KB |
Output is correct |