牛客小白月赛90 D.小A的线段(easy version)

题目

思路

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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值