#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
struct Point
{
int x,id;
bool en;
Point(int _x=0,int _id=0,int _en=0) : x(_x),id(_id),en(_en) {};
bool operator<(const Point &oth) const
{
return x<oth.x;
}
};
array<int,2> t[N+10];
vector<Point>::iterator pos[N+10][2];
bool vis[N+10][2];
vector<Point> st;
vector<int> siz;
int dfs(vector<Point>::iterator x)
{
int ans=0;
while(x!=st.end() && !vis[(x->id)][(x->en)])
{
vis[(x->id)][(x->en)]=true;
ans++;
x=next(pos[(x->id)][!(x->en)]);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>t[i][0]>>t[i][1];
st.push_back(Point(t[i][0],i,0));
st.push_back(Point(t[i][1],i,1));
}
sort(st.begin(),st.end());
for(auto it=st.begin();it!=st.end();it++)
pos[(it->id)][(it->en)]=it;
int ans=dfs(st.begin());
for(int i=1;i<=n;i++)
{
for(int j:{0,1})
{
if(!vis[i][j])
siz.push_back(dfs(pos[i][j]));
}
}
sort(siz.begin(),siz.end());
while(m--)
{
if(siz.empty())
{
ans++;
siz.push_back(1);
}
else
{
ans+=2+siz.back();
siz.pop_back();
}
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
6 ms |
908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
6 ms |
948 KB |
Output is correct |
3 |
Correct |
14 ms |
1928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
6940 KB |
Output is correct |
2 |
Correct |
241 ms |
15892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
11556 KB |
Output is correct |
2 |
Correct |
338 ms |
24848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
464 ms |
43548 KB |
Output is correct |
2 |
Correct |
555 ms |
51932 KB |
Output is correct |
3 |
Correct |
581 ms |
59480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
701 ms |
59576 KB |
Output is correct |
2 |
Correct |
743 ms |
64620 KB |
Output is correct |
3 |
Correct |
618 ms |
63152 KB |
Output is correct |
4 |
Correct |
602 ms |
63872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
771 ms |
65536 KB |
Output is correct |
2 |
Correct |
793 ms |
65536 KB |
Output is correct |
3 |
Correct |
503 ms |
65536 KB |
Output is correct |
4 |
Correct |
752 ms |
65536 KB |
Output is correct |