#include <iostream>
#include <vector>
#include <queue>
#include <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;
map<ll,ll> mp;
ll n,a,b,t,crto = 0,rs = 0;
ll gcd(ll x,ll y) {return y?gcd(y,x%y):x;}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>a>>b;
t = a / gcd(a,b+1);
for (int i=0;i<n;i++)
{
ll l,r;cin>>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());
ll 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);
cout<<rs<<"\n";
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:48:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<cr.size();i++)
~^~~~~~~~~~
strange_device.cpp:73:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<dt.size();i++)
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
1080 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 |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
13 ms |
1080 KB |
Output is correct |
17 |
Correct |
73 ms |
5992 KB |
Output is correct |
18 |
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 |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 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 |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
605 ms |
17148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2054 ms |
102500 KB |
Output is correct |
3 |
Correct |
662 ms |
17364 KB |
Output is correct |
4 |
Correct |
667 ms |
17232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2054 ms |
102500 KB |
Output is correct |
3 |
Correct |
662 ms |
17364 KB |
Output is correct |
4 |
Correct |
667 ms |
17232 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3612 ms |
157204 KB |
Output is correct |
7 |
Correct |
1662 ms |
50344 KB |
Output is correct |
8 |
Correct |
3632 ms |
157268 KB |
Output is correct |
9 |
Correct |
3721 ms |
157376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2054 ms |
102500 KB |
Output is correct |
3 |
Correct |
662 ms |
17364 KB |
Output is correct |
4 |
Correct |
667 ms |
17232 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
310 ms |
15088 KB |
Output is correct |
7 |
Correct |
268 ms |
18316 KB |
Output is correct |
8 |
Correct |
262 ms |
18312 KB |
Output is correct |
9 |
Correct |
269 ms |
18336 KB |
Output is correct |
10 |
Correct |
199 ms |
12892 KB |
Output is correct |
11 |
Correct |
271 ms |
18180 KB |
Output is correct |
12 |
Correct |
269 ms |
18172 KB |
Output is correct |
13 |
Correct |
293 ms |
18192 KB |
Output is correct |
14 |
Correct |
72 ms |
5740 KB |
Output is correct |
15 |
Correct |
262 ms |
14952 KB |
Output is correct |
16 |
Correct |
112 ms |
5616 KB |
Output is correct |
17 |
Correct |
344 ms |
17948 KB |
Output is correct |
18 |
Correct |
3395 ms |
157228 KB |
Output is correct |
19 |
Correct |
3446 ms |
157400 KB |
Output is correct |
20 |
Correct |
3484 ms |
157352 KB |
Output is correct |
21 |
Correct |
263 ms |
18280 KB |
Output is correct |
22 |
Correct |
260 ms |
18152 KB |
Output is correct |
23 |
Correct |
217 ms |
10080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
89 ms |
6384 KB |
Output is correct |
3 |
Correct |
107 ms |
5732 KB |
Output is correct |
4 |
Correct |
1863 ms |
65844 KB |
Output is correct |
5 |
Correct |
118 ms |
7520 KB |
Output is correct |
6 |
Correct |
118 ms |
7292 KB |
Output is correct |
7 |
Correct |
119 ms |
7300 KB |
Output is correct |
8 |
Correct |
134 ms |
8516 KB |
Output is correct |
9 |
Correct |
141 ms |
8556 KB |
Output is correct |
10 |
Correct |
101 ms |
5860 KB |
Output is correct |
11 |
Correct |
103 ms |
5864 KB |
Output is correct |
12 |
Correct |
91 ms |
5952 KB |
Output is correct |
13 |
Correct |
123 ms |
7108 KB |
Output is correct |
14 |
Correct |
1061 ms |
25540 KB |
Output is correct |
15 |
Correct |
103 ms |
5984 KB |
Output is correct |
16 |
Correct |
937 ms |
25600 KB |
Output is correct |
17 |
Correct |
1699 ms |
62836 KB |
Output is correct |
18 |
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 |
11 ms |
1080 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 |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
13 ms |
1080 KB |
Output is correct |
17 |
Correct |
73 ms |
5992 KB |
Output is correct |
18 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
4 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
4 ms |
504 KB |
Output is correct |
28 |
Correct |
605 ms |
17148 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2054 ms |
102500 KB |
Output is correct |
31 |
Correct |
662 ms |
17364 KB |
Output is correct |
32 |
Correct |
667 ms |
17232 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
3612 ms |
157204 KB |
Output is correct |
35 |
Correct |
1662 ms |
50344 KB |
Output is correct |
36 |
Correct |
3632 ms |
157268 KB |
Output is correct |
37 |
Correct |
3721 ms |
157376 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
310 ms |
15088 KB |
Output is correct |
40 |
Correct |
268 ms |
18316 KB |
Output is correct |
41 |
Correct |
262 ms |
18312 KB |
Output is correct |
42 |
Correct |
269 ms |
18336 KB |
Output is correct |
43 |
Correct |
199 ms |
12892 KB |
Output is correct |
44 |
Correct |
271 ms |
18180 KB |
Output is correct |
45 |
Correct |
269 ms |
18172 KB |
Output is correct |
46 |
Correct |
293 ms |
18192 KB |
Output is correct |
47 |
Correct |
72 ms |
5740 KB |
Output is correct |
48 |
Correct |
262 ms |
14952 KB |
Output is correct |
49 |
Correct |
112 ms |
5616 KB |
Output is correct |
50 |
Correct |
344 ms |
17948 KB |
Output is correct |
51 |
Correct |
3395 ms |
157228 KB |
Output is correct |
52 |
Correct |
3446 ms |
157400 KB |
Output is correct |
53 |
Correct |
3484 ms |
157352 KB |
Output is correct |
54 |
Correct |
263 ms |
18280 KB |
Output is correct |
55 |
Correct |
260 ms |
18152 KB |
Output is correct |
56 |
Correct |
217 ms |
10080 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
89 ms |
6384 KB |
Output is correct |
59 |
Correct |
107 ms |
5732 KB |
Output is correct |
60 |
Correct |
1863 ms |
65844 KB |
Output is correct |
61 |
Correct |
118 ms |
7520 KB |
Output is correct |
62 |
Correct |
118 ms |
7292 KB |
Output is correct |
63 |
Correct |
119 ms |
7300 KB |
Output is correct |
64 |
Correct |
134 ms |
8516 KB |
Output is correct |
65 |
Correct |
141 ms |
8556 KB |
Output is correct |
66 |
Correct |
101 ms |
5860 KB |
Output is correct |
67 |
Correct |
103 ms |
5864 KB |
Output is correct |
68 |
Correct |
91 ms |
5952 KB |
Output is correct |
69 |
Correct |
123 ms |
7108 KB |
Output is correct |
70 |
Correct |
1061 ms |
25540 KB |
Output is correct |
71 |
Correct |
103 ms |
5984 KB |
Output is correct |
72 |
Correct |
937 ms |
25600 KB |
Output is correct |
73 |
Correct |
1699 ms |
62836 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
504 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
1 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
18 ms |
1660 KB |
Output is correct |
80 |
Correct |
1242 ms |
63176 KB |
Output is correct |
81 |
Correct |
1889 ms |
102396 KB |
Output is correct |
82 |
Correct |
4728 ms |
194312 KB |
Output is correct |
83 |
Execution timed out |
5074 ms |
194508 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |