The Preliminary Contest for ICPC Asia Shanghai 2019 - B. Light bulbs(差分 + 思维)

题目链接:https://nanti.jisuanke.com/t/41399

题意:一排 N个初始关着的灯泡,M个操作,每个操作使得区间 [L,R] 的状态反转,求最后开着灯泡的数量。

思路:内存只有8192K,考虑差分,但不能直接遍历,考虑到每一个区间只有奇数才会有贡献,而 M 只有1000,直接遍历区间计算贡献即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
const int N = 2e5 + 7;
int n, m;
map <int, int > mp;
int main()
{
    int T, ca = 1; scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d",&n, &m);
        while(m--)
        {
            int x, y; scanf("%d%d",&x, &y);
            mp[x]++; mp[y+1]--;
        }
        int pre = 0, ans = 0, sum = 0;
        for(auto x : mp)
        {
            if(sum & 1) ans += x.first - pre;
            pre = x.first;
            sum += x.second;
        }
        mp.clear();
        printf("Case #%d: %d\n", ca++, ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值