题目
思路
m的范围只有1-10,直接暴力枚举所有方案
考虑使用位运算 即遍历1-(1<<m)来枚举全部方案
使用差分+前缀和来存每一段被覆盖的数量
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define pll pair<ll,ll>
#define endl '\n'
const int Inf =1e18;
const int inf = 0x3f3f3f3f;
inline ll read();
const int N = 2e5 + 10;
const int mod=998244353;
int l[N],r[N];
void solve() {
int n=read(),m=read();
for(int i=0;i<m;i++){
l[i]=read(),r[i]=read();
}
int ans=0;
for(int i=0;i<(1<<m);i++){//m条线段,二进制就是(1<<m)
vector<int>a(N);//差分数组
for(int j=0;j<m;j++){
if(i&(1<<j)){//位运算判断这条线段是否被选择
a[l[j]]++;
a[r[j]+1]--;
}
}
for(int j=1;j<=n;j++){//差分数组转变为前缀和数组
a[j]+=a[j-1];
}
bool f=0;
for(int j=1;j<=n;j++){
if(a[j]<2){
f=1;
break;
}
}
if(f==0)ans++;
ans%=mod;//记得取模
}
cout<<ans<<endl;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int tt = 1;
//tt=read();
while (tt--)
solve();
}
inline ll read() {
ll x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}