#include<bits/stdc++.h>
using namespace std;
#define N 4000010
#define NN 300100
#define ll long long
#define lb long double
#define P pair<ll,ll>
#define LL long long
//#define P 1000000007ll
//priority_queue<int >que;
//priority_queue<int,vector<int>,greater<int> >que;
//upper_bound(nums.begin(), nums.end(), value);
int n,m,k;
P ra[N];
int a[N];
queue<pair<P,int>>que;
map<int,int>ma1;
map<P,int>ma2;
void solve(){
cin>>n>>m>>k;
ma1.clear();
ma2.clear();
for(int i=1;i<=n;i++) a[i]=0;
for(int i=1;i<=m;i++){
cin>>ra[i].first>>ra[i].second;
a[ra[i].first]++;
a[ra[i].second+1]--;
}
sort(ra+1,ra+1+m);
int ans=0;
a[0]=0;
for(int i=1;i<=n;i++){
a[i]+=a[i-1];
if(a[i]==0) ans++;
}
//
// cout<<"[DEBUG]"<<endl;
// for(int i=1;i<=n;i++)
// cout<<a[i]<<" ";
// cout<<endl<<"[DEBUG]"<<endl;
ra[m+1].first=1000000000;
while (!que.empty()){
que.pop();
}
int nx=1;
for(int i=1;i<=n;i++){
while(que.empty()==false && que.front().first.second+1<=i) {
que.pop();
}
while(i>=ra[nx].first) {
que.push({ra[nx],nx});
nx++;
}
if(a[i]==1) {
int n1=que.front().second;
ma1[n1]++;
// cout<<"n3="<<n1<<endl;
// cout<<"-----"<<endl;
}
if(a[i]==2) {
int n1=que.front().second;
pair<P,int> tmp=que.front();
que.pop();
while(que.empty()==false && que.front().first.second+1<=i) {
que.pop();
}
int n2=que.front().second;
ma2[{min(n1,n2),max(n1,n2)}]++;
// cout<<"n1="<<n1<<endl;
// cout<<"n2="<<n2<<endl;
// cout<<"-----"<<endl;
que.push(tmp);
}
}
int m1=0,m2=0;
for(auto it:ma1){
int val=it.second;
if(m1<val){
m2=m1;
m1=val;
}
else{
m2=max(m2,val);
}
}
int res=m1+m2;
// cout<<"res="<<res<<endl;
// cout<<"-----"<<endl;
for(auto it :ma2){
int x1=it.first.first;
int x2=it.first.second;
int val=it.second;
res=max(res,ma1[x1]+ma1[x2]+val);
}
ans+=res;
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin>>T;
for (int t = 1; t <= T; t++) {
solve();
}
return 0;
}
Codeforces Round 906 (Div. 2) E1. Doremy‘s Drying Plan (Easy Version)
最新推荐文章于 2024-11-07 23:24:07 发布