分情况讨论找规律
#include<bitsstdc++.h>
#define ll long long
#define maxn 1000005
using namespace std;
struct nod{
int l,r,v;
nod(){}
nod(int a,int b,int c) {l=a; r=b; v=c;}
};
int n,q;
int num[505][505];
ll dp[505][505];
vector<nod> now;
ll dfs(int l,int r){
if (dp[l][r]!=-1) return dp[l][r];
if (l==r) return dp[l][r]=0;
dp[l][r]=1ll<<60;
for (int k=l;k<r;k++)
{
ll cost=0;
// for (nod tt:now)
for (vector<nod>::iterator tt = now.begin(); tt < now.end(); tt++)
if (max((*tt).l,l)<=min((*tt).r,r))
{
if (!((*tt).l<=l && r<=(*tt).r))
if ((*tt).l<=k && k+1<=(*tt).r) cost+=(*tt).v;
}
dp[l][r]=min(dp[l][r],dfs(l,k)+dfs(k+1,r)+cost);
}
return dp[l][r];
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>q;
int l,r;
for (int i=1;i<=q;i++) {cin>>l>>r; num[l][r]++;}
for (int l=1;l<=n;l++)
for (int r=l;r<=n;r++)
if (num[l][r])
now.push_back(nod(l,r,num[l][r]));
cout<<dfs(1,n)+q<<endl;
}